Date Range Search based on Oldest Document

Hi team,

I am writing to ask: What is the best way to construct a query to satisfy the following requirement: Date range specification in the filter clause of a boolean query so that gte is based on the oldest document in the index.

Consider:

...
"filter":
{
  "range":
  {
    "CustomElasticSearchDateTime":
    {
      "gte": "OLDEST DOCUMENT", <-- (How to get the oldest document here based on the index being searched?)
      "lte": "OLDEST DOCUMENT + 6M/M",
      "relation": "WITHIN"
     }
  }
}
...

Here is how I am currently get the oldest document from the index:

{
	"size": 1,
	"_source": false,
	"sort":
	{
		"CustomElasticSearchDateTime": "asc"
	},
	"query":
	{
		"match_all": {}
	}
}

But it does not seem possible to add this query under the gte of the previous query; or, is it possible. Any help here would be much appreciated.

Thank you

You need to run 2 queries.
One to get that value and the second one using the value.

I don't think you can do this with one single query yet.

It might be possible in the future with ES|QL.

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