Generic way to read aggregations from JAVA API

It seems to me that with the JAVA API when trying to read aggregation results, you have to know each specific bucket type.

Is there a quick way to just loop through the aggs and dump them to file using JAVA API?

loop through the bucket? http://stackoverflow.com/questions/21018493/how-to-access-aggregations-result-with-elasticsearch-java-api-in-searchresponse

What format are you wanting to use when you dump them to a file? if you are wanting to write the file in JSON you could use the XContentBuilder. A simple way to do this would be:

XContentHelper.toString(aggregation)

Hi, Jason I saw that one but you have to specifically cast each aggregation. If you have a nice complex aggregation with nesting it gets complicated.

For instance I have...

date_histogram
     terms
          avg

So using the API you would have to build the type of solution that could accommodate all the possible scenarios.

Colings86, yep it did the trick (Found it just as I was reading your response):

builder = XContentFactory.jsonBuilder();
builder.startObject();
scrollResp.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
2 Likes

Hey there. I can't figure out a proper solution on the same issue. I am using the Java API to run queries and process searchhit-responses. I want to attach all aggregations to our response-object (grouped search results). So far I could apply this by parsing the full response-string.

Any advice or help is appreciated :slight_smile:

Cheers, Dominik

you must use metaData field of aggregation for this purpose. While creating aggregationBuilder for querying set a metaData storing all the info which u later require about this aggregation.In Java API, its a map of String pointing to Objects.
The search response will return the metaData added during querying as it is in the response.

I tried reading the metaData field of the aggregation but it seems to always be null. I'd also like to have a reasonably generic way of reading the aggregations, but it seems a bit difficult.

My production code currently uses the REST API and parses the json string to a Map and then passes it on to various methods for processing. Migrating all those processing methods to use the Java API seems very cumbersome because of this. Is there an efficient way to turn the SearchResponse into a Map? I could use that as an interim solution and leave those processing methods untouched for now. SearchResponse has a toXContent that can be used to format a json string and that could then be parsed back to a Map but that doesn't seem efficient at all.