# Custom Aggregations in ES

**URL:** <https://discuss.elastic.co/t/custom-aggregations-in-es/311385>\
**Category:** Elasticsearch\
**Created:** [August 4, 2022, 8:38am UTC](https://discuss.elastic.co/t/custom-aggregations-in-es/311385 "2022-08-04T08:38:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnu\_teja](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishnu_teja/32/103949_2.png) [@vishnu\_teja](https://discuss.elastic.co/u/vishnu_teja)\
**Post date:** [August 4, 2022, 8:38am UTC](https://discuss.elastic.co/t/custom-aggregations-in-es/311385/1 "2022-08-04T08:38:07Z")

</div>

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:

```auto
{
  {
     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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![79g](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/79g/32/109191_2.png) [@79g](https://discuss.elastic.co/u/79g)\
**Post date:** [August 4, 2022, 8:50am UTC](https://discuss.elastic.co/t/custom-aggregations-in-es/311385/2 "2022-08-04T08:50:26Z")

</div>

Hello,

please could you check [Sum by quantity and sort by lowest price - #2 by 79g](https://discuss.elastic.co/t/sum-by-quantity-and-sort-by-lowest-price/311242/2) ?

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

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

```

Hope it helps!

---

<div class="post-metadata">

**Author:** ![vishnu\_teja](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishnu_teja/32/103949_2.png) [@vishnu\_teja](https://discuss.elastic.co/u/vishnu_teja)\
**Post date:** [August 4, 2022, 9:08am UTC](https://discuss.elastic.co/t/custom-aggregations-in-es/311385/3 "2022-08-04T09:08:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![79g](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/79g/32/109191_2.png) [@79g](https://discuss.elastic.co/u/79g)\
**Post date:** [August 4, 2022, 9:18am UTC](https://discuss.elastic.co/t/custom-aggregations-in-es/311385/4 "2022-08-04T09:18:31Z")

</div>

For that you could use [Top Hit aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) for example.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 1, 2022, 9:19am UTC](https://discuss.elastic.co/t/custom-aggregations-in-es/311385/5 "2022-09-01T09:19:23Z")

</div>

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