You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch中如何统计各房产重复评论的总数量?

Solution: Calculate Total Duplicate Reviews per Property

Absolutely! You can compute the total sum of duplicate review counts for each property directly within your Elasticsearch query using a pipeline aggregation. Here's how to adjust your existing query to get the desired result:

Modified Query

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

Key Changes Explained

  • We added a new pipeline aggregation total_duplicate_reviews under the propertyGrouping aggregation.
  • The sum_bucket aggregation uses the buckets_path parameter to target the doc_count (shortened to _count in Elasticsearch) of each bucket in the dupReviwes terms aggregation. This automatically sums up all duplicate counts for unique review keywords under each property.

Expected Response Output

Your response will now include the total_duplicate_reviews value for each property bucket, showing the exact sum of all duplicate review counts:

{
  "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 }
        ]
      },
      "total_duplicate_reviews": {
        "value": 49
      }
    },
    {
      "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 }
        ]
      },
      "total_duplicate_reviews": {
        "value": 22
      }
    }
  ]
}

Quick Note

The sum_other_doc_count in your original response represents duplicate reviews that weren't included in the top 100 reviewKeyword buckets. If you want to include these in your total sum, adjust the size parameter in the dupReviwes terms aggregation to a value large enough to capture all unique review keywords for each property (or use size: 0 if you only need the total and don't care about individual keyword breakdowns).

内容的提问来源于stack exchange,提问作者Yatin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 03:47:32