我有一个Elasticsearch索引,它使用join type字段通过父子关系将两种类型的索引文档相互关联起来:posts
,它们是comments
的父代。posts
有一个category
关键字字段,comments
属于posts
,我想在每个category
帖子中找到comments
的号,如下所示:
// what query do I need to get this result?
{
"aggregations" : {
"comment-counts-by-post-category" : {
"buckets" : [
{
"key" : "Dogs",
"doc_count" : 2,
},
{
"key" : "Cats",
"doc_count" : 1,
}
]
}
}
}
下面是一个完整的示例:
我有一个具有以下Map的索引:
PUT posts-index/
{
"mappings": {
"properties": {
"post": {
"type": "object",
"properties": {
"category": {
"type": "keyword"
}
}
},
"text": {
"type": "keyword"
},
"post_comment_join": {
"type": "join",
"relations": {
"post": "comment"
}
}
}
}
}
我创建了两个帖子,一个在Dogs
类别,另一个在Cats
类别:
PUT posts-index/_doc/post-1
{
"text": "this is a dog post",
"post": {
"category": "Dogs"
},
"post_comment_join": {
"name": "post"
}
}
PUT posts-index/_doc/post-2
{
"text": "this is a cat post",
"post": {
"category": "Cats"
},
"post_comment_join": {
"name": "post"
}
}
然后,我创建了一些评论(在本例中,2条关于狗的帖子,1条关于猫的帖子)
PUT posts-index/_doc/comment-1&routing=1&refresh
{
"text": "this is comment 1 for post 1",
"post_comment_join": {
"name": "comment",
"parent": "post-1"
}
}
PUT posts-index/_doc/comment-2&routing=1&refresh
{
"text": "this is comment 2 for post 1",
"post_comment_join": {
"name": "comment",
"parent": "post-1"
}
}
PUT posts-index/_doc/comment-3&routing=1&refresh
{
"text": "this is a comment 1 for post 2",
"post_comment_join": {
"name": "comment",
"parent": "post-2"
}
}
我可以使用has_parent
查询搜索所有comment
文档:
POST post-index/_search
{
"query": {
"has_parent": {
"parent_type": "post",
"query": {
"match_all": {}
}
}
}
}
{
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [ /* returns the 3 comments */ ]
}
}
我不知道如何找到每个类别中的评论数量
我研究过Parent Aggregations,但它们似乎只允许基于父节点的type
进行聚合。在本例中,所有父节点都是post
类型,所以这没有帮助。
我还尝试使用基本的terms
聚合,使用join_field#parent_field
语法:
POST post-index/_search
{
"query": {
"has_parent": {
"parent_type": "post",
"query": {
"match_all": {}
}
}
},
"aggs": {
"agg-by-post-category": {
"terms": {
"field": "post_comment_join#post.category"
}
}
}
}
// returns { "buckets": [] } in the aggs
不幸的是,这没有返回任何结果。看起来好像post_comment_join#post
语法可以用于按父文档 * 聚合 *,但不能按父文档上的 * 属性 * 聚合。(即,按post
的_id
字段聚合,但不能按post.category
聚合)
有人能帮我找出正确的aggs
语法来返回所有按父帖子类别分组的评论吗?
同样,这是我想要的结果:
{
"aggregations" : {
"comment-counts-by-post-category" : {
"buckets" : [
{
"key" : "Dogs",
"doc_count" : 2,
},
{
"key" : "Cats",
"doc_count" : 1,
}
]
}
}
}
平台详细信息
亚马逊Opensearch服务7.9版
1条答案
按热度按时间vohkndzv1#
您可以使用以下两种方法中的任何一种来按类别查找评论计数。
第一个