akuiper
akuiper

Reputation: 215117

Calculate percentiles for groups?

How can I calculate percentiles of one field grouped by another field with Elastic Search? I have tried this:

GET /dealergeo/sale/_search
{
  "size": 0,
  "aggs": {
    "dom_rank": {
      "percentiles": {
        "field": "avgdom"
      },
      "aggs": {
        "name": {
          "terms": {
            "field": "make",
            "size": 10
          }
        }
      }
    }
  }
}

So basically I want to group all data by make and then calculate the percentiles of avgdom, but it gives an error:

{ "error": { "root_cause": [ { "type": "aggregation_initialization_exception", "reason": "Aggregator [dom_rank] of type [percentiles] cannot accept sub-aggregations" } ], "type": "aggregation_initialization_exception", "reason": "Aggregator [dom_rank] of type [percentiles] cannot accept sub-aggregations" }, "status": 500 }

What am I missing here? or is this possible with Elastic Search? Thanks for your help!

Upvotes: 1

Views: 632

Answers (1)

akuiper
akuiper

Reputation: 215117

Just figured out, I need to wrap the percentile aggregation inside the terms aggregation instead of the other way around:

GET /dealergeo/sale/_search
{
  "size": 0,
  "aggs": {
    "make_agg": {
      "terms": {
        "field": "make",
        "size": 5
    },
    "aggs": {
      "dom_rank": {
        "percentiles": {
          "field": "avgdom"
     }
    }
   }
  }
 }
}

Upvotes: 1

Related Questions