user626528
user626528

Reputation: 14419

How to do "group by / max" query?

I need to group all records by a value of Field1, and calculate max value of Field2 for every group. So, is there any way to make max aggregation work over multiple groups within the same query?

Upvotes: 5

Views: 7659

Answers (1)

Jettro Coenradie
Jettro Coenradie

Reputation: 4733

GET /yourindex/_search?search_type=count
{
  "aggs": {
    "yourGroup": {
      "terms": {
        "field": "field1",
        "size": 10
      },
      "aggs": {
        "theMax": {
          "max": {
            "field": "field2"
          }
        }
      }
    }
  }
}

Upvotes: 15

Related Questions