从聚合结果求和doc_count-错误“聚合不存在buckets_path”

时间:2019-11-15 20:41:52

标签: elasticsearch

我有以下查询和汇总:

{
"query": {
    "bool": {
        "must": {
            "multi_match": {
                "query": "qatar"
            }
        },
        "filter": {
            "range": {
                "timestamp": {
                    "gte": "2019-07-06 00:00:00",
                    "lte": "2019-10-08 00:00:00"
                }
            }
        }
    }
},
"aggs": {
    "setinment_agg": {
        "terms": {
            "field": "sentiment"
        }
    }
}

}

哪个返回以下响应:

 {
"took": 3,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
        "value": 10000,
        "relation": "gte"
    },
    "max_score": null,
    "hits": []
},
"aggregations": {
    "setinment_agg": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
                "key": "positive",
                "doc_count": 132050
            },
            {
                "key": "negative",
                "doc_count": 79410
            },
            {
                "key": "unknown",
                "doc_count": 15162
            },
            {
                "key": "neutral",
                "doc_count": 4440
            }
        ]
    }
}

}

然后我添加了一个总和,以尝试将所有结果doc_count加起来,如下所示:

{
"query": {
    "bool": {
        "must": {
            "multi_match": {
                "query": "qatar"
            }
        },
        "filter": {
            "range": {
                "timestamp": {
                    "gte": "2019-07-06 00:00:00",
                    "lte": "2019-10-08 00:00:00"
                }
            }
        }
    }
},
"aggs": {
    "setinment_agg": {
        "terms": {
            "field": "sentiment"
        }
    },
    "sum_buckets": {
        "sum_bucket": {
            "buckets_path": "sentiment_agg>_count"
        }
    }
}

}

会产生以下错误:

{
"error": {
    "root_cause": [{
        "type": "illegal_argument_exception",
        "reason": "buckets_path aggregation does not exist for aggregation [sum_buckets]: sentiment_agg>_count"
    }],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [{
        "shard": 0,
        "index": "rad_worldcup",
        "node": "cUbcxsrSRn2VYcIEOh42tQ",
        "reason": {
            "type": "illegal_argument_exception",
            "reason": "buckets_path aggregation does not exist for aggregation [sum_buckets]: sentiment_agg>_count"
        }
    }],
    "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "buckets_path aggregation does not exist for aggregation [sum_buckets]: sentiment_agg>_count",
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "buckets_path aggregation does not exist for aggregation [sum_buckets]: sentiment_agg>_count"
        }
    }
},
"status": 400

}

我一定在这里误解了buckets_path和管道的用法。如果要添加所有doc_count,这不是我应该怎么做吗?预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

不是所有汇总的总和doc_count ==总点击数? 如果您只有这4种情绪,并且总是将它们全部收回,则应该使用:

{
    "track_total_hits": true,
     "query": { ...

,总匹配将是doc_counts个总和

https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-track-total-hits.html