是否可以在非术语字段的术语聚合响应中对存储桶进行排序? [英] Is it possible to sort buckets in Terms aggregation response on a non-term field?

查看:60
本文介绍了是否可以在非术语字段的术语聚合响应中对存储桶进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对ElasticSearch Terms聚合的结果进行分类.以下是ElasticSearch中的索引记录之一

I need to sort the buckets in result of a ElasticSearch Terms aggregation. Below is the one of the indexed records in ElasticSearch

{"personId":"10","Salary":10000, "Age":20, "personName":"xyz"}

我在Salary字段上使用术语汇总.以下是字词汇总的ElasticSearch查询:

I am using Terms aggregation over the field Salary. Below is the Terms aggregated ElasticSearch query:

{
    "aggs" : {
        "genders" : {
            "terms" : {
                "field" : "Salary"
            }
        }
    }
}

此查询根据Salary值返回存储桶.可以使用以下查询中的顺序对这些存储桶按薪金值进行排序:

This query returns the buckets on the basis of Salary values. These buckets can be sort over the Salary value using order below query:

{
    "aggs" : {
        "genders" : {
            "terms" : {
                "field" : "gender",
                "order" : { "_term" : "asc" }
            }
        }
    }
}

但是我需要在 Age (非条款字段)的任何字段上对存储桶进行排序,有什么办法吗?

But I need to sort buckets on any the field Age (non terms field), is there any way to do it ?

推荐答案

聚合的全部目的是将文档分派"到存储桶中,每个存储桶由 terms 的声明字段定义>汇总,以您的情况为 Salary .

The whole point of aggregations is to "dispatch" the documents into buckets, each of which is defined by the declared field of the terms aggregation, in your case Salary.

您在响应中得到的存储桶不再是文档.例如,在存储区 10000 中,您将获得具有 Salary:10000 的文档数,并且存储区数将与不同的 Salary数量相同值包含在您的所有文档中(尽管默认情况下只有10个存储桶).

The buckets you get in the response are not documents anymore. For instance, in the bucket 10000, you'll get the count of documents which have Salary: 10000, and you'll have as many buckets as different Salary values there are in all your documents (by default only 10 buckets, though).

因此,由于存储桶不是文档,并且由于存储桶可以聚合具有不同 Age 值的文档,因此尚不清楚您希望如何将 Salary 存储桶设置为按年龄排序.

So, since buckets are not documents, and since a bucket can aggregate documents with different Age values, it's not clear how you'd like the Salary buckets to be sorted by Age.

也许,一种解决方法可能是在 Age 字段上添加 terms 子聚合,这样您可以获得最高的 Salary 存储桶,在此之下,您将获得 Age 个存储桶.然后,您可以根据需要对 Salary / Age 存储桶对进行排序.

Maybe, one way out of this could be to add a terms sub-aggregation on the Age field, so you get top Salary buckets and below that you get Age buckets. Then you can sort your Salary/Age bucket pairs any way you want.

这篇关于是否可以在非术语字段的术语聚合响应中对存储桶进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆