Elasticsearch-针对聚合的查询字段

时间:2018-11-14 13:02:06

标签: elasticsearch elasticsearch-aggregation

我正在探索使用Elasticsearch查询和汇总数据的简便性。但是我无法通过以下单个查询来透视和汇总数据:

考虑数据:

enter image description here

是否可以查询以下结果 如下所示枢转和汇总值:

enter image description here

必填结果:

{  
   {  
      "A":a1,
      "B":b1,
      "Value":3
   },
   {  
      "A":a1,
      "B":b2,
      "Value":3
   },
   {  
      "A":a2,
      "B":b2,
      "Value":4
   },
   {  
      "A":a1,
      "B":b3,
      "Value":11
   }
}

1 个答案:

答案 0 :(得分:1)

是的,您可以为termsA嵌套两个B聚合,就像这样,您将获得预期的结果:

{
  "size": 0,
  "aggs": {
    "A": {
      "terms": {
        "field": "A"
      },
      "aggs": {
        "B": {
          "terms": {
            "field": "B"
          },
          "aggs": {
            "value_sum": {
              "sum": {
                "field": "Value1"
              }
            }
          }
        }
      }
    }
  }
}