Kobi
Kobi

Reputation: 138127

Can Elasticsearch filter by a date range without specifying a field?

I have multiple date fields and I want to have a single date range query to filter by any of them.

For example, I may have books in my index, and each book may have a published date, edition date, print date, and the author's birth date.

The mapping is straightforward (generated using Elasticsearch.net Nest):

"printDate" : {
    "type" : "date",
    "format" : "strict_date_optional_time||epoch_millis"
},

I looked at range queries and query string ranges - both need the name of the field explicitly and don't seem to support wildcards.
For example, this doesn't find anything, but works if I use a real field name instead of "*Date":

"filter": [
  {
    "range": {
      "*Date": {
        "gte": "2010-01-01T00:00:00",
        "lte": "2015-01-01T00:00:00"
      }
    }
  }
]

I also tried placing [2010-01-01 TO 2015-01-01] in a query string, but the dates aren't parsed correctly - it also finds 2010 or 01 as part of other strings (and seemingly other dates).
Another option is to list each field under a "should" clause and specifying "minimum_should_match":1, but that will make me maintain a list of all date fields, which seems inelegant.

Is there a way of searching for a date range on all date fields?

Upvotes: 1

Views: 2124

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Try this query:

{
  "query": {
    "query_string": {
      "default_field": "*Date", 
      "query": "[2010-01-01T00:00:00 TO 2015-01-01T00:00:00]"
    }
  }
}

Upvotes: 2

Related Questions