【问题标题】:ElasticSearch - Ordering aggregation by nested aggregation on nested fieldElasticSearch - 通过嵌套字段上的嵌套聚合排序聚合
【发布时间】:2017-09-13 01:58:30
【问题描述】:
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 0,
  "aggs": {
    "itineraryId": {
      "terms": {
        "field": "iid",
        "size": 2147483647,
        "order": [
          {
            "price>price>price.max": "desc"
          }
        ]
      },
      "aggs": {
        "duration": {
          "stats": {
            "field": "drn"
          }
        },
        "price": {
          "nested": {
            "path": "prl"
          },
          "aggs": {
            "price": {
              "filter": {
                "terms": {
                  "prl.cc.keyword": [
                    "USD"
                  ]
                }
              },
              "aggs": {
                "price": {
                  "stats": {
                    "field": "prl.spl.vl"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

在这里,我得到了错误:

“无效条款聚合订单路径[价格>价格>价格.max]。条款 存储桶只能在构建的子聚合器路径上排序 路径内的零个或多个单桶聚合和最终 单桶或路径末端的指标聚合。子路径 [price] 指向非单桶聚合"

如果我按持续时间聚合排序,查询工作正常

"order": [
      {
        "duration.max": "desc"
      }

那么有没有办法通过嵌套字段上的嵌套聚合来订购聚合,例如下面的内容?

"order": [
      {
        "price>price>price.max": "desc"
      }

【问题讨论】:

  • 目前尚不支持,但刚刚提交了社区拉取请求以解决此问题:github.com/elastic/elasticsearch/pull/24121 原始问题:github.com/elastic/elasticsearch/issues/16838
  • @Val 你知道其他解决方法吗?
  • 是的,将最高价格添加为根级别的新字段。
  • 那么你可以有一个max_price 对象,每个货币一个价格"max_price": {"USD": 1000, "EUR": 1210, ...}
  • 否,因为每种货币只有一个值(始终是任何嵌套货币的最高价格)。然后,您可以在 max_price.USD 上添加另一个 max 子聚合,并使用它对 iid 术语进行排序。

标签: elasticsearch elasticsearch-5


【解决方案1】:

正如 Val 在 cmets 中指出的那样,ES 还不支持它。

在此之前,您可以先聚合嵌套聚合,然后使用反向嵌套聚合来聚合持续时间,即存在于文档根目录中。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html

【讨论】:

  • @J Aamish - 我认为这对于嵌套字段价格是不可能的。在持续时间字段上,排序已经按照问题中的说明进行。
猜你喜欢
  • 2015-07-30
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2015-11-18
  • 1970-01-01
  • 2018-01-30
  • 2014-12-31
相关资源
最近更新 更多