# How to filter an index by the maximum value of a field?

**URL:** <https://discuss.elastic.co/t/how-to-filter-an-index-by-the-maximum-value-of-a-field/306755>\
**Category:** Kibana\
**Created:** [June 9, 2022, 4:02am UTC](https://discuss.elastic.co/t/how-to-filter-an-index-by-the-maximum-value-of-a-field/306755 "2022-06-09T04:02:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arifullah](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@Arifullah](https://discuss.elastic.co/u/Arifullah)\
**Post date:** [June 9, 2022, 4:02am UTC](https://discuss.elastic.co/t/how-to-filter-an-index-by-the-maximum-value-of-a-field/306755/1 "2022-06-09T04:02:37Z")

</div>

There is a field in the index by the name of date but the data of that field is not in a proper date format its only a number. So I want a filter to include only the data of maximum data of date field I mean how to create a filter which finds the maximum value of the date field and filter the index based upon that value.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 10, 2022, 1:21am UTC](https://discuss.elastic.co/t/how-to-filter-an-index-by-the-maximum-value-of-a-field/306755/2 "2022-06-10T01:21:43Z")

</div>

Terms aggregation with order on `_key` will create a bucket for sub-aggregation.

```auto
GET kibana_sample_data_flights/_search
{
  "size":0,
  "aggs": {
    "MaxFlightDelay": {
      "terms": {
        "field": "FlightDelayMin",
        "order": {"_key":"desc"},
        "size": 1
      },
      "aggs": {
        "hit": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

```

---

<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:** [July 8, 2022, 1:22am UTC](https://discuss.elastic.co/t/how-to-filter-an-index-by-the-maximum-value-of-a-field/306755/3 "2022-07-08T01:22:28Z")

</div>

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