使用ES 1.7.3,无法基于两个不同聚合的结果来计算数据,但是,在ES
2.0中,可以使用管道聚合来完成此 *** 作。
但是,您要问的内容并不太复杂,无法在客户端使用1.7.3进行。如果使用下面的查询,您将获得获得期望数字所需要的一切:
POST components/_search{ "size": 0, "aggs": { "total_duration": { "sum": { "field": "duration" } }, "components": { "terms": { "field": "component" }, "aggs": { "duration_sum": { "sum": { "field": "duration" } } } } }}
结果将如下所示:
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "total_duration": { "value": 44 }, "components": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "a", "doc_count": 2, "duration_sum": { "value": 15 } }, { "key": "b", "doc_count": 1, "duration_sum": { "value": 27 } }, { "key": "c", "doc_count": 1, "duration_sum": { "value": 2 } } ] } }}
现在,您需要做的只是以下内容。我正在使用Javascript,但是您可以使用任何其他可以读取JSON的语言来执行此 *** 作。
var response = ...the JSON response above...var total_duration = response.aggregations.total_duration.value;var total_docs = response.hits.total;response.aggregations.components.buckets.forEach(function(comp_stats) { // total duration for the component var total_duration_comp = comp_stats.duration_sum.value; // percentage duration of the component var perc_duration_comp = total_duration_comp / total_duration * 100; // percentage documents for the component var perc_doc_comp = comp_stats.doc_count / total_docs * 100;});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)