Elasticsearch scripted_metric null_pointer_exception

I'm trying to use the scripted_metric aggs of Elasticsearch and normally, it's working perfectly fine with my other scripts

However, with script below, I'm encountering an error called "null_pointer_exception" but they're just copy-pasted scripts and working for 6 modules already

$max = 10;

{
    "query": {
        "match_all": {}
        //omitted some queries here, so I just turned it into match_all
    }
  },
  "aggs": {
    "ARTICLE_CNT_PDAY": {
      "histogram": {
        "field": "pub_date",
        "interval": "86400"
      },
      "aggs": {
        "LATEST": {
          "nested": {
            "path": "latest"
          },
          "aggs": {
            "SUM_SVALUE": {
              "scripted_metric": {
                "init_script": "
                    state.te = [];
                    state.g = 0;
                    state.d = 0;
                    state.a = 0;
                ",
                "map_script": "
                    if(state.d != doc['_id'].value){
                        state.d = doc['_id'].value;
                        state.te.add(state.a);
                        state.g = 0;
                        state.a = 0;
                    }
                    state.a = doc['latest.soc_mm_score'].value;
                ",
                "combine_script": "
                    state.te.add(state.a);
                    double count = 0;
                    for (t in state.te) {
                        count += ((t*10)/$max)
                    }
                    return count;
                ",
                "reduce_script": "
                    double count = 0;
                    for (a in states) {
                        count += a;
                    }
                    return count;
                "
              }
            }
          }
        }
      }
    }
  }
}

I tried running this script in Kibana, and here's the error message:

Error

What I'm getting is, that there's something wrong with the reduce_script portion, tried to change this part:

FROM

for (a in states) {
    count += a;
}

TO

for (a in states) {
    count += 1;
}

And worked perfectly fine, I felt that the a variable isn't getting what it's supposed to hold

Any ideas here? Would appreciate your help, thank you very much!

The reason is explained here:

If a parent bucket of the scripted metric aggregation does not collect any documents an empty aggregation response will be returned from the shard with a null value. In this case the reduce_script's states variable will contain null as a response from that shard. reduce_script's should therefore expect and deal with null responses from shards.

So obviously one of my buckets is empty, and need to deal with that null like this:

            "reduce_script": "
                double count = 0;                    
                for (a in states) {
                    count += (a ?: 0);
                }
                return count;
            "

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.