Features of parent-child documents

1. Father / Subdocuments are completely independent .

2. Parent document updates do not affect child documents .

3. Child document updates do not affect the parent document or other child documents .

Mapping and indexing of parent-child documents

1. Father and son type The creation of must be in index new or update-mapping It's time to make sure

PUT /company
{
"mappings": {
"branch": {}, // Parent document type
"employee": {
"_parent": {
"type": "branch" // subdocument type
}
}
}
}

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

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, effect :

  • Establish an association between parent and child documents
  • Ensure that the child document can be indexed to the fragment where the parent document is located (parent id As route)
PUT /company/employee/?parent=london // Appoint id = london The parent document of 
{
"name": "Alice Smith",
"dob": "1970-10-24",
"hobby": "hiking"
}

4. If you want to change the parent document of a document , Not just update perhaps reindex Old documents ( The new parent document may be on different tiles ), You need to delete the old document and re index it .

The application of paternity

notice parent-child Relationship , It's easy to think of things like SQL All kinds of things like that JOIN operation —— For example, query a document and retrieve all the parent or child documents .

However ,ES Similar... Is not supported in JOIN Inquire about . Even if child aggregation It can't be like SQL Like that JOIN operation !

stay ES Medium parent-child Relationship can be understood as a filter condition , as follows :

// Query a document , Only this document has " Parent document " And only when certain conditions are met can it be regarded as a match 
{"has_parent": { // Does the document have parent
"type": "branch", // Its parent Where type Must be branch
"query": { // Its parent The following must be satisfied query Conditions
"match": {
"country": "UK"
}
}
} // If the above conditions are met ,hit The document
}
// Query a document , Only this document has " subdocument " And only when certain conditions are met can it be regarded as a match
{
"has_child": { // Does the document have child
"type": "employee", // Its child Where type Must be employee
"query": { // Its parent The following must be satisfied query Conditions
"match": {
"name": "Alice Smith"
}
}
} // If the above conditions are met ,hit The document
}

1. has_child: Based on the content of the subdocument , Find the parent document

// request GET /company/branch/_search
{
"query": {
"has_child": { // be based on child The content of , To query for parent file
"type": "employee",
"query": { // stay child In the implementation of match query operation
"match": {
"name": "Alice Smith"
}
}
}
}
}
// result
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "company",
"_type": "branch", // Be careful !!! The return is parent Documents
"_id": "london",
"_score": ,
"_source": {
"name": "London Westminster",
"city": "London",
"country": "UK"
}
}
]
}
}

2. has_parent: Based on the content of the parent document , Find subdocuments

// request GET /company/employee/_search
{
"query": {
"has_parent": { // be based on parent The content of , To query for child file
"type": "branch",
"query": { // stay parent In the implementation of match query Inquire about
"match": {
"country": "UK"
}
}
}
}
}
// result
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "company",
"_type": "employee", // Be careful !!! The return is child Documents
"_id": "",
"_score": ,
"_routing": "london",
"_parent": "london",
"_source": {
"name": "Alice Smith",
"dob": "1970-10-24",
"hobby": "hiking"
}
}
]
}
}

3. children aggregation: To the related child Document aggregation operations

// request GET /company/branch/_search
{
"size" : ,
"aggs": {
"country": {
"terms": {
"field": "country" // In different ways country To group ( Barrel Division )
},
"aggs": {
"employees": {
"children": { //children aggregation, Son type by employee
"type": "employee"
},
"aggs": {
"hobby": {
"terms": {
"field": "hobby" // In different ways hobby To group ( Barrel Division )
}
}
}
}
}
}
}
}
// result
"aggregations": {
"country": {
"doc_count_error_upper_bound": ,
"sum_other_doc_count": ,
"buckets": [ //country Aggregation results
{
"key": "uk",
"doc_count": ,
"employees": { //children aggregation polymerization
"doc_count": ,
"hobby": {
"doc_count_error_upper_bound": ,
"sum_other_doc_count": ,
"buckets": [ //hobby Aggregation results
{
"key": "hiking",
"doc_count":
}
]
}
}
}
]
}
}

ES More related articles about parent-child document query

  1. ElasticSearch 6.x Parent child documentation [join] analysis

    ES6.0 in the future , Indexed type There can only be one , It makes the structure of father and son less clear , For, after all, java For developers ,index->db,type->table It's easier to understand the structure of . According to the official instructions , The previous one ...

  2. 015-elasticsearch5.4.3【 5、 ... and 】- Search for API【 Four 】Joining Multi document query 、GEO Inquire about 、moreLikeThisQuery、script Script query 、span Span query

    One .Joining Multi document query joining query image Elasticsearch In such a distributed system, complete SQL Style connections are very expensive . contrary ,Elasticsearch Provides two forms of connection , Aim at the level of ...

  3. elasticsearch Parent child documentation ( 11、 ... and )

    explain demand One product is sold in multiple regions Each region has its own price , The way 1 Redundant lines ,a The products are in   area1 area2 area3 Regional sales a The product will generate 3 Product data Search for id Just remove the weight , But the problem is polymerization ...

  4. Elasticsearch Must know must know dry goods knowledge one :ES Index the CRUD

    ​ If in tradition DBMS Query massive data in relational database , Especially fuzzy query , We usually use it like % The value of the query %, But this will make it impossible to apply the index , So the efficiency of full table scanning is low , Even if it's looking for exact values in indexed fields , In the face of massive data ...

  5. Elasticsearch Additions and deletions And —— mget Multi document query

    Previously, we talked about the addition, deletion, modification and query of a single document , Basically, it also achieves the function of a basic database . This article mainly describes the query of multiple documents , Through this query syntax , According to the query conditions of multiple documents , Returns multiple document collections . For more information, please refer to my collation ELK writing ...

  6. Elasticsearch Document query

    Simple data sets up to now , I've learned the basics , Now let's try a more realistic dataset , Here's a fictional one ready JSON, About the customer's bank account information . The structure of each document is as follows : { , , "firstname& ...

  7. SpringMVC MongoDB And “ Basic document query (Query、BasicQuery)”

    One . brief introduction spring Data  MongoDB Provides org.springframework.data.mongodb.core.MongoTemplate Yes MongoDB Of CRUD The operation of , Last time I ...

  8. Spring Data MongoDB 3、 ... and : Basic document query (Query、BasicQuery)( One )

    One . Brief introduction Spring Data  MongoDB Provides org.springframework.data.mongodb.core.MongoTemplate Yes MongoDB Of CRUD The operation of , Last one ...

  9. Spring Data MongoDB 3、 ... and : Basic document query (Query、BasicQuery

    One . brief introduction spring Data  MongoDB Provides org.springframework.data.mongodb.core.MongoTemplate Yes MongoDB Of CRUD The operation of , Last time I ...

Random recommendation

  1. Black horse programmer :Java Programming _7K Bank business scheduling system for interview questions

    =========== ASP.Net+Android+IOS Development ..Net train . Looking forward to communicating with you !=========== Simulate and realize the logic of bank business scheduling system , The specific requirements are as follows : There are... In the bank 6 A business window ,1 - 4 Number ...

  2. HTML5 and CSS3 Login page to make a record

    This article details the use of HTML5 and CSS3 The whole process of making a login page . View demo login.html <form id="login"> <h1> ...

  3. poi excel export The statement

    1. Question Description: ~ The front is get Submitted by , The parameter contains Chinese “ test ” ~ Derived excel, The file name is OK , And the title is messy 2. Solution: ~ Breakpoints found , Parameter reception is random code ...

  4. Keil STM32 debugging , Use ST-Link Prompt when downloading the program “flash timeout.reset the target and try it again”

    Reference resources : Very depressed. , It didn't last long STM32F4-DISCOVERY I hung up. ? STM32F103RB   Jlink It appears when debugging flash timeout.reset the target and t ...

  5. Activity Switch inside fragment Achieve the bottom of the menu switch encountered by the pit

    1. As a general rule ,app The bottom navigation will be designed to 5 Menu , have access to textView, You can also use radioButton, Here I choose to use radioButton, to radioButton Set up directly selector Can be realized ...

  6. Nginx Https Configuration without www Jump www

    hold morethink.cn and www.morethink.cn Merge into one server Up , Use 301 Permanent redirection . And then https://morethink.cn go to https://www.mor ...

  7. Hbase Environment requirements for each version

    1.HBase Various versions JDK Support situation           HBase Version JDK 7 JDK 8 JDK 9 JDK 10 2.0 Not Supported yes Not Support ...

  8. java Generate txt file

    FileWriter fileWriter = new FileWriter("C:/Users/li/Desktop/a.txt"); fileWriter.write(“aaa ...

  9. JS event ( 3、 ... and ) Some common events

    1. Scroll bar events scroll EventUtil.addHandler(window,"scroll",function(event){ if(document.compatMode ...

  10. Use PM2 management Node.js colony

    Introduce as everyone knows ,Node.js Running on the Chrome Of JavaScript On the runtime platform , We gracefully call the platform V8 engine . Whether it's V8 engine , Or later Node.js, It's all single threaded , therefore , In multi-core ...