ElasticSearch的多个度量子聚合情况

时间:2014-07-24 21:20:04

标签: elasticsearch elasticsearch-aggregation

我知道Elasticsearch支持使用分段的子聚合(其中分段聚合可以具有分段或度量子聚合)。使用度量标准聚合无法进行子聚合。可能这是有道理的,但这是用例。

我有term aggregation作为父母。并使用另一个term聚合作为它的子级。子term具有top_hits类型的子聚合。 top_hits是度量标准聚合,因此无法进行任何子聚合。现在需要将avg聚合包括在内。给定top_hits是聚合树中的最后一个聚合,因为avg是一个度量聚合,所以不能将top_hits作为子节点。

以下是所需的聚合级别。 (当然,它是无效的,因为top_hits是一个度量聚合,对avg聚合也是如此。

{
  "aggregations": {
    "top_makes": {
      "terms": {
        "field": "make"
      },
      "aggregations": {
        "top_models": {
          "terms": {
            "field": "model"
          },
          "aggregations": {
            "top_res": {
              "top_hits": {
                "_source": {
                  "include": [
                    "model",
                    "color"
                  ]
                },
                "size": 10
              }
            }
          }
        }
      },
      "aggregations": {
        "avg_length": {
          "avg": {
            "field": "vlength"
          }
        }
      }
    }
  }
}

解决此问题的解决方法或解决方法是什么?

1 个答案:

答案 0 :(得分:2)

我认为这会有效,验证..

{
  "aggregations": {
    "top_makes": {
      "terms": {
        "field": "make"
      },
      "aggregations": {
        "top_models": {
          "terms": {
            "field": "model"
          },
          "aggregations": {
            "top_res": {
              "top_hits": {
                "_source": {
                  "include": [
                    "model",
                    "color"
                  ]
                },
                "size": 10
              }
            }
          },
          "avg_length": {
            "avg": {
              "field": "vlength"
            }
          }
        }
      }
    }
  }
}

关键是您可以为父聚合提供一个或多个sibbling(子聚合)。