# Difference of min and max aggregation for missing values

**URL:** <https://discuss.elastic.co/t/difference-of-min-and-max-aggregation-for-missing-values/283158>\
**Category:** Elasticsearch\
**Created:** [September 2, 2021, 10:53am UTC](https://discuss.elastic.co/t/difference-of-min-and-max-aggregation-for-missing-values/283158 "2021-09-02T10:53:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![str8y](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@str8y](https://discuss.elastic.co/u/str8y)\
**Post date:** [September 2, 2021, 10:53am UTC](https://discuss.elastic.co/t/difference-of-min-and-max-aggregation-for-missing-values/283158/1 "2021-09-02T10:53:18Z")

</div>

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:

```auto
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

```auto
GET /test/_search
{
  "size": 0, 
  "aggs": {
    "agg": {
      "min": {
        "field": "number"
      }
    }
  }
}

```

```auto
{
  "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.

```auto
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:

```auto
{
  "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:

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

```

```auto
{
  "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!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 30, 2021, 10:53am UTC](https://discuss.elastic.co/t/difference-of-min-and-max-aggregation-for-missing-values/283158/2 "2021-09-30T10:53:26Z")

</div>

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