Custom Aggregations in ES

Hi,
I would like to know, if we can have custom aggregations in ElasticSearch, For EX: we have a use-case where we need a function like any_value(), it works like this:

Say I have the following documents in my index:

{
  {
     name: "sachin",
     sport: "cricket",
     score: 10
  },
  {
     name: "sachin",
     sport: "football"
     score: 100
  },
  {
     name: "dravid",
     sport: "golf"
     score: 100
  },
  {
     name: "dravid",
     sport: "baseball"
     score: 50
  }
}

and i need a aggregations query, which groups by name, gets the average of score, and pick any of the of the sport, so the response should be:

Response:
[ {"aggName": "sachin", "aggSport": "cricket", "aggScore": 55},
  {"aggName": "dravid", "aggSport": "baseball", "aggScore": 75}]

I know that ES support aggregations like avg, sum, max, min etc on double, int etc, does it support implementations like any_string() (picks any string), first_string() (picks the first string hit) etc.

Is there a way we can achieve the functionality?

Thanks.

Hello,

please could you check Sum by quantity and sort by lowest price - #2 by 79g ?

The query is almost the same, with an average as agg

"aggs": {
    "aggScore": {
      "avg": {
        "field": "score"
      }
    }
}

Hope it helps!

I Know that average works, i need a aggregation on the sport field which picks up any of the fields, like for sachin it can be either cricket or football.

For that you could use Top Hit aggregation for example.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.