feat(商城): 代码优化

master
wayn 12 months ago
parent ba9ab56076
commit db7b14ff19

@ -27,7 +27,7 @@
<spring-boot.version>3.1.4</spring-boot.version>
<mysql.connector.java.version>8.0.30</mysql.connector.java.version>
<lettuce.version>6.2.3.RELEASE</lettuce.version>
<elasticsearch.version>7.17.9</elasticsearch.version>
<elasticsearch.version>7.17.14</elasticsearch.version>
<mybatis-plus.version>3.5.4.1</mybatis-plus.version>
<druid.version>1.2.20</druid.version>
<lombok.version>1.18.26</lombok.version>

@ -1,35 +1,41 @@
{
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"sales": {
"type": "integer"
},
"isHot": {
"type": "boolean"
},
"isNew": {
"type": "boolean"
},
"countPrice": {
"type": "float"
},
"retailPrice": {
"type": "float"
},
"keyword": {
"type": "keyword"
},
"isOnSale": {
"type": "boolean"
},
"createTime": {
"type": "date"
}
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"suggest": {
"type": "completion",
"analyzer": "ik_max_word"
}
}
},
"sales": {
"type": "integer"
},
"isHot": {
"type": "boolean"
},
"isNew": {
"type": "boolean"
},
"countPrice": {
"type": "float"
},
"retailPrice": {
"type": "float"
},
"keyword": {
"type": "keyword"
},
"isOnSale": {
"type": "boolean"
},
"createTime": {
"type": "date"
}
}
}

@ -1,6 +1,5 @@
package com.wayn.common.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.wayn.common.enums.ReturnCodeEnum;
import lombok.Getter;

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.wayn.data.elastic.config.ElasticConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
@ -31,6 +32,8 @@ import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
@ -187,7 +190,7 @@ public class ElasticDocument implements DisposableBean {
* @param c
* @return java.util.List<T>
*/
public <T> List<T> search(String idxName, SearchSourceBuilder builder, Class<T> c) throws IOException {
public <T> List<T> searchResult(String idxName, SearchSourceBuilder builder, Class<T> c) throws IOException {
SearchRequest request = new SearchRequest(idxName);
request.source(builder);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
@ -195,6 +198,37 @@ public class ElasticDocument implements DisposableBean {
return Arrays.stream(hits).map(hit -> JSON.parseObject(hit.getSourceAsString(), c)).collect(Collectors.toList());
}
/**
*
*
* @param idxName index
* @param suggest
* @param builder
* @return java.util.List<String>
* @throws IOException
*/
public List<String> searchSuggest(String idxName, String suggest, SuggestBuilder builder) throws IOException {
SearchRequest searchRequest = new SearchRequest(idxName);
searchRequest.source(new SearchSourceBuilder().suggest(builder));
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
Suggest suggestions = response.getSuggest();
List<String> sugguestList = new ArrayList<>();
List<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> entries =
suggestions.getSuggestion(suggest).getEntries();
for (Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> entry : entries) {
for (Suggest.Suggestion.Entry.Option option : entry.getOptions()) {
String keyword = option.getText().string();
if (!StringUtils.isEmpty(keyword)) {
if (sugguestList.contains(keyword)) {
continue;
}
sugguestList.add(keyword);
}
}
}
return sugguestList;
}
/**
* http

@ -60,7 +60,7 @@ public class LoginController {
// 判断图形验证码
String redisCaptchaCode = redisCache.getCacheObject(registryObj.getCaptchaKey());
if (registryObj.getCaptchaCode() == null || !StringUtils.equalsIgnoreCase(redisCaptchaCode, registryObj.getCaptchaCode().trim())) {
if (registryObj.getCaptchaCode() == null || !redisCaptchaCode.equals(registryObj.getCaptchaCode().trim().toLowerCase())) {
return R.error(USER_CAPTCHA_CODE_ERROR);
}

@ -29,6 +29,10 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -58,6 +62,21 @@ public class SearchController extends BaseController {
private ElasticDocument elasticDocument;
@GetMapping("sugguest")
public R sugguest(SearchVO searchVO) throws IOException {
String keyword = searchVO.getKeyword();
String suggestField = "name.suggest";
String suggestName = "my-suggest";
SuggestionBuilder<CompletionSuggestionBuilder> termSuggestionBuilder = SuggestBuilders.completionSuggestion(suggestField)
.prefix(keyword)
.skipDuplicates(true)
.size(10);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion(suggestName, termSuggestionBuilder);
List<String> list = elasticDocument.searchSuggest("goods", suggestName, suggestBuilder);
return R.success().add("suggest", list);
}
@GetMapping("result")
public R result(SearchVO searchVO) throws IOException {
// 获取筛选、排序条件
@ -118,7 +137,7 @@ public class SearchController extends BaseController {
searchSourceBuilder.from((int) (page.getCurrent() - 1) * (int) page.getSize());
searchSourceBuilder.size((int) page.getSize());
// 执行Elasticsearch查询
List<JSONObject> list = elasticDocument.search("goods", searchSourceBuilder, JSONObject.class);
List<JSONObject> list = elasticDocument.searchResult("goods", searchSourceBuilder, JSONObject.class);
List<Integer> goodsIdList = list.stream().map(jsonObject -> (Integer) jsonObject.get("id")).collect(Collectors.toList());
if (goodsIdList.isEmpty()) {
return R.success().add("goods", Collections.emptyList());

@ -40,7 +40,8 @@ public class SecurityConfig {
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// cors启用
.cors(httpSecurityCorsConfigurer -> {})
.cors(httpSecurityCorsConfigurer -> {
})
// CSRF(跨站请求伪造)禁用因为不使用session
.csrf(AbstractHttpConfigurer::disable)
// 认证失败处理类
@ -50,8 +51,10 @@ public class SecurityConfig {
// 过滤请求
.authorizeHttpRequests(
registry -> registry
.requestMatchers("favicon.ico", "/actuator/**", "/login", "/registry", "/sendEmailCode", "/test/**", "/seckill/**", "/captcha").anonymous()
.requestMatchers("/home/**", "/category/**", "/comment/**", "/goods/detail/**", "/cart/goodsCount", "/diamond/**").permitAll()
.requestMatchers("favicon.ico", "/actuator/**", "/login", "/registry",
"/search/**", "/sendEmailCode", "/test/**", "/seckill/**", "/captcha").anonymous()
.requestMatchers("/home/**", "/category/**", "/comment/**",
"/goods/detail/**", "/cart/goodsCount", "/diamond/**").permitAll()
.requestMatchers("/upload/**").anonymous()
.requestMatchers("/common/download**").anonymous()
.requestMatchers("/doc.html").anonymous()

Loading…
Cancel
Save