2016-07-31 72 views
1

我想在聚合中使用scripted_metric。我在我的腳本中有一些參數值,我希望爲每個查詢設置這些參數值,可以根據需要創建此查詢嗎? 下面就是我在尋找在elasticsearch中創建參數化scripted_metric

"aggs": { 
    "testAgg": { 
     "scripted_metric": { 
     "init_script": "_agg['maximum'] = []", 
     "map_script": "max = 0; for(tv in _source.tvs){ if(tv.att1>= param1 && tv.attr2 <= param2 && tv.att3 > max){max = tv.att3; }}; _agg.maximum.add(max);", 
     "combine_script": "sum = 0; for (m in _agg.maximum) { sum += m }; return sum;", 
     "reduce_script": "sum = 0; for (a in _aggs) { sum += a }; return sum;" 
     } 
    } 
    } 

參數1和參數是我的參數值,如何改變這種聚集我的目的一個例子嗎?

TNX :)

回答

1

您可以通過指定一個全球性的params地圖

"aggs": { 
    "testAgg": { 
     "scripted_metric": { 
     "params": { 
      "_agg": {}, 
      "param1": 10, 
      "param2": 20 
     }, 
     "init_script": "_agg['maximum'] = []", 
     "map_script": "max = 0; for(tv in _source.tvs){ if(tv.att1>= param1 && tv.attr2 <= param2 && tv.att3 > max){max = tv.att3; }}; _agg.maximum.add(max);", 
     "combine_script": "sum = 0; for (m in _agg.maximum) { sum += m }; return sum;", 
     "reduce_script": "sum = 0; for (a in _aggs) { sum += a }; return sum;" 
     } 
    } 
    } 
+0

我試試這個自己做,但是當添加「PARAMS」屬性,並運行我得到這個錯誤的查詢:「原因「:{ 」type「:」script_exception「, 」reason「:」無法使用lang [groovy]運行內嵌腳本[_agg ['maximum'] = []]「, 」caused_by「:{ 」鍵入「:」missing_property_exception「, 」reason「:」沒有這樣的屬性:_agg for class: 61d7231114f6331ff30ad062a76adc50bd85e778「 } } –

+0

沒有」參數「屬性它的工作正常和正確 –

+0

你有哪個版本的ES?聚合中是否有[啓用動態腳本](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#enable-dynamic-scripting)? – Val