Elasticsearch null values aggregation

Hi ,
is null values considered a correct value for AVG aggregation in elasticsearch ?

example
a=5
b=15
c=0
avg =(5+15+0)/3
or
avg=(5+15)/2

any help pease!

1 Like

I believe the answer is easy to get:

DELETE average
PUT average/_doc/1
{
  "foo": "bar", 
  "value": 5
}
PUT average/_doc/2
{
  "foo": "bar", 
  "value": 15
}
PUT average/_doc/3
{
  "foo": "bar"
}
GET average/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      "avg": {
        "field": "value"
      }
    }
  }
}

Gives

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg" : {
      "value" : 10.0
    }
  }
}

But with value = 0 (which is not null value):

PUT average/_doc/3
{
  "foo": "bar",
  "value": 0
}
GET average/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      "avg": {
        "field": "value"
      }
    }
  }
}

This gives:

{
  "took" : 147,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg" : {
      "value" : 6.666666666666667
    }
  }
}
1 Like

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