# How to get the inner hits of a nested query ? (JAVA API version 5.5)

**URL:** https://discuss.elastic.co/t/how-to-get-the-inner-hits-of-a-nested-query-java-api-version-5-5/100146
**Category:** Elasticsearch
**Created:** [September 12, 2017, 1:32am UTC](https://discuss.elastic.co/t/how-to-get-the-inner-hits-of-a-nested-query-java-api-version-5-5/100146 "2017-09-12T01:32:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Pritom\_Ahmed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pritom_ahmed/32/20783_2.png) [@Pritom\_Ahmed](https://discuss.elastic.co/u/Pritom_Ahmed)
#### Post date: [September 12, 2017, 1:32am UTC](https://discuss.elastic.co/t/how-to-get-the-inner-hits-of-a-nested-query-java-api-version-5-5/100146/1 "2017-09-12T01:32:41Z")

</div>

Hi,

I was wondering How to get the inner hits of a nested query?

```
 SearchResponse response = client.prepareSearch("trace-index2")
                .setTypes("trace-log2")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.nestedQuery("spans",
                        QueryBuilders.boolQuery().must(QueryBuilders.termQuery("Name", "FindIDs")), ScoreMode.None))
                .setFrom(0).setSize(10).setExplain(true)
                .get();
```

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 12, 2017, 7:09am UTC](https://discuss.elastic.co/t/how-to-get-the-inner-hits-of-a-nested-query-java-api-version-5-5/100146/2 "2017-09-12T07:09:56Z")

</div>

check the `NestedQueryBuilder` returned by `QueryBuilders.nestedQuery` in order to configure inner hits.

--Alex

---

<div class="post-metadata">

### Author: ![Pritom\_Ahmed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pritom_ahmed/32/20783_2.png) [@Pritom\_Ahmed](https://discuss.elastic.co/u/Pritom_Ahmed)
#### Post date: [September 12, 2017, 6:38pm UTC](https://discuss.elastic.co/t/how-to-get-the-inner-hits-of-a-nested-query-java-api-version-5-5/100146/3 "2017-09-12T18:38:38Z")

</div>

thanks @spinscale for the reply. I implemented as you suggested. the code is as follows.

```
                .setQuery(QueryBuilders.nestedQuery("spans",
                        QueryBuilders.boolQuery().must(QueryBuilders.termQuery("spans.operationName", "FindDriverIDs")), ScoreMode.None)
                    .innerHit(new InnerHitBuilder("spansInnerHit")
                            .addDocValueField("spans.duration")
                            .setSize(10)))
                .addDocValueField("duration")
                .setFrom(0).setSize(10).setExplain(true)
                .get();

        for (SearchHit hit : response.getHits().getHits()) {
            if (hit.getInnerHits() != null) {
                for (String innerHitKey : hit.getInnerHits().keySet()) {
                    System.out.println(innerHitKey);
                    for (SearchHit innerHit : hit.getInnerHits().get(innerHitKey).getHits()) {
                        for (String key : innerHit.getFields().keySet()) {
                            SearchHitField innerObj = innerHit.getFields().get(key);
                            System.out.println(innerObj.getName() + " " + innerObj.getValue());
                        }
                    }
                }
            }
        }
    } 

```

But I do not get results. i know for a fact that there is a match with the query. I tested with the GET API.

```
curl -XGET 'localhost:9200/index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "nested": {
      "path": "spans",
      "query": {
        "bool": {
          "must": [
            { "match": { "spans.operationName": "FindDriverIDs" }}
          ]
        }
      },
      "inner_hits": {
        "highlight": {
          "fields": {
            "spans.operationName": {}
          }
        }
      }
    }
  }
}
'

```

Am I missing anything obvious ?

Thanks

---

<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: [October 10, 2017, 6:38pm UTC](https://discuss.elastic.co/t/how-to-get-the-inner-hits-of-a-nested-query-java-api-version-5-5/100146/4 "2017-10-10T18:38:49Z")

</div>

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