2014-11-21 57 views
0

我想计算Elasticsearch中的项聚合器的计数之间的pourcentile。使用Elasticsearch中的子项聚合器的数据的脚本

我的查询:

{ 
    "query" : { 
     "match_all" : {} 
    }, 
    "size" : 0, 
    "aggs": { 
     "eventName" : { 
      "terms" : { "field" : "json.eventName" } 
     } 
    } 
} 

结果聚合:

"aggregations": { 
    "eventName": { 
     "doc_count_error_upper_bound": 0, 
     "buckets": [ 
      { 
       "key": "term1", 
       "doc_count": 30235 
      }, 
      { 
       "key": "term2", 
       "doc_count": 30216 
      }, 
      { 
       "key": "term3", 
       "doc_count": 22177 
      }, 
      { 
       "key": "term4", 
       "doc_count": 17173 
      } 
     ] 
    } 
} 

我想 “字词1” 和 “term4” 之间的这一指标为例:56%

+0

不知道我的问题? – 2014-11-24 08:31:04

回答

0

我觉得scripted_metric能帮上忙。

看看我的答案不同this的问题。

在你的情况下,你可以计算两个条件,然后返回term4Cnt/term1Cnt。 A的你所需要的粗略估计:

"init_script": "_agg.term1Cnt = 0; _agg.term4Cnt = 0;", 
"map_script": "if (doc.json.eventName == "term1") { 
        _agg.term1Cnt += 1; 
       } else if (doc.json.eventName == "term4") { 
        _agg.term4Cnt += 1;", 
       }" 
"reduce_script": "term1Cnt = 0; term4Cnt = 0; 
        for (agg in _aggs) { 
        term1Cnt += agg.term1Cnt; 
        term4Cnt += agg.term4Cnt; 
        }; 
        return term4Cnt/term4Cnt;" 

这是假设你知道你的提前条款(事件名称)。您也可以过滤相关事件。

希望这有助于。