Demand:
Searches for all child documents that have the same parent ID.
Data:
Mapping:
{" mappings": { "branch": {}, "employee": { "_parent": { "type": "Branch" } } } }
Parent Document:
{"Index": {"_id": "London"}} {"Name": "London Westminster", "City": "London", "Country": "UK"} {"Index": {"_id": "Liverpool"}} {"Name": "Liverpool Central", "City": "Liverpool", "Country": "UK"} {"Index": {"_id": "Paris"}} {"Name": "Champsélysées", "City": "Paris", "Country": "France"}
Sub-document:
{"Index": {"_id": 1, "parent": "London"}} {"Name": "Alice Smith", "DOB": "1970-10-24", "hobby": "Hiking"} {"Index": {"_id": 2, "parent": "London"}} {"Name": "Mark Thomas", "DOB": "1982-05-16", "hobby": "Diving"} {"Index": {"_id": 3, "parent": "Liverpool"}} {"Name": "Barry Smith", "DOB": "1979-04-01", "hobby": "Hiking"} {"Index": {"_id": 4, "parent": "Paris"}} {"Name": "Adrien Grand", "DOB": "1987-05-11", "hobby": "Horses"}
search for employee with parent ID London:
Curl GET Company/employee/_search
"Query": {"has_parent": {" Type": "Branch", "query": { "term": { "_parent": "London" } } } }}
The search did not result in any results. All sorts of attempts were unsuccessful, and then the answers to the questions on the StackOverflow were answered, and the correct searches were made:
Curl GET Company/employee/_search
{" query": {" has_parent": {" type": "Branch", "query": { "IDs": { "values": ["London" ] } } } }
The corresponding Java API:
Hasparentquerybuilder hpqb=querybuilders.hasparentquery ("branch", Querybuilders.idsquery (). IDs ("London"));
My question on StackOverflow link: How to search for child documents with the same parent ID in Elasticsearch?
Elasticsearch Java API (eight)--search for subdocuments with the same parent ID