Elasticsearch聚合。按嵌套存储桶doc_count排序 [英] Elasticsearch aggregation. Order by nested bucket doc_count

查看:567
本文介绍了Elasticsearch聚合。按嵌套存储桶doc_count排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的是通过唯一对(城市,州)的聚合。根据Elasticsearch文档术语聚合不支持从同一文档中的多个字段收集术语。因此,我创建了这样的嵌套agg:

What I want to achieve is aggregation by unique pairs (city, STATE). As per Elasticsearch documentation The terms aggregation does not support collecting terms from multiple fields in the same document. Thus I created a nested agg like this:

{
  "size": 0,
  "aggs": {
    "cities": {
      "terms": {
        "field": "address.city",
        "size": 12
      },
      "aggs": {
        "states": {
          "terms": {
            "field": "address.stateOrProvince"
          },
          "aggs": {
            "topCity": {
              "top_hits": {
                "size": 1,
                "sort": [
                  {
                    "price.value": {
                      "order": "desc" }}]}}}}}}}}

由于这种聚合,我得到这样的响应:

As a result of this aggregation I get response like this:

{
  "aggregations": {
    "cities": {
      "buckets": [
        {
          "key": "las vegas",
          "doc_count": 5927,
          "states": {
            "buckets": [
              { "key": "nv", "doc_count": 5840 },
              { "key": "nm", "doc_count": 85 }
            ]
          }
        },
        {
          "key": "jacksonville",
          "doc_count": 5689,
          "states": {
            "buckets": [
              { "key": "fl", "doc_count": 2986 },
              { "key": "nc", "doc_count": 1962 },
              { "key": "ar", "doc_count": 290 }]}}]}}}

问题是如何通过最深的doc_count获得结果?

The question is how to get results ordered by the deepest doc_count?

预期的订购清单应如下:

Expected ordered list should be like this:


  1. 拉斯维加斯,内华达州(5840)

  2. jacksonville,fl(2986)

  3. jacksonville,nc(1962)

  4. jacksonville,ar(290)

  5. 拉斯维加斯,纳米(85)

  1. las vegas, nv (5840)
  2. jacksonville, fl (2986)
  3. jacksonville, nc (1962)
  4. jacksonville, ar (290)
  5. las vegas, nm (85)


推荐答案

我不相信有一种方法可以对多个存储桶中的内部doc_count进行排序。在ES 2.0(仍处于Beta版)中,您可以对汇总采取措施,但是在ES 1.x

I don't believe there is a way to sort on the inner doc_count accross multiple buckets. In ES 2.0 (still in Beta) you'll be able to take action on aggregations but that's not possible in ES 1.x

这篇关于Elasticsearch聚合。按嵌套存储桶doc_count排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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