saeed zhiany saeed zhiany - 3 months ago 28
Groovy Question

create parametric scripted_metric in elasticsearch

I want to use scripted_metric within an aggregation. I have some parametric values in my script that I want to set those per query, It is possible to create this query at all?
below an example for what I'm looking for

"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;"
}
}
}


param1 and param2 are my parametric values, how to change this aggregation for my purpose?

tnx :)

Val Val
Answer

You can do it by specifying a global params map

"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;"
      }
    }
  }
Comments