2014-11-21 33 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;" 

這是假設你知道你的提前條款(事件名稱)。您也可以過濾相關事件。

希望這有助於。