Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed more than 2 aggregations in elastic?

I'm making an application that allows users to create reports on customers data, the user is able to add 0 or more grouping fields.

I'm using elastic 2.2.0, so if the user wants to group by day, then by age and then by sex the program sends the following query to elastic:

{
  "aggs": {
    "group_a": {
      "date_histogram": {
        "field": "transactionDate",
        "interval": "day"
      },
      "aggs": {
        "group_b": {
          "terms": {
            "field": "age"
          }
        },
        "aggs": {
          "group_c": {
            "terms": {
              "field": "sex"
            }
          }
        }
      }
    }
  }
}

I get this error:

{
  "error": {
    "root_cause": [
      {
        "type": "search_parse_exception",
        "reason": "Could not find aggregator type [group_c] in [aggs]",
        "line": 15,
        "col": 11
      }
    ]
  }
}

How can I fix this?

like image 437
johnblanco Avatar asked Mar 04 '16 16:03

johnblanco


1 Answers

If you want to nest group_c into group_b, you should use this json:

{
  "aggs": {
    "group_a": {
      "date_histogram": {
        "field": "transactionDate",
        "interval": "day"
      },
      "aggs": {
        "group_b": {
          "terms": {
            "field": "age"
          },
          "aggs": {
            "group_c": {
              "terms": {
                "field": "sex"
              }
            }
          }
        }
      }
    }
  }
}

Note that you were defining group_c outside of group_b, that was the error.

like image 187
fps Avatar answered Oct 14 '22 17:10

fps