Chapter VII - the parent - child relationship Document
Tiger brothers, father and son soldiers into battle.
This chapter as a complex search bedding, introduces his son documentation in order to better introduce ES operating in complex scenes.
In the non-relational database database, we often have associated with the table look-up table. For example relational query tables and student achievement tables will be able to find out information and performance information Society. In ES, the parent-child relationship is similar to the document associated with the query table.
background
ES5.x begun using the parent-child relationship document multi-table related query, you can create multiple core types Type next index Index. But ES6.x began to allow only the next index Index to create a type Type, and even created will be removed. Type in a future release. In order to continue to support multi-table associated with the query, ES6.x introduced a join
new type of document to support the creation of a parent-child relationship.
problem
Suppose now that there is such a demand scenario: a blog a number of articles, the article title, content, author, date, etc., and there will be an article in the review, comment information content of the comments, author, date, etc., by ES to store blog articles and review information.
At this time, the article itself is the "father", and comment is the "child", such problems can also nested
implement nested objects, in most cases netsted
nested objects and parent-child
parent and child objects can replace each other, but they are still different advantages and disadvantages. The following describes these two data structures.
nested nested objects
Data structure of an article as shown below:
{
"title":"ElasticSearch6.x实战教程",
"author":"OKevin",
"content":"这是一篇水文",
"created":1562141626000,
"comments":[{
"name":"张三",
"content":"写的真菜",
"created":1562141689000
},{
"name":"李四",
"content":"辣鸡",
"created":1562141745000
}]
}
And create an index mapping structure defined by RESTful API:
PUT http://localhost:9200/blog
{
"mappings":{
"article":{
"properties":{
"title":{
"type":"text",
"analyzer":"ik_smart",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"author":{
"type":"text",
"analyzer":"ik_smart",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"content":{
"type":"text",
"analyzer":"ik_smart"
},
"created":{
"type":"date"
},
"comments":{
"type":"nested",
"properties":{
"name":{
"type":"text",
"analyzer":"ik_smart",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"content":{
"type":"text",
"analyzer":"ik_smart",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"created":{
"type":"date"
}
}
}
}
}
}
}
Insert data:
POST http://localhost:9200/blog/article
{
"title":"ElasticSearch6.x实战教程",
"author":"OKevin",
"content":"这是一篇水文",
"created":1562141626000,
"comments":[{
"name":"张三",
"content":"写的真菜",
"created":1562141689000
},{
"name":"李四",
"content":"辣鸡",
"created":1562141745000
}]
}
POST http://localhost:9200/blog/article
{
"title":"ElasticSearch6.x从入门到放弃",
"author":"OKevin",
"content":"这是一篇ES从入门到放弃文章",
"created":1562144089000,
"comments":[{
"name":"张三",
"content":"我已入门",
"created":1562144089000
},{
"name":"李四",
"content":"我已放弃",
"created":1562144089000
}]
}
POST http://localhost:9200/blog/article
{
"title":"ElasticSearch6.x原理解析",
"author":"专家",
"content":"这是一篇ES原理解析的文章",
"created":1562144089000,
"comments":[{
"name":"张三",
"content":"牛逼,专家就是不一样",
"created":1562144089000
},{
"name":"李四",
"content":"大牛",
"created":1562144089000
}]
}
- Query author "OKevin" articles All comments (parent check child)
GET http://localhost:9200/blog/article/_search
{
"query":{
"bool":{
"must":[{
"match":{
"author.keyword":"OKevin"
}
}]
}
}
}
ES results returned all of the data is "OKevin" of the two.
- Queries article contains comments "spicy chicken" (check sub parent)
GET http://localhost:9200/blog/article/_search
{
"query":{
"bool":{
"must":[{
"match":{
"author.keyword":"OKevin"
}
},{
"nested":{
"path":"comments",
"query":{
"bool":{
"must":[{
"match":{
"comments.content":"辣鸡"
}
}]
}
}
}
}]
}
}
}
ES did just returned data contains "spicy chicken".
Two queries are returned directly to the entire document data.
parent-child father and son documentation
Since the father and son to achieve the associated document lookup table, and that its data structure should be like this:
Article data structure
{
"title":"ElasticSearch6.x实战教程",
"author":"OKevin",
"content":"这是一篇实战教程",
"created":1562141626000,
"comments":[]
}
Comments data structures
{
"name":"张三",
"content":"写的真菜",
"created":1562141689000
}
ES6.x used to be associated with these two structures are stored (it looks closer to query relational database tables associated with table) in two types Type in, but the beginning of a ES6.x index Index can only create a type type, to think for surface-related inquiries, it means that the above two tables need to rub together, ES6.x thereby defining a new data type - join
.
And create an index mapping structure defined by RESTful API:
{
"mappings":{
"article":{
"properties":{
"title":{
"type":"text",
"analyzer":"ik_smart",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"author":{
"type":"text",
"analyzer":"ik_smart",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"content":{
"type":"text",
"analyzer":"ik_smart"
},
"created":{
"type":"date"
},
"comments":{
"type":"join",
"relations":{
"article":"comment"
}
}
}
}
}
}
Focus on one of the "comments" field, you can see the type is defined as join
, relations defines who is a parent who is a child, "article": "comment" represents the article is the parent comment is a child.
Insert the document is Father and Son father and son were inserted (as can be understood as more tables tucked it away in a table).
Insert in the parent document:
POST http://localhost:9200/blog/article/1
{
"title":"ElasticSearch6.x实战教程",
"author":"OKevin",
"content":"这是一篇水文",
"created":1562141626000,
"comments":"article"
}
POST http://localhost:9200/blog/article/2
{
"title":"ElasticSearch6.x从入门到放弃",
"author":"OKevin",
"content":"这是一篇ES从入门到放弃文章",
"created":1562144089000,
"comments":"article"
}
POST http://localhost:9200/blog/article/3
{
"title":"ElasticSearch6.x原理解析",
"author":"专家",
"content":"这是一篇ES原理解析的文章",
"created":1562144089000,
"comments":"article"
}
Insert Subdocument:
POST http://localhost:9200/blog/article/4?routing=1
{
"name":"张三",
"content":"写的真菜",
"created":1562141689000,
"comments":{
"name":"comment",
"parent":1
}
}
POST http://localhost:9200/blog/article/5?routing=1
{
"name":"李四",
"content":"辣鸡",
"created":1562141745000,
"comments":{
"name":"comment",
"parent":1
}
}
POST http://localhost:9200/blog/article/6?routing=2
{
"name":"张三",
"content":"我已入门",
"created":1562144089000,
"comments":{
"name":"comment",
"parent":2
}
}
POST http://localhost:9200/blog/article/7?routing=2
{
"name":"李四",
"content":"我已放弃",
"created":1562144089000,
"comments":{
"name":"comment",
"parent":2
}
}
POST http://localhost:9200/blog/article/8?routing=3
{
"name":"张三",
"content":"牛逼,专家就是不一样",
"created":1562144089000,
"comments":{
"name":"comment",
"parent":3
}
}
POST http://localhost:9200/blog/article/9?routing=3
{
"name":"李四",
"content":"大牛",
"created":1562144089000,
"comments":{
"name":"comment",
"parent":3
}
}
If you query the index data will find a total of nine data, not nested
as a "comments" nested "article" in.
- Query author "OKevin" articles All comments (parent check child)
GET http://localhost:9200/blog/article/_search
{
"query":{
"has_parent":{
"parent_type":"article",
"query":{
"match":{
"author.keyword":"OKevin"
}
}
}
}
}
ES only returns the data structure of the comment review, but not all data including articles also returns. This is one of the nested object queries difference between father and son Archie - the sub-document can be returned separately .
- Queries article contains comments "spicy chicken" (check sub parent)
GET http://localhost:9200/blog/artice/_search
{
"query":{
"has_child":{
"type":"comment",
"query":{
"match":{
"content":"辣鸡"
}
}
}
}
}
ES also only returns the parent document data, without the data sub-documents (comments) are.
nested
Nested objects and parent-child
the biggest difference between father and son documentation, nested objects "father" is a document data, and the "father" father and son are two documents document data. This means that if the operations involving nested objects nested documents will affect the entire document (re-index, but the query faster), including modify, delete, query. The father and son documentation subdocuments or parent document itself independent operation document, document or pair parent document does not affect each other (not re-indexing, querying relatively slow).
No public concern: CoderBuff, reply "es" get "ElasticSearch6.x combat tutorial" full version of PDF.
![](https://img2018.cnblogs.com/blog/630246/201907/630246-20190717223740465-1981496921.png)