NPE for Average based on Scripted Field with missing values

Hi,

in a kibana data table, we want add an 'average' metric, based on a scripted field. As we want to calculate the average only for a part of the dataset, we tried to rely on the default of the missing field behavior for average aggragation...

The missing parameter defines how documents that are missing a value should be treated. By default they will be ignored [...] .

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html#_missing_value

The query leaves kibana with somehting like this, where is_enabled is of type boolean and tags of type keyword, and any_numeric_field is a numeric field in the mapping.

{
  "aggs": {
    "2": {
      "aggs": {
        "5": {
          "avg": {
            "field": "any_numeric_field",
            "script": "if (!doc['is_enabled'].value) { return null; } else { if (doc['tags']?.contains('some_value')) { return 1; } else { return 0; } }"
          }
        }
      }
    }
  },
  "size": 0,
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "_source": {
    "excludes": []
  },
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}

and results in an NullPointerException in Elasticsearch

Caused by: java.lang.NullPointerException
        at org.elasticsearch.script.AggregationScript.runAsDouble(AggregationScript.java:142) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.support.ValuesSource$Numeric$WithScript$DoubleValues.advanceExact(ValuesSource.java:447) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.metrics.AvgAggregator$1.collect(AvgAggregator.java:85) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.MultiBucketCollector$MultiLeafBucketCollector.collect(MultiBucketCollector.java:200) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.bucket.BestBucketsDeferringCollector.prepareSelectedBuckets(BestBucketsDeferringCollector.java:197) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.bucket.DeferringBucketCollector.replay(DeferringBucketCollector.java:45) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.bucket.DeferableBucketAggregator.runDeferredCollections(DeferableBucketAggregator.java:103) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.bucket.terms.GlobalOrdinalsStringTermsAggregator.buildAggregation(GlobalOrdinalsStringTermsAggregator.java:233) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.aggregations.AggregationPhase.execute(AggregationPhase.java:130) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.query.QueryPhase.execute(QueryPhase.java:140) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesService.lambda$loadIntoContext$19(IndicesService.java:1335) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesService.lambda$cacheShardLevelResult$20(IndicesService.java:1392) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesRequestCache$Loader.load(IndicesRequestCache.java:174) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesRequestCache$Loader.load(IndicesRequestCache.java:157) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.cache.Cache.computeIfAbsent(Cache.java:433) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesRequestCache.getOrCompute(IndicesRequestCache.java:123) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesService.cacheShardLevelResult(IndicesService.java:1398) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.indices.IndicesService.loadIntoContext(IndicesService.java:1332) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.SearchService.loadOrExecuteQueryPhase(SearchService.java:336) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:358) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.SearchService.lambda$executeQueryPhase$1(SearchService.java:343) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.action.ActionListener.lambda$map$2(ActionListener.java:146) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:63) ~[elasticsearch-7.6.1.jar:7.6.1]
        ... 9 more

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