2017-05-11 92 views
2

假设有一个简单的博客索引,它包含两种类型:博客和评论。一个博客可以有多个评论。该指数是这样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-%2Ablog-*

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" 
     } 
    } 
}' 

的结果只包含第一条评论。

这是由于第二个评论是在第二个索引中,它本身没有父博客文档。所以它不知道博客的作者。

blog indices

所以,我的问题是怎样运用的亲子关系是使用侧翻时?

在这种情况下,这种关系甚至可能吗?

类似的问题:ElasticSearch parent/child on different indexes

回答

0

形成的父子关系部分的所有文件必须住在同一个指数,更确切的相同碎片。因此,如果使用翻转,不可能拥有父子关系,因为它会创建新的索引。

上述问题的一种解决方案可能是通过在comment类型中添加提交的blog_authorblog_id来使数据非规范化。在这种情况下,映射看起来就像这样(请注意,父子关系已被删除):

"mappings": { 
    "comment": { 
    "properties": { 
     "blog_id": { "type": "keyword" }, 
     "blog_author": { "type": "keyword" }, 
     "name": { "type": "keyword" }, 
     "comment": { "type": "text" } 
    } 
    }, 
    "blog": { 
    "properties": { 
     "author": { "type": "keyword" }, 
     "subject": { "type": "text" }, 
     "content": { "type": "text" } 
    } 
    } 
} 

,并获取由博客作者的评论的查询是:

curl -X POST \ 
    http://localhost:9200/blog-%2A/comment/_search \ 
    -H 'cache-control: no-cache' \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "query": { 
    "match": { 
     "blog_author": "user1" 
    } 
    } 
}'