Authored by Gino Zhang

搜索处理suggest_conversion增加排序处理

@@ -21,6 +21,8 @@ import org.elasticsearch.index.query.MatchQueryBuilder; @@ -21,6 +21,8 @@ import org.elasticsearch.index.query.MatchQueryBuilder;
21 import org.elasticsearch.index.query.MultiMatchQueryBuilder; 21 import org.elasticsearch.index.query.MultiMatchQueryBuilder;
22 import org.elasticsearch.index.query.QueryBuilder; 22 import org.elasticsearch.index.query.QueryBuilder;
23 import org.elasticsearch.index.query.QueryBuilders; 23 import org.elasticsearch.index.query.QueryBuilders;
  24 +import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
  25 +import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
24 import org.elasticsearch.search.sort.SortBuilder; 26 import org.elasticsearch.search.sort.SortBuilder;
25 import org.elasticsearch.search.sort.SortBuilders; 27 import org.elasticsearch.search.sort.SortBuilders;
26 import org.elasticsearch.search.sort.SortOrder; 28 import org.elasticsearch.search.sort.SortOrder;
@@ -293,14 +295,22 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -293,14 +295,22 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
293 return new ArrayList<>(); 295 return new ArrayList<>();
294 } 296 }
295 297
  298 + final String countFiled = getCountField(paramMap);
296 SearchParam searchParam = new SearchParam(); 299 SearchParam searchParam = new SearchParam();
297 List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList()); 300 List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList());
298 - QueryBuilder query = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest);  
299 - searchParam.setQuery(query); 301 + QueryBuilder queryBuilder = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest);
  302 + FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
  303 + functionScoreQueryBuilder.add(QueryBuilders.termQuery("type",2), ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size()));
  304 + for (int index = 0; index < keywordsInDest.size(); index++) {
  305 + // 根据顺序从高到低加分
  306 + functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_lowercase", keywordsInDest.get(index)),
  307 + ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size() - index));
  308 + }
  309 +
  310 + searchParam.setQuery(functionScoreQueryBuilder);
300 searchParam.setPage(1); 311 searchParam.setPage(1);
301 searchParam.setSize(SMART_SUGGESTION_TERM_COUNT); 312 searchParam.setSize(SMART_SUGGESTION_TERM_COUNT);
302 - searchParam.setFiter(QueryBuilders.rangeQuery(getCountField(paramMap)).gte(20));  
303 - searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort("weight").order(SortOrder.DESC))); 313 + searchParam.setFiter(QueryBuilders.rangeQuery(countFiled).gte(20));
304 314
305 final String indexName = ISearchConstants.INDEX_NAME_SUGGEST; 315 final String indexName = ISearchConstants.INDEX_NAME_SUGGEST;
306 JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(indexName, searchParam); 316 JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(indexName, searchParam);
@@ -310,7 +320,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -310,7 +320,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
310 } 320 }
311 321
312 SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam); 322 SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
313 - if (searchResult == null || CollectionUtils.isEmpty(searchResult.getResultList())) { 323 + if (searchResult == null) {
314 return new ArrayList<>(); 324 return new ArrayList<>();
315 } 325 }
316 326