Elasticsearch中如何统计各房产重复评论的总数量?
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_reviewsunder thepropertyGroupingaggregation. - The
sum_bucketaggregation uses thebuckets_pathparameter to target thedoc_count(shortened to_countin Elasticsearch) of each bucket in thedupReviwesterms 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

