Elastic 7 : unable to do simple Min/Max agg with date_range type

My index document is as simple as (end_date and start_date are long) :

{
  "_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 :

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 :

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 ?

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.

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.

The full index is as simple as :

{
  "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 ?

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

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