如何使用Python在Elasticsearch的对象上执行多个聚集?(How to perform

2019-09-26 06:17发布

我想在这格式我Elasticsearch数据执行日期直方图查询:

datetime,field_obj和field_obj中有三个字段: a,b,c

除了日期直方图聚集,我想找到field_obj即AVG(field_a)的平均值,AVG(field_b),AVG(field_c)也。 我想工作它是这样的:

    res = es.search(index="demo",body={"from": 0, "size": 0, "query": 
        {"match_all": {}}, "aggs": {
            "date_avg": {
                "date_histogram": {"field": "datetime","interval": "year"},
                    "aggs": {"avg_a": {"avg": {"field": "field.a"}}},
                    "aggs": {"avg_b": {"avg": {"field": "field.b"}}},
                    "aggs": {"avg_c": {"avg": {"field": "field.c"}}},
                         }}
         })  

然而,这个查询只产生field_c的平均值。 所有其他的平均值得到覆盖。

Answer 1:

好的开始! 你需要做的是这样的,它会工作:

res = es.search(index="demo",body={
  "from": 0,
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "date_avg": {
      "date_histogram": {
        "field": "datetime",
        "interval": "year"
      },
      "aggs": {
        "avg_a": {
          "avg": {
            "field": "field.a"
          }
        },
        "avg_b": {
          "avg": {
            "field": "field.b"
          }
        },
        "avg_c": {
          "avg": {
            "field": "field.c"
          }
        }
      }
    }
  }
})  


文章来源: How to perform multiple aggregation on an object in Elasticsearch using Python?