Generic way to read aggregations from JAVA API

(None) #1

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?

(Jason Wee) #2

loop through the bucket?

(Colin Goodheart-Smithe) #3

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:


(None) #4

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


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();
scrollResp.toXContent(builder, ToXContent.EMPTY_PARAMS);

(Dominik) #5

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

(sneha) #6

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.

(system) #8