2016-11-27 61 views
0

我想获得的所有“上岗”,它具有父“博客” ID为所有的孩子:25,只有那些“上岗”,它至少有一个孩子“的评论”获取其在elasticsearch儿童和PARENT_ID

我是elasticsearch的新手。使用版本5

到目前为止,我知道,我可以使用parent_idhas_childhas_parent

不知道我怎么能结合这些查询...

到目前为止我有:

GET /blog/post/_search 
{ 
    "query": { 
     "parent_id" : { 
      "type" : "post", 
      "id" : "25" 
     } 
    } 
} 

回答

0

您可以使用bool query来组合查询。
因此,假设您已经创建了类似这样的例子映射和数据:

PUT /blog 
{ 
    "mappings": { 
    "blog": {}, 
    "post": { 
     "_parent": { 
     "type": "blog" 
     } 
    }, 
    "comment": { 
     "_parent": { 
     "type": "post" 
     } 
    } 
    } 
} 

POST /blog/blog/_bulk 
{ "index": { "_id": "21" }} 
{ "name": "blog 11"} 
{ "index": { "_id": "25" }} 
{ "name": "blog 25" } 
{ "index": { "_id": "22" }} 
{ "name": "blog 24"} 

POST /blog/post/_bulk 
{ "index": { "_id": "1", "parent": "21" }} 
{ "name": "post of blog 21 (with comments)"} 
{ "index": { "_id": "2", "parent": "25" }} 
{ "name": "post of blog 25 (with comments)"} 
{ "index": { "_id": "3", "parent": "25" }} 
{ "name": "post of blog 25 (without comments)"} 

POST /blog/comment/_bulk 
{ "index": { "_id": "10", "parent": "1", "routing": "21"}} 
{ "name": "comment for post 1"} 
{ "index": { "_id": "11", "parent": "2", "routing": "25" }} 
{ "name": "comment for post 2"} 

您所查询的是:

GET blog/post/_search 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "parent_id": { 
      "type": "post", 
      "id": "25" 
      } 
     }, 
     { 
      "has_child": { 
      "type": "comment", 
      "query": { 
       "match_all": { 

       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

也许会更适合来存储您的意见为nested型帖子。
您可以阅读关于nestedparent/child关系here之间差异的更多信息。