Difference of min and max aggregation for missing values

Hi,
I'm trying to understand the difference of the min and max aggregation for documents where all field values are missing.

Let's imagine the following simple document:

PUT /test/_doc/1
{
  "name": "A",
  "number": null
}

If I want to get only the max or min value for the number field, the query in both cases results in the same -> null

GET /test/_search
{
  "size": 0, 
  "aggs": {
    "agg": {
      "min": {
        "field": "number"
      }
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "agg" : {
      "value" : null
    }
  }
}

However, if I use the min or max agg inside a terms aggregation, the result is different for min and max.

GET /test/_search
{
  "size": 0, 
  "aggs": {
    "terms": {
      "terms": {
        "field": "name.keyword"
      },
      "aggs": {
        "agg": {
          "max": {
            "field": "number"
          }
        }
      }
    }
  }
}

For max agg, the result looks similar. It contains a bucket for name value 'A' and null as the value for the max agg:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "terms" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "A",
          "doc_count" : 1,
          "agg" : {
            "value" : null
          }
        }
      ]
    }
  }
}

Whereas for the min aggregation the bucket with the name value 'A' is missing in the result completely:

GET /test/_search
{
  "size": 0, 
  "aggs": {
    "terms": {
      "terms": {
        "field": "name.keyword"
      },
      "aggs": {
        "agg": {
          "min": {
            "field": "number"
          }
        }
      }
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "terms" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ ]
    }
  }
}

Is there anyone who could give some details about that behavior?

Many thanks!!

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