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

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();

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

--Alex

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

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