如何从 elasticsearch 中的术语聚合查询中获取所有 doc_counts 的总和?

How to get Sum of all doc_counts from term aggregation query in elasticsearch?

我有评论,每条评论都链接到 属性。因此,对于 属性 id,可能会有多个评论,并且这些评论也可能是重复的。 现在我需要获取每个 属性.

的重复评论总数

这就是我如何获得每个 属性

的重复评论
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "reviewKeyword": {
              "value": ""
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "propertyGrouping": {
      "terms": {
        "field": "propertyId",
        "size": 10
      }
      , "aggs": {
        "dupReviwes": {
          "terms": {
            "field": "reviewKeyword",
            "size": 100
          }
        }
      }
    }
  }

现在我还想要的是子聚合返回的文档计数的总和。 这是示例响应

{
  "buckets": [
    {
      "key": 532,
      "doc_count": 2431,
      "dupReviwes": {
        "doc_count_error_upper_bound": 10,
        "sum_other_doc_count": 2382,
        "buckets": [
          {
            "key": "Good hotel",
            "doc_count": 31
          },
          {
            "key": "Overall good",
            "doc_count": 18
          }
        ]
      }
    },
    {
      "key": 496,
      "doc_count": 2207,
      "dupReviwes": {
        "doc_count_error_upper_bound": 8,
        "sum_other_doc_count": 2185,
        "buckets": [
          {
            "key": "Good",
            "doc_count": 16
          },
          {
            "key": "Nice",
            "doc_count": 6
          }
        ]
      }
    }
  ]
}

所以我还想要每个桶的文档计数总和 所以对于上述情况:对于两个键

Key : 532
sum_doc_count=49 (31+18)

key : 496
sum_doc_count=32 (16 + 6)

对于一些查询这可能吗?

是的,您可以使用 sum_bucket pipeline aggregation 轻松实现,只需像这样修改您的查询:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "reviewKeyword": {
              "value": ""
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "propertyGrouping": {
      "terms": {
        "field": "propertyId",
        "size": 10
      },
      "aggs": {
        "dupReviews": {
          "terms": {
            "field": "reviewKeyword",
            "size": 100
          }
        },
        "sum_buckets": {
          "sum_bucket": {
            "buckets_path": "dupReviews>_count"
          }
        }
      }
    }
  }
}