【发布时间】:2017-05-11 07:49:40
【问题描述】:
假设有一个简单的博客索引,它包含两种类型:博客和评论。一个博客可以有多个 cmet。索引是这样创建的
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.并在博客中添加另一条评论
curl -X POST \
'http://localhost:9200/blog-active/comment/1?parent=1' \
-H 'content-type: application/json' \
-d '{
"name": "commenter2",
"comment": "new comment2"
}'
现在,如果我们使用(blog-%2A 是 blog-*)在“author1”博客上搜索所有 cmets 的所有博客索引
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 rollover elasticsearch-5