如何在弹性中嵌入两个以上的聚合?

joh*_*nco 5 elasticsearch

我正在开发一个允许用户创建有关客户数据的报告的应用程序,该用户能够添加0个或多个分组字段。

我使用的是Elastic 2.2.0,因此,如果用户要按天分组,则按年龄分组,然后按性别分组,则程序会将以下查询发送给Elastic:

{
  "aggs": {
    "group_a": {
      "date_histogram": {
        "field": "transactionDate",
        "interval": "day"
      },
      "aggs": {
        "group_b": {
          "terms": {
            "field": "age"
          }
        },
        "aggs": {
          "group_c": {
            "terms": {
              "field": "sex"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

{
  "error": {
    "root_cause": [
      {
        "type": "search_parse_exception",
        "reason": "Could not find aggregator type [group_c] in [aggs]",
        "line": 15,
        "col": 11
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Fed*_*ner 4

如果你想嵌套group_c到 中group_b,你应该使用这个 json:

{
  "aggs": {
    "group_a": {
      "date_histogram": {
        "field": "transactionDate",
        "interval": "day"
      },
      "aggs": {
        "group_b": {
          "terms": {
            "field": "age"
          },
          "aggs": {
            "group_c": {
              "terms": {
                "field": "sex"
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您是group_c在 之外定义的group_b,这是错误的。