Authored by Gino Zhang

suggestTips需要排除原始输入的关键词

@@ -18,10 +18,7 @@ import com.yoho.search.service.vo.SearchApiResult; @@ -18,10 +18,7 @@ import com.yoho.search.service.vo.SearchApiResult;
18 import com.yoho.search.service.vo.SuggestApiResult; 18 import com.yoho.search.service.vo.SuggestApiResult;
19 import org.apache.commons.collections.CollectionUtils; 19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.lang.StringUtils; 20 import org.apache.commons.lang.StringUtils;
21 -import org.elasticsearch.index.query.MatchQueryBuilder;  
22 -import org.elasticsearch.index.query.MultiMatchQueryBuilder;  
23 -import org.elasticsearch.index.query.QueryBuilder;  
24 -import org.elasticsearch.index.query.QueryBuilders; 21 +import org.elasticsearch.index.query.*;
25 import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; 22 import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
26 import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; 23 import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
27 import org.elasticsearch.search.sort.SortBuilder; 24 import org.elasticsearch.search.sort.SortBuilder;
@@ -267,7 +264,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -267,7 +264,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
267 // 2) 根据terms搜索构造搜索请求 264 // 2) 根据terms搜索构造搜索请求
268 final String countField = getCountField(paramMap); 265 final String countField = getCountField(paramMap);
269 SearchParam searchParam = new SearchParam(); 266 SearchParam searchParam = new SearchParam();
270 - MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(paramMap.get(SearchRequestParams.PARAM_SEARCH_KEYWORD)); 267 + MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(queryWord);
271 queryBuilder.field("keyword").field("keyword.keyword_ik", 10F).field("keyword.keyword_pinyin").field("keyword.keyword_jianpin") 268 queryBuilder.field("keyword").field("keyword.keyword_ik", 10F).field("keyword.keyword_pinyin").field("keyword.keyword_jianpin")
272 .analyzer("ik_smart") 269 .analyzer("ik_smart")
273 .type(MultiMatchQueryBuilder.Type.BEST_FIELDS) 270 .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)
@@ -277,7 +274,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -277,7 +274,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
277 searchParam.setQuery(queryBuilder); 274 searchParam.setQuery(queryBuilder);
278 searchParam.setPage(1); 275 searchParam.setPage(1);
279 searchParam.setSize(SMART_SUGGESTION_TERM_COUNT); 276 searchParam.setSize(SMART_SUGGESTION_TERM_COUNT);
280 - searchParam.setFiter(QueryBuilders.rangeQuery(countField).gte(SMART_SUGGESTION_COUNT_LIMIT)); 277 +
  278 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  279 + boolFilter.must(QueryBuilders.rangeQuery(countField).gte(SMART_SUGGESTION_COUNT_LIMIT));
  280 + boolFilter.mustNot(QueryBuilders.termQuery("keyword.keyword_lowercase", queryWord.trim().toLowerCase()));
  281 + searchParam.setFiter(boolFilter);
281 282
282 // 3) 先从缓存中获取 283 // 3) 先从缓存中获取
283 final String indexName = ISearchConstants.INDEX_NAME_SUGGEST; 284 final String indexName = ISearchConstants.INDEX_NAME_SUGGEST;
@@ -326,6 +327,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -326,6 +327,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
326 final String countFiled = getCountField(paramMap); 327 final String countFiled = getCountField(paramMap);
327 SearchParam searchParam = new SearchParam(); 328 SearchParam searchParam = new SearchParam();
328 List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList()); 329 List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList());
  330 + keywordsInDest.remove(queryWord);
329 331
330 // 4.2) 设置keyword列表 332 // 4.2) 设置keyword列表
331 QueryBuilder queryBuilder = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest); 333 QueryBuilder queryBuilder = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest);