按具有Elasticsearch联接字段的父文档的属性聚合

wqlqzqxt  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(171)

我有一个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版

vohkndzv

vohkndzv1#

您可以使用以下两种方法中的任何一种来按类别查找评论计数。
第一个

相关问题