Authored by wangnan

修改suggest逻辑,用wildcard

@@ -84,34 +84,39 @@ public class SearchServiceHelper { @@ -84,34 +84,39 @@ public class SearchServiceHelper {
84 return QueryBuilders.matchAllQuery(); 84 return QueryBuilders.matchAllQuery();
85 } 85 }
86 86
87 - //构建MultiMatch  
88 - MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword); 87 + if (isSuggest) {
  88 + WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("suggest", "*" + keyword + "*");
  89 + return wildcardQueryBuilder;
  90 + } else {
  91 + //构建MultiMatch
  92 + MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
89 93
90 - //获取MultiMatchQueryType  
91 - MultiMatchQueryBuilder.Type multiMatchQueryBuilderType = this.getMultiMatchQueryBuilderType();  
92 - if (multiMatchQueryBuilderType != null) {  
93 - queryBuilder.type(multiMatchQueryBuilderType);  
94 - } 94 + //获取MultiMatchQueryType
  95 + MultiMatchQueryBuilder.Type multiMatchQueryBuilderType = this.getMultiMatchQueryBuilderType();
  96 + if (multiMatchQueryBuilderType != null) {
  97 + queryBuilder.type(multiMatchQueryBuilderType);
  98 + }
95 99
96 - if (paramMap.containsKey("queryField") && StringUtils.isNotBlank(paramMap.get("queryField"))) {  
97 - String[] fields = paramMap.get("queryField").split(",");  
98 - for (int i = 0; i < fields.length; i++) {  
99 - queryBuilder.field(fields[i]); 100 + if (paramMap.containsKey("queryField") && StringUtils.isNotBlank(paramMap.get("queryField"))) {
  101 + String[] fields = paramMap.get("queryField").split(",");
  102 + for (int i = 0; i < fields.length; i++) {
  103 + queryBuilder.field(fields[i]);
  104 + }
  105 + } else {
  106 + //设置searchfield和权重
  107 + this.setDefaultSearchField(queryBuilder, indexName);
100 } 108 }
101 - } else {  
102 - //设置searchfield和权重  
103 - this.setDefaultSearchField(queryBuilder, indexName, isSuggest);  
104 - }  
105 109
106 - //设置Operator  
107 - if (ISearchConstans.SEARCH_OPERATOR.equalsIgnoreCase("or")) {  
108 - queryBuilder.operator(MatchQueryBuilder.Operator.OR);  
109 - queryBuilder.minimumShouldMatch(ISearchConstans.SEARCH_MINIMUM_SHOULD_MATCH);  
110 - } else {  
111 - queryBuilder.operator(MatchQueryBuilder.Operator.AND); 110 + //设置Operator
  111 + if (ISearchConstans.SEARCH_OPERATOR.equalsIgnoreCase("or")) {
  112 + queryBuilder.operator(MatchQueryBuilder.Operator.OR);
  113 + queryBuilder.minimumShouldMatch(ISearchConstans.SEARCH_MINIMUM_SHOULD_MATCH);
  114 + } else {
  115 + queryBuilder.operator(MatchQueryBuilder.Operator.AND);
112 116
  117 + }
  118 + return queryBuilder;
113 } 119 }
114 - return queryBuilder;  
115 } 120 }
116 121
117 122
@@ -223,17 +228,18 @@ public class SearchServiceHelper { @@ -223,17 +228,18 @@ public class SearchServiceHelper {
223 * 228 *
224 * @param queryBuilder 229 * @param queryBuilder
225 */ 230 */
226 - private void setDefaultSearchField(MultiMatchQueryBuilder queryBuilder, String indexName, boolean isSuggest) { 231 + private void setDefaultSearchField(MultiMatchQueryBuilder queryBuilder, String indexName) {
227 List<String> fields = new ArrayList<>(); 232 List<String> fields = new ArrayList<>();
228 - if (isSuggest) {  
229 - fields = ISearchConstans.SEARCH_SUGGUST_FIELD;  
230 - } else if (indexName.equals(ISearchConstans.INDEX_NAME_BIZAREA)) { 233 + if (indexName.equals(ISearchConstans.INDEX_NAME_BIZAREA)) {
231 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_BIZAREA; 234 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_BIZAREA;
232 - } else if (indexName.equals(ISearchConstans.INDEX_NAME_COMMENT)) { 235 + }
  236 + if (indexName.equals(ISearchConstans.INDEX_NAME_COMMENT)) {
233 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_COMMENT; 237 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_COMMENT;
234 - } else if (indexName.equals(ISearchConstans.INDEX_NAME_LINE)) { 238 + }
  239 + if (indexName.equals(ISearchConstans.INDEX_NAME_LINE)) {
235 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_LINE; 240 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_LINE;
236 - } else if (indexName.equals(ISearchConstans.INDEX_NAME_STORE)) { 241 + }
  242 + if (indexName.equals(ISearchConstans.INDEX_NAME_STORE)) {
237 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_STORE; 243 fields = ISearchConstans.SEARCH_DEFAULT_FIELD_STORE;
238 } 244 }
239 if (indexName.equals(ISearchConstans.INDEX_NAME_TOPIC)) { 245 if (indexName.equals(ISearchConstans.INDEX_NAME_TOPIC)) {