nabrugir nabrugir - 1 year ago 60
Java Question

elasticsearch Get aggregations in Java

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


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

XContentBuilder aggregationBuilder = XContentFactory.jsonBuilder();
.field("docId", docID)
.field("script", "doc['text'].values.size()")

SearchResponse response = client.prepareSearch("my_dataset").setTypes("document")

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

Answer Source

The solution that worked is


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