ES Parent-Child document query

Source: Internet
Author: User

Features of parent-child documents

1. The parent/subdocument is completely independent.

2. Parent document updates do not affect sub-documents.

3. Sub-document updates do not affect parent documents or other subdocuments.

Mapping and indexing of parent-child documents

1. The establishment of a parent-child relationship type must be determined when the index is new or update-mapping

PUT/company{"Mappings": {    "Branch": {},//Parent Document Type    "Employee": {                  "_parent": {        "type":"Branch"      //Child Document Type      }    }  }}

2. The index of the parent document is the same as the normal document index.

post/company/branch/_bulk{"Index": {"_id":"London" }}{ "name":"London Westminster"," City":"London","Country":"UK" }

3. The subdocument index must specify its corresponding parent document ID, acting as:

    • Establish associations between parent-child documents
    • Ensure that the subdocument can be indexed to the Shard where the parent document resides (parent ID as Route)
put/company/employee/1? parent=london//Specify ID = parent document for London{  "name":"Alice Smith",  "DOB":"1970-10-24",  "Hobby":"Hiking"}

4. If you want to change the document's parent document, you cannot just update or reindex the old document (the new parent document may be on a different shard), you need to delete the old document and then re-index it.

Application of parent-child relationship

Seeing Parent-child relationships, it's easy to think of a variety of JOIN operations like SQL-such as querying a document and retrieving all of the parent or child documents.

However, similar JOIN queries are not supported in ES.

The parent-child relationship in ES is basically understood to be just a filter condition, as follows:

//query a document, only the document has a "parent document" and meets certain criteria to match{"has_parent": {//whether the document has a parent      "type":"Branch",//its parent must be of type branch      "Query": {//its parent must meet the following query criteria        "Match": {          "Country":"UK"        }      }    }                           //if the above conditions are met, hit the document}//query a document, only the document has a "subdocument" and meet certain criteria to calculate the match{"Has_child": {//whether the document has a child      "type":"Employee",//the child's type must be an employee      "Query": {//its parent must meet the following query criteria        "Match": {          "name":"Alice Smith"        }      }    }                                //if the above conditions are met, hit the document}

1. Has_child: Find the parent document based on the content of the subdocument

//Requestget/company/branch/_search{"Query": {    "Has_child": {//Query the parent document that satisfies the criteria based on the child's content      "type":"Employee",      "Query": {//to perform a match query operation in child        "Match": {          "name":"Alice Smith"        }      }    }  }}//Results{  "took":2,  "timed_out":false,  "_shards": {    " Total":5,    "successful":5,    "failed":0  },  "hits": {    " Total":1,    "Max_score":1,    "hits": [      {        "_index":" Company",        "_type":"Branch",//Attention!!! Returns the parent's document        "_id":"London",        "_score":1,        "_source": {          "name":"London Westminster",          " City":"London",          "Country":"UK"        }      }    ]  }}

2. Has_parent: Find sub-documents based on the contents of the parent document

//Request Get/company/employee/_search{  "Query": {    "has_parent": {//Query the child document that satisfies the criteria, based on the contents of the parent      "type":"Branch",       "Query": {//to execute a match query query in the parent        "Match": {          "Country":"UK"        }      }    }  }}//Results{  "took":2,  "timed_out":false,  "_shards": {    " Total":5,    "successful":5,    "failed":0  },  "hits": {    " Total":1,    "Max_score":1,    "hits": [      {        "_index":" Company",        "_type":"Employee",//Attention!!! The child's document is returned        "_id":"1",        "_score":1,        "_routing":"London",        "_parent":"London",        "_source": {          "name":"Alice Smith",          "DOB":"1970-10-24",          "Hobby":"Hiking"        }      }    ]  }}

3. Children Aggregation:

ES Parent-Child document query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.