1

I have the following: I notice that at the end of running the code, if I print out aggregations.asMap().get('subjects'); I am getting: org.elasticsearch.search.aggregations.bucket.terms.StringTerms@6cff59fa

Printing out "aggregations" gives me: org.elasticsearch.search.aggregations.InternalAggregations@65cf321d

What I really want is the entire string/json response that is normally returned if you were to curl on elasticsearch to get aggregations. How do I get to the raw response from the aggregation query? Also, is there a way to iterate and print out what's in those "wrapped up" objects?

https://github.com/spring-projects/spring-data-elasticsearch/blob/ab7e870d5f82f6c0de236048bd7001e8e7d2a680/src/test/java/org/springframework/data/elasticsearch/core/aggregation/ElasticsearchTemplateAggregationTests.java

@Test
public void shouldReturnAggregatedResponseForGivenSearchQuery() {
    // given
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .withSearchType(COUNT)
            .withIndices("articles").withTypes("article")
            .addAggregation(terms("subjects").field("subject"))
            .build();
    // when
    Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });
    // then
    System.out.println(aggregations); // gives me some cryptic InternalAggregations object, how do I get to the raw JSON normally returned by elasticsearch?
    System.out.println(aggregations.asMap().get("subjects")); // gives me some StringTerms object I have no idea how to iterate over to get results
}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Rolando
  • 58,640
  • 98
  • 266
  • 407

2 Answers2

3

You cannot get the raw JSON response this way, since Spring Data Elasticsearch will take care of parsing it for you, that's the whole point.

If you need to parse those buckets, you can do it like this easily:

...
StringTerms subjects = aggregations.asMap().get("subjects");
for (Terms.Bucket bucket : subjects.getBuckets()) {
    String key = bucket.getKey();
    long docCount = bucket.getDocCount();
    // do something with the key and the doc count
}

If you really want to see the JSON being returned, what you can do is to re-write the parsed Aggregations object into JSON using serialization, but that won't really be helpful:

InternalAggregations aggregations = ...;
XContentBuilder jsonBuilder = JsonXContent.contentBuilder();
aggregations.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
String rawJson = jsonBuilder.string();
Val
  • 207,596
  • 13
  • 358
  • 360
  • Is there anything that allows me to pass in raw JSON without having to use all these builders to make the query? – Rolando Dec 28 '15 at 05:59
  • I don't think so. The whole point of this API is to make it easier to build queries without having to deal with JSON. – Val Dec 28 '15 at 06:00
  • @Rolando I stumbled upon elasticsearch templates and looks like that is what i would be soon using. Trying to figure out JAVA APIs and then using elasticsearch slowlog file to match if the query was right is crazy! – dy10 Jan 05 '18 at 13:04
  • Am I correct assuming this is relevant to 3.x spring-data-elasticsearch? (I am trying to figure out docCounts in 4.0.) – Jaroslav Záruba Aug 25 '20 at 20:47
  • Hi @Val please check out my question if you have time https://stackoverflow.com/questions/72478148/how-to-get-elasticsearch-data-aggregations-using-swagger-api-calls-spring-data – cluis92 Jun 02 '22 at 14:48
-1

Set Size of EsRequest to Zero

Get Esresponse.toString()

Convert String to Json

Get aggregation field from Json.