假设有一个简单的博客索引,它包含两种类型:博客和评论。一个博客可以有多个评论。该指数是这样Elasticsearch:翻转后的亲子关系
curl -X PUT \
'http://localhost:9200/%3Cblog-%7Bnow%2Fd%7D-000001%3E?pretty=' \
-H 'content-type: application/json' \
-d '{
"mappings": {
"comment": {
"_parent": { "type": "blog" },
"properties": {
"name": { "type": "keyword" },
"comment": { "type": "text" }
}
},
"blog": {
"properties": {
"author": { "type": "keyword" },
"subject": { "type": "text" },
"content": { "type": "text" }
}
}
}
}'
指数%3Cblog-%7Bnow%2Fd%7D-000001%3E
创建等于<blog-{now/d}-000001>
(见here更多有关日期数学)。 我们将为此索引添加'blog-active'别名。这个别名将被用于存储数据。
curl -X POST 'http://localhost:9200/_aliases?pretty=' \
-H 'content-type: application/json' \
-d '{ "actions" : [ { "add" : { "index" : "blog-*", "alias" : "blog-active" } } ] }'
现在,如果我们执行以下操作:
1.增加使用blog-active
别名
curl -X POST http://localhost:9200/blog-active/blog/1 \
-H 'content-type: application/json' \
-d '{
"author": "author1",
"subject": "subject1",
"content": "content1"
}'
2.添加到博客
curl -X POST \
'http://localhost:9200/blog-active/comment/1?parent=1' \
-H 'content-type: application/json' \
-d '{
"name": "commenter1",
"comment": "new comment1"
}'
3的评论的博客。翻转max_docs = 2
curl -X POST \
http://localhost:9200/blog-active/_rollover \
-H 'content-type: application/json' \
-d '{
"conditions": {
"max_docs": 2
},
"mappings": {
"comment": {
"_parent": { "type": "blog" },
"properties": {
"name": { "type": "keyword" },
"comment": { "type": "text" }
}
},
"blog": {
"properties": {
"author": { "type": "keyword" },
"subject": { "type": "text" },
"content": { "type": "text" }
}
}
}
}'
4.And再添评论的博客现在
curl -X POST \
'http://localhost:9200/blog-active/comment/1?parent=1' \
-H 'content-type: application/json' \
-d '{
"name": "commenter2",
"comment": "new comment2"
}'
如果我们搜索与 '作者1' 博客所有评论所有博客指数(blog-%2A
为blog-*
)
curl -X POST \
http://localhost:9200/blog-%2A/comment/_search \
-H 'content-type: application/json' \
-d '{
"query": {
"has_parent" : {
"query" : {
"match" : { "author" : { "query" : "author1" } }
},
"parent_type" : "blog"
}
}
}'
的结果只包含第一条评论。
这是由于第二个评论是在第二个索引中,它本身没有父博客文档。所以它不知道博客的作者。
所以,我的问题是怎样运用的亲子关系是使用侧翻时?
在这种情况下,这种关系甚至可能吗?
类似的问题:ElasticSearch parent/child on different indexes