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"));
}
}
文章来源地址https://www.toymoban.com/news/detail-504969.html
文章来源:https://www.toymoban.com/news/detail-504969.html
到了这里,关于ES根据得分数聚合去重的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!