ES根据得分数聚合去重

1.es原型

{
	"query": {
		"bool": {
			"should": [{
				"match": {
					"titleSegs": {
						"query": "相同",
						"operator": "OR",
						"prefix_length": 0,
						"max_expansions": 50,
						"fuzzy_transpositions": true,
						"lenient": false,
						"zero_terms_query": "NONE",
						"auto_generate_synonyms_phrase_query": true,
						"boost": 1.0
					}
				}
			},  {
				"constant_score": {
					"filter": {
						"match": {
							"titleFirstPYSuggest.suffix": {
								"query": "xt",
								"operator": "OR",
								"prefix_length": 0,
								"max_expansions": 50,
								"fuzzy_transpositions": true,
								"lenient": false,
								"zero_terms_query": "NONE",
								"auto_generate_synonyms_phrase_query": true,
								"boost": 1.0
							}
						}
					},
					"boost": 20.0
				}
			}],
			"adjust_pure_negative": true,
			"boost": 10.0
		}
	},
	"aggregations": {
		"top_title_hits": {
			"terms": {
				"field": "title",
				"size": 10,
				"min_doc_count": 1,
				"shard_min_doc_count": 0,
				"show_term_doc_count_error": false,
				"order": [{
					"maxSource": "desc"
				}, {
					"_key": "asc"
				}]
			},
			"aggregations": {
				"maxSource": {
					"max": {
						"script": {
							"source": "_score",
							"lang": "painless"
						}
					}
				},
				"top_title_hits": {
					"top_hits": {
						"from": 0,
						"size": 10,
						"version": false,
						"explain": false
					}
				}
			}
		}
	}
}

2.Java客户端

AggregationBuilder aggregation = 
                AggregationBuilders.terms(topHitsAggregationName)
                .field("title")
                .minDocCount(1).size(resultNum)
                .order(BucketOrder.aggregation("maxSource", false));
        TopHitsAggregationBuilder topHitsAggregation = 
                        
                             AggregationBuilders.topHits(topHitsAggregationName)
                             .size(resultNum);
       // topHitsAggregation.fetchSource("title", null);
// 自定义
        aggregation.subAggregation(AggregationBuilders.max("maxSource")
.script( new Script("_score")));
        aggregation.subAggregation(topHitsAggregation);

        searchSourceBuilder.aggregation(aggregation);

        SearchRequest searchRequest = new SearchRequest()
                .indices(indexName).source(searchSourceBuilder)
                .searchType(SearchType.DFS_QUERY_THEN_FETCH);

3.展示搜索结果

ParsedStringTerms parsedStringTerms = searchResponse.getAggregations().get(topHitsAggregationName);
       for (Terms.Bucket bucket : parsedStringTerms.getBuckets()) {
            ParsedTopHits parsedTopHits = bucket.getAggregations().get(topHitsAggregationName);
           JSONObject jsonObject = null;
           AutoCompleteResultVO vo = new AutoCompleteResultVO();
           if (parsedTopHits != null) {
                SearchHit topSearchHit = parsedTopHits.getHits().getHits()[0];
               jsonObject = JSONObject.parseObject(topSearchHit.getSourceAsString());
                jsonObject.put("_esScore", topSearchHit.getScore());
            } else {
              jsonObject = new JSONObject();
          }

          if (jsonObject.containsKey("title")) {
              vo.setName(jsonObject.getString("title"));
          }
         
        }
经验分享 程序员 微信小程序 职场和发展