Aggregation with average value in java api

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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 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.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/9c802ccd-1c2d-4d7a-b6f5-c0f045a5097d%40googlegroups.comhttps://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.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5paxw_hxdQLGfjkbJzm0uf2GQ0BbmB7Rh7C_RtKfgqxg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/8635fb87-8d03-44ac-866a-550b5bb07dce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.