Elasticsearch Ordering术语聚合在最热门字段子聚合之后的存储桶中 [英] Elasticsearch Ordering terms aggregation buckets after field in top hits sub aggregation
问题描述
我想根据热门匹配中第一个元素所具有的属性,从术语聚合中排序存储桶.
I would like to order the buckets from a terms aggregation based on a property possessed by the first element in a top hits aggregation.
我的最大努力查询如下(带有语法错误):
My best effort query looks like this (with syntax errors):
{
"aggregations": {
"toBeOrdered": {
"terms": {
"field": "parent_uuid",
"size": 1000000,
"order": {
"topAnswer._source.id": "asc"
}
},
"aggregations": {
"topAnswer": {
"top_hits": {
"size": 1
}
}
}
}
}
}
有人知道如何做到这一点吗?
Does anyone know how to accomplish this?
示例:
{
"a":1,
"b":2,
"id":4
}
{
"a":1,
"b":3,
"id":1
}
{
"a":2,
"b":4,
"id":3
}
按"a"分组,按"id"(desc)对存储区进行排序,然后对"b"(desc)的热门歌曲进行排序,将得出:
Grouping by "a" and ordering the buckets by "id" (desc) and sorting the top hits on "b" (desc) would give:
{2:{
"a":2,
"b":4,
"id":3
},1:{
"a":1,
"b":3,
"id":1
}}
推荐答案
您可以使用以下查询进行操作.想法是为每个parent_uuid
存储桶显示具有最小id
值的第一个热门匹配项,并使用min
子聚合根据最小的id
值对parent_uuid
存储桶进行排序.>
You can do it with the following query. The idea is to show for each parent_uuid
bucket the first top hit with the minimum id
value and to sort the parent_uuid
buckets according the smallest id
value as well using a min
sub-aggregation.
{
"aggregations": {
"toBeOrdered": {
"terms": {
"field": "parent_uuid",
"size": 1000000,
"order": {
"topSort": "desc"
}
},
"aggregations": {
"topAnswer": {
"top_hits": {
"size": 1,
"sort": {
"b": "desc"
}
}
},
"topSort": {
"max": {
"field": "id"
}
}
}
}
}
}
尝试一下,并报告是否适合您.
Try it out and report if this works out for you.
这篇关于Elasticsearch Ordering术语聚合在最热门字段子聚合之后的存储桶中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!