diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java index 6b46fe3..13aff12 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java @@ -96,6 +96,7 @@ public class GoodsController extends BaseController { elasticEntity.setId(goods.getId().toString()); map.put("id", goods.getId()); map.put("name", goods.getName()); + map.put("pyname", goods.getName()); map.put("sales", goods.getActualSales() + goods.getVirtualSales()); map.put("isHot", goods.getIsHot()); map.put("isNew", goods.getIsNew()); diff --git a/waynboot-admin-api/src/main/resources/es/index/goods.json b/waynboot-admin-api/src/main/resources/es/index/goods.json index e2a1ec0..4466417 100644 --- a/waynboot-admin-api/src/main/resources/es/index/goods.json +++ b/waynboot-admin-api/src/main/resources/es/index/goods.json @@ -7,12 +7,20 @@ "type": "text", "analyzer": "ik_max_word", "fields": { - "suggest": { + "kw": { "type": "completion", - "analyzer": "ik_max_word" + "analyzer": "keyword" + }, + "py": { + "type": "completion", + "analyzer": "pinyin" } } }, + "pyname": { + "type": "text", + "analyzer": "pinyin" + }, "sales": { "type": "integer" }, diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java index 6d16357..593571f 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java @@ -65,7 +65,7 @@ public class SearchController extends BaseController { @GetMapping("sugguest") public R sugguest(SearchVO searchVO) throws IOException { String keyword = searchVO.getKeyword(); - String suggestField = "name.suggest"; + String suggestField = "name.py"; String suggestName = "my-suggest"; SuggestionBuilder termSuggestionBuilder = SuggestBuilders.completionSuggestion(suggestField) .prefix(keyword) @@ -89,20 +89,19 @@ public class SearchController extends BaseController { Boolean isPrice = searchVO.getIsPrice(); Boolean isSales = searchVO.getIsSales(); String orderBy = searchVO.getOrderBy(); - SearchHistory searchHistory = new SearchHistory(); - if (memberId != null && StringUtils.isNotEmpty(keyword)) { - searchHistory.setCreateTime(LocalDateTime.now()); - searchHistory.setUserId(memberId); - searchHistory.setKeyword(keyword); - } + Page page = getPage(); // 查询包含关键字、已上架商品 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); MatchQueryBuilder matchFiler = QueryBuilders.matchQuery("isOnSale", true); MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", keyword); + MatchQueryBuilder pymatchQuery = QueryBuilders.matchQuery("pyname", keyword); MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("keyword", keyword); - boolQueryBuilder.filter(matchFiler).should(matchQuery).should(matchPhraseQueryBuilder).minimumShouldMatch(1); + boolQueryBuilder.filter(matchFiler).should(matchQuery) + .should(pymatchQuery) + .should(matchPhraseQueryBuilder) + .minimumShouldMatch(1); searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS)); // 按是否新品排序 if (isNew) { @@ -153,8 +152,14 @@ public class SearchController extends BaseController { AsyncManager.me().execute(new TimerTask() { @Override public void run() { - searchHistory.setHasGoods(true); - iSearchHistoryService.save(searchHistory); + SearchHistory searchHistory = new SearchHistory(); + if (memberId != null && StringUtils.isNotEmpty(keyword)) { + searchHistory.setCreateTime(LocalDateTime.now()); + searchHistory.setUserId(memberId); + searchHistory.setKeyword(keyword); + searchHistory.setHasGoods(true); + iSearchHistoryService.save(searchHistory); + } } }); }