在elasticsearch中获取所有具有child和parent

编程入门 行业动态 更新时间:2024-10-28 16:26:49
elasticsearch中获取所有具有child和parent_id的子项(Get all child which has child and parent_id in elasticsearch)

我想获得所有具有父母“博客”的“帖子”,其ID为25,并且只有那些至少有一个孩子“评论”的“帖子”

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

到目前为止,我知道我可以使用parent_id , has_child和has_parent

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

到目前为止我有:

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

I would like to get all "posts" which has parent "blog" with id: 25 and only those "posts" which has at least one child "comment"

I am new to elasticsearch. Using version 5.

So far I know that I can use parent_id, has_child and has_parent

Not sure how can I combine those queries...

so far I have:

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

最满意答案

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

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类型存储在帖子中。 你可以在这里阅读更多关于nested和parent/child关系的区别。

You can combine queries using bool query. So, assuming you have created your mappings and data similar to this example:

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

Your query is:

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

Maybe it would be more suitable to store your comments as a nested type in posts. You can read more about the difference between nested and parent/child relations here.

更多推荐

本文发布于:2023-08-07 20:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:elasticsearch   child   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!