gpt4 book ai didi

Elasticsearch 聚合 : How to Sort Bucket Order

转载 作者:行者123 更新时间:2023-11-29 02:55:41 24 4
gpt4 key购买 nike

ES 版本:1.5(Amazon Elasticsearch)

我的目标:在某个字段上进行重复数据删除的搜索结果。我目前正在对处理重复数据删除的聚合进行一些研究。因此,我的结果是一个包含 1 大小桶的列表桶。但是,我找不到对存储桶列表进行排序的方法。

当前查询:

curl -XGET "http://localhost:9200/myidx/product/_search?search_type=count" -d '{
"size": 2,
"query": {
"function_score": {
"field_value_factor": {
"field": "relevance",
"factor": 2.0
},
"query": { "term": { "title": "abcd" } },
"score_mode": "multiply",
"boost_mode": "multiply"
}
},
"aggs": {
"unique": {
"terms": {
"field": "groupid",
"size": 2
},
"aggs": {
"sample": {
"top_hits": {
"size": 1
}
}
}
}
}
}'

结果:

{ ...
"aggregations": {
"unique": {
"doc_count_error_upper_bound": 1,
"sum_other_doc_count": 39,
"buckets": [
{
"key": 717878424,
"doc_count": 14,
"sample": {
"hits": {
"total": 14,
"max_score": 45.856163,
"hits": [
{
"_index": "myidx",
"_type": "product",
"_id": "89531",
"_score": 45.856163,
"_source": { ... }
}
]
}
}
},
{
"key": 717878423,
"doc_count": 8,
"sample": {
"hits": {
"total": 8,
"max_score": 68.78424,
"hits": [
{
"_index": "myidx",
"_type": "product",
"_id": "89517",
"_score": 68.78424,
"_source": { ... }
}
]
}
}
}
]
}
}
}

我希望将 max_score=68.78424 的第二个桶作为第一个桶。这可能吗?

如果聚合不是推荐的解决方案,请告知。

最佳答案

是的,您可以通过在最大文档分数上添加另一个子聚合并按该分数对 unique terms 聚合进行排序来实现。

curl -XGET "http://localhost:9200/myidx/product/_search?search_type=count" -d '{
"size": 2,
"query": {
"function_score": {
"field_value_factor": {
"field": "relevance",
"factor": 2.0
},
"query": { "term": { "title": "abcd" } },
"score_mode": "multiply",
"boost_mode": "multiply"
}
},
"aggs": {
"unique": {
"terms": {
"field": "groupid",
"size": 2,
"order": {
"max_score": "desc"
}
},
"aggs": {
"max_score": {
"max": {
"script": "doc.score"
}
},
"sample": {
"top_hits": {
"size": 1
}
}
}
}
}
}'

关于 Elasticsearch 聚合 : How to Sort Bucket Order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34195346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com