# How to get Average from AvgAggregationBuilder elastic search for spesific keys

**URL:** <https://discuss.elastic.co/t/how-to-get-average-from-avgaggregationbuilder-elastic-search-for-spesific-keys/223921>\
**Category:** Elasticsearch\
**Created:** [March 17, 2020, 12:30pm UTC](https://discuss.elastic.co/t/how-to-get-average-from-avgaggregationbuilder-elastic-search-for-spesific-keys/223921 "2020-03-17T12:30:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barak\_Kedem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/barak_kedem/32/46413_2.png) [@Barak\_Kedem](https://discuss.elastic.co/u/Barak_Kedem)\
**Post date:** [March 17, 2020, 12:30pm UTC](https://discuss.elastic.co/t/how-to-get-average-from-avgaggregationbuilder-elastic-search-for-spesific-keys/223921/1 "2020-03-17T12:30:09Z")

</div>

I'm am trying to get results from [Elastic search](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-aggs.html)  
with spesic keys example:

> average salary, by city, gender

so i check at [metrics\_aggregations](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html)

and I was able

```auto
AvgAggregationBuilder totalDuration = AggregationBuilders.avg("salaryAvg").field("salary");

```

it did return the most high level - average salary without specific fields

So I tried using **subAggregation**

```
AvgAggregationBuilder totalDuration = AggregationBuilders
.avg("salaryAvg").field("salary")
 .subAggregation(AggregationBuilders.terms("city").field("gender"));

```

but i got below error

` Server Error. 21525\nElasticsearchStatusException[Elasticsearch exception [type=aggregation_initialization_exception, reason=Aggregator [salaryAvg] of type [avg] cannot accept sub-aggregations]]\n\tat org.elasticsearch.rest.BytesRestResponse.errorFromXConten`

how can I query it with Elasticsearch what I want is something quite similar to SQL - group by

each row of my elastic response data seems like that:

`widgetResponse.getHits().getHits()[0].getSourceAsMap().get("attributes")`

````
{HashMap$Node@53898} "city" -> "new York"
{HashMap$Node@53899} "pageId" -> "123"
{HashMap$Node@53900} "salary" -> "45765"
{HashMap$Node@53900} "gender" -> "female"
{HashMap$Node@53901} "userId" -> "324"
{HashMap$Node@53902} "accountId" -> "456"

widgetResponse.getHits().getHits()[1].getSourceAsMap().get("attributes")

{HashMap$Node@53898} "city" -> "new Jersy"
{HashMap$Node@53899} "pageId" -> "123"
{HashMap$Node@53899} "salary" -> "77777"
{HashMap$Node@53900} "gender" -> "female"
{HashMap$Node@53901} "userId" -> "334"
{HashMap$Node@53902} "accountId" -> "999"
```

````

How can I support it using Elastic search JAVA API?

---

<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:** [April 14, 2020, 12:30pm UTC](https://discuss.elastic.co/t/how-to-get-average-from-avgaggregationbuilder-elastic-search-for-spesific-keys/223921/2 "2020-04-14T12:30:11Z")

</div>

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