# Aggregation with average value in java api

**URL:** <https://discuss.elastic.co/t/aggregation-with-average-value-in-java-api/17699>\
**Category:** Elasticsearch\
**Created:** [May 23, 2014, 5:30pm UTC](https://discuss.elastic.co/t/aggregation-with-average-value-in-java-api/17699 "2014-05-23T17:30:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [May 23, 2014, 5:30pm UTC](https://discuss.elastic.co/t/aggregation-with-average-value-in-java-api/17699/1 "2014-05-23T17:30:31Z")

</div>

Hi,

I'm trying to do aggregation using java api. But couldn't get the  
_SearchResponse_ correctly. Getting below error. I've written method like  
below using XContentBuilder.  
Couldn't get any working scenarios from google. Please suggest any working  
example for aggregation of single value.

- public static Aggregations searchAggregation() throws IOException {\*

- Client client = ESClientFactory.getInstance();\*

- XContentBuilder contentBuilder = XContentFactory.jsonBuilder()\*

- .startObject("aggs").startObject("memory\_average")\*

- .startObject("avg").field("field", "MEMORY").endObject()\*

- .endObject().endObject();\*

- System.out.println(contentBuilder.string());\*

- SearchResponse response = client.prepareSearch("virtualmachines")\*

- .setTypes("nodes").setQuery(QueryBuilders.matchAllQuery())\*

- .setAggregations(contentBuilder).execute().actionGet();\*

- client.close();\*

- System.out.println(response);\*

- return response.getAggregations();\*

- }\*

_Error:_

Exception in thread "main"  
_org.elasticsearch.transport.TransportSerializationException_: Failed to  
deserialize exception response from stream

```
  at 

```

org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(  
_MessageChannelHandler.java:169_)

```
  at 

```

org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(  
_MessageChannelHandler.java:123_)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 23, 2014, 9:48pm UTC](https://discuss.elastic.co/t/aggregation-with-average-value-in-java-api/17699/2 "2014-05-23T21:48:52Z")

</div>

Hi,

You should be able to fix your problem by replacing the call to  
setAggregations with:

.addAggregation(AggregationBuilders.avg("memory\_average").field("MEMORY"))

On Fri, May 23, 2014 at 7:30 PM, Subhadip Bagui [i.bagui@gmail.com](mailto:i.bagui@gmail.com) wrote:

> Hi,
> 
> I'm trying to do aggregation using java api. But couldn't get the  
> _SearchResponse_ correctly. Getting below error. I've written method like  
> below using XContentBuilder.  
> Couldn't get any working scenarios from google. Please suggest any working  
> example for aggregation of single value.
> 
> - public static Aggregations searchAggregation() throws IOException {\*
> 
> - Client client = ESClientFactory.getInstance();\*
> 
> - XContentBuilder contentBuilder = XContentFactory.jsonBuilder()\*
> 
> - .startObject("aggs").startObject("memory\_average")\*
> 
> - .startObject("avg").field("field", "MEMORY").endObject()\*
> 
> - .endObject().endObject();\*
> 
> - System.out.println(contentBuilder.string());\*
> 
> - SearchResponse response = client.prepareSearch("virtualmachines")\*
> 
> - .setTypes("nodes").setQuery(QueryBuilders.matchAllQuery())\*
> 
> - .setAggregations(contentBuilder).execute().actionGet();\*
> 
> - client.close();\*
> 
> - System.out.println(response);\*
> 
> - return response.getAggregations();\*
> 
> - }\*
> 
> _Error:_
> 
> Exception in thread "main"  
> _org.elasticsearch.transport.TransportSerializationException_: Failed to  
> deserialize exception response from stream
> 
> ```
> at
> 
> ```
> 
> org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(  
> _MessageChannelHandler.java:169_)
> 
> ```
> at
> 
> ```
> 
> org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(  
> _MessageChannelHandler.java:123_)
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5paxw\_hxdQLGfjkbJzm0uf2GQ0BbmB7Rh7C\_RtKfgqxg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5paxw_hxdQLGfjkbJzm0uf2GQ0BbmB7Rh7C_RtKfgqxg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [May 25, 2014, 6:38am UTC](https://discuss.elastic.co/t/aggregation-with-average-value-in-java-api/17699/3 "2014-05-25T06:38:17Z")

</div>

Thank you Adrien, I'm getting the aggregation value now.

One doubt here. I have a field which stores values likes "CPU\_USED" :  
"0.04%"  
Want to do aggregation on that. Can I do any string manipulation here on  
the field passed on AggregationBuilders ? Tried like this but not working .  
Please suggest.  
addAggregation(AggregationBuilders.avg("cpu\_used\_avg").script("doc['CPU\_USED'].value".replace("%",  
"")))

Thanks  
Subhadip

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8635fb87-8d03-44ac-866a-550b5bb07dce%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8635fb87-8d03-44ac-866a-550b5bb07dce%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:27am UTC](https://discuss.elastic.co/t/aggregation-with-average-value-in-java-api/17699/4 "2017-07-06T01:27:09Z")

</div>


