1

How can I get the aggregations of a query in Java?

This is my query:

GET my_dataset/document/_search
     {
        "query": {
             "match": {
                 "docId": "1"
            }
        },
        "aggs": {
            "text": {
                "avg": {
                    "script": "doc['text'].values.size()" 
               }
            }
        }
     }

This is the output that I get

..
},
  "aggregations": {
    "text": {
      "value": 32
    }
  }

How can I get the value 32? I am trying the following with no success

response.getAggregations().getValue("text"); 

This is how I build the query in Java and get the response

  XContentBuilder aggregationBuilder = XContentFactory.jsonBuilder();
        aggregationBuilder.startObject()
               .startObject("query")
               .startObject("match")
               .field("docId", docID)
               .endObject()
               .endObject()
               .startObject("aggs")
               .startObject("text")
               .startObject("max")
               .field("script", "doc['text'].values.size()")
               .endObject()
               .endObject()
               .endObject()
               .endObject();

                SearchResponse response = client.prepareSearch("my_dataset").setTypes("document")
                                        .setSource(aggregationBuilder)
                                        .execute()
                                        .actionGet();

This is the response that I got while debugging enter image description here

4
  • Can you show how you're building your query in Java? Can you try response.getAggregations().get("text").get("value");? Commented May 27, 2016 at 3:24
  • get() is not a valid method for the aggregation. get("value") gives that error before compiling. Commented May 27, 2016 at 3:31
  • Fair enough. If you set a breakpoint after the query has run, what do you see in the response.getAggregations() object? Commented May 27, 2016 at 3:34
  • Solved. It is response.getAggregations().get("text").getProperty("value") Commented May 27, 2016 at 4:17

3 Answers 3

1

The solution that worked is

response.getAggregations().get("text").getProperty("value") 

getProperty("value") has to be used instead of getValue().

Sign up to request clarification or add additional context in comments.

1 Comment

can you please explain, why you propose this solution? the question is very nice formulated and it seems a bit trivial to post such a bare answer (i'm pretty sure it's the right answer, but let us know why and how it helps with the problem mentioned)
0

Can you try

response.getAggregations().get("text").getValue(); 

6 Comments

Which version of ES client are you using ?
It is the version 2.3.3
static.javadoc.io/org.elasticsearch/elasticsearch/2.3.3/org/… if you are using the 2.3.3 lib the get("text") must not complain , also change the last call to getProperty static.javadoc.io/org.elasticsearch/elasticsearch/2.3.3/org/…
get("text") does not complain but doesnt give me the value. getProperty() does not compile, same as getValue();
OK, so what does get("text").getProperty("value") give you back ?
|
0

See https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html

Example:

import org.elasticsearch.search.aggregations.metrics.avg.Avg;
...
Avg avg = searchResponse.getAggregations().get("text");
double value = avg.getValue();

Tested with version 2.3.3.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.