Yao Pan
Yao Pan

Reputation: 524

how to get parent id of a document in Elasticsearch in java?

I have documents with parent-child relationship in Elasticsearch.I do a search action in java client and get the child documents .There are getId()getIndex()getType()getSource() methods for using,but no getParent().How can I get the parent id in java?

Upvotes: 0

Views: 801

Answers (1)

Val
Val

Reputation: 217554

When iterating over the response hits, you can use the SearchHit.field("_parent") method in order to retrieve the id of the parent document.

SearchResponse response = client.prepareSearch().execute().actionGet();
for (SearchHit hit : response.getHits().getHits()) {
    SearchHitField parent = hit.field("_parent");
}

Upvotes: 1

Related Questions