# Iterating over ES Aggregation query using JAVA API

**URL:** <https://discuss.elastic.co/t/iterating-over-es-aggregation-query-using-java-api/118931>\
**Category:** Elasticsearch\
**Created:** [February 7, 2018, 9:37pm UTC](https://discuss.elastic.co/t/iterating-over-es-aggregation-query-using-java-api/118931 "2018-02-07T21:37:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joker](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@joker](https://discuss.elastic.co/u/joker)\
**Post date:** [February 7, 2018, 9:37pm UTC](https://discuss.elastic.co/t/iterating-over-es-aggregation-query-using-java-api/118931/1 "2018-02-07T21:37:33Z")

</div>

I have following query which is queried using JAVA, but I don't know how to iterate over "permalink\_url" field ?  
Field response for `permalink_url` looks like =\>

```
    "permalink_url": {
            "hits": {
              "total": 1,
              "max_score": 10.197851,
              "hits": [
                {
                  "_index": "instagram_2018",
                  "_type": "post",
                  "_id": "145672123558_101543454556995",
                  "_score": 10.1934331,
                  "_source": {
                    "permalink_url": "https://www.instagram.com/p/123456789/"
                  }
                }
              ]
            }
          }

```

Query =\>

```
GET instagram_2018/_search?pretty
{
  "size" : 0,
  "timeout" : 40000,
  "query" : {
    "bool" : {
      "must" : [ {
        "bool" : {
          "should" : {
            "bool" : {
              "must" : {
                "bool" : {
                  "must" : {
                    "bool" : {
                      "should" : [ {
                        "match" : {
                          "ABC.text" : {
                            "query" : "Some text",
                            "type" : "phrase"
                          }
                        }
                      }
                      } ]
                    }
                  }
                }
              }
            }
          }
        }
      }, {
        "range" : {
          "created_time" : {
            "from" : "2018-02-01T11:16:00.000Z",
            "to" : "2018-02-07T19:16:00.000Z",
            "include_lower" : true,
            "include_upper" : false
          }
        }
      }, {
        "terms" : {
          "from.name" : ["SpaceX"]
        }
      }]
    }
  },
  "aggregations" : {
    "post_type_filter" : {
      "filter" : {
        "term" : {
          "_type" : "post"
        }
      },
      "aggregations" : {
        "gender" : {
          "terms" : {
            "field" : "ABC.gender"
          }
        },
        "permalink_url" : {
          "top_hits": {
            "_source": {"includes":["permalink_url"],"excludes": [""]}
          }
        },
        "reaction_like" : {
          "sum" : {
            "field" : "ABC.reaction_like_count"
          }
        },
        "unique_users" : {
          "cardinality" : {
            "field" : "from.name"
          }
        }
      }
    }
  }
}

```

I tried following but it doesn't work -\>  
`((TopHits) aggregations.get("permalink_url")).getName()`  
`((TopHits) aggregations.get("permalink_url")).getHits().toString()`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 7, 2018, 9:37pm UTC](https://discuss.elastic.co/t/iterating-over-es-aggregation-query-using-java-api/118931/2 "2018-03-07T21:37:34Z")

</div>

Closed on March 7, 2018, 9:37pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
