title: elasticsearch Get aggregations in Java
body: <p>How can I get the aggregations of a query in Java?</p>
  
  <p>This is my query:</p>
  
  <pre><code>GET my_dataset/document/_search
       {
          "query": {
               "match": {
                   "docId": "1"
              }
          },
          "aggs": {
              "text": {
                  "avg": {
                      "script": "doc['text'].values.size()" 
                 }
              }
          }
       }
  </code></pre>
  
  <p>This is the output that I get</p>
  
  <pre><code>..
  },
    "aggregations": {
      "text": {
        "value": 32
      }
    }
  </code></pre>
  
  <p>How can I get the value 32? I am trying the following with no success</p>
  
  <pre><code>response.getAggregations().getValue("text"); 
  </code></pre>
  
  <p>This is how I build the query in Java and get the response</p>
  
  <pre><code>  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();
  </code></pre>
  
  <p>This is the response that I got while debugging
  <a href="https://i.stack.imgur.com/3Aqqz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Aqqz.png" alt="enter image description here"></a></p>