# Elastic 7 : unable to do simple Min/Max agg with date\_range type

**URL:** <https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731>\
**Category:** Elasticsearch\
**Created:** [June 3, 2024, 3:11pm UTC](https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731 "2024-06-03T15:11:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanpl](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jeanpl](https://discuss.elastic.co/u/jeanpl)\
**Post date:** [June 3, 2024, 3:11pm UTC](https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731/1 "2024-06-03T15:11:52Z")

</div>

My index document is as simple as (end\_date and start\_date are long) :

```json
{
  "_index" : "myindex_1639524591",
  "_type" : "_doc",
  "_id" : "UwtsvH0BFRn_76fpoFuN",
  "_score" : 1.0,
  "fields" : {
    "end_date" : [
      1495619822000
    ],
    "start_date" : [
      1495619822000
    ],
    ...
  }
}

```

Then, with the Java API, I just do agg, to find "min(start\_date)", and "max(end\_date)", from the returned documents. The code is as follow :

```java
MinAggregationBuilder minDateAgg = AggregationBuilders.min("min_date").field("start_date");

MaxAggregationBuilder maxDateAgg = AggregationBuilders.max("max_date").field("end_date");

```

It works.

Now, I've updated the index to drop "start\_date" and "end\_date", and use a single "dateRange" field, that has date\_range (epoch\_millis) type.

So I tried in Java :

```java
MinAggregationBuilder minDateAgg = AggregationBuilders.min("min_date").field("dateRange.gte");

MaxAggregationBuilder maxDateAgg = AggregationBuilders.max("max_date").field("dateRange.lte");

...

sourceBuilder.sort("dateRange.gte", SortOrder.ASC);

```

But I have multiples exceptions, when I try to do that agregation in Java

Does the lastest Java code version is supposed to work ?

---

<div class="post-metadata">

**Author:** ![Artem\_Shelkovnikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artem_shelkovnikov/32/134322_2.png) [@Artem\_Shelkovnikov](https://discuss.elastic.co/u/Artem_Shelkovnikov)\
**Post date:** [June 3, 2024, 3:40pm UTC](https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731/2 "2024-06-03T15:40:03Z")

</div>

Can you share the mapping for this index?

If the new fields are not described in your mappings, it would not be possible to run search/queries over these fields.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 3, 2024, 3:41pm UTC](https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731/3 "2024-06-03T15:41:08Z")

</div>

Hey

Could you share the exact mapping you have, some data you added, etc?  
The best thing to do is to reproduce first the problem using the Kibana dev console.

If it fails there, it needs to be fixed before trying to "fix it" using the Java client.

From your full reproduction script it will be easier to iterate and help you.

---

<div class="post-metadata">

**Author:** ![jeanpl](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jeanpl](https://discuss.elastic.co/u/jeanpl)\
**Post date:** [June 4, 2024, 3:53pm UTC](https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731/4 "2024-06-04T15:53:11Z")

</div>

The full index is as simple as :

```auto
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "_routing": {
      "required": true
    },
    "properties": {

      "keys" : { "type": "keyword", "index": false },
      "rangeDate": { "type": "date_range", "format": "epoch_millis"},

    }
  }
}

```

The only specifics is that I use routing, and the rangeDate field, that has a date\_range type.

When I query with Java, of course, I set the routing values properly.

The problem arise with aggregations, Elastic 7 seems unable to just do a simple "min" of the dateRange.gte field, and "max" of the dateRange.lte field (exceptions occurs).

Could you confirm, the following Java code is supposed to be correct ?

```java
MinAggregationBuilder minDateAgg = AggregationBuilders.min("min_date").field("dateRange.gte");

MaxAggregationBuilder maxDateAgg = AggregationBuilders.max("max_date").field("dateRange.lte");

...

sourceBuilder.sort("dateRange.gte", SortOrder.ASC);

```

Thank you

---

<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 2, 2024, 3:53pm UTC](https://discuss.elastic.co/t/elastic-7-unable-to-do-simple-min-max-agg-with-date-range-type/360731/5 "2024-07-02T15:53:39Z")

</div>

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