Skip to content

scripted_metric _agg parameter disappears if params are provided #19768

@consulthys

Description

@consulthys

When running a scripted_metric aggregation (in 2.3.x and 5.0.0-alpha4), if the user needs to specify some parameters in the params section, the implicit _agg hashmap disappears.

When running the following dummy query:

POST index/type/_search
{
  "size": 0,
  "aggs": {
    "testAgg": {
      "scripted_metric": {
        "params": {
          "param1": 10,
          "param2": 20
        },
        "init_script": "_agg['max'] = []",
        "map_script": ";",
        "combine_script": ";",
        "reduce_script": ";"
      }
    }
  }
}

One gets an error stating No such property: _agg for class: b0b7ce0d2dbb5254a703a6f4d048654c3f84857b. There are obviously no errors without the params section.

In order for this to work, one needs to re-specify the _agg implicit parameter in the params section like this:

POST index/type/_search
{
  "size": 0,
  "aggs": {
    "testAgg": {
      "scripted_metric": {
        "params": {
          "_agg": {},
          "param1": 10,
          "param2": 20
        },
        "init_script": "_agg['max'] = []",
        "map_script": ";",
        "combine_script": ";",
        "reduce_script": ";"
      }
    }
  }
}

I'm not sure if this is done on purpose, but it feels a bit counterintuitive. The official documentation (see below) states that _agg is an implicit hashmap, but doesn't state that _agg needs to be explicitly re-defined if other parameters are defined in params .

If this is not specified, the default is the equivalent of providing:

"params" : {
   "_agg" : {}
}

I think this could be easily fixed by modifying ScriptedMetricAggregatorFactory.createInternal() like this:

    Map<String, Object> params = this.params;
    if (params != null) {
        params = deepCopyParams(params, context.searchContext());
    } else {
        params = new HashMap<>();
    }
    if (!params.containsKey("_agg")) {
        params.put("_agg", new HashMap<String, Object>());
    }

Note: If this is deemed worthy, I can gladly submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions