How to make a full text-query on date attribute in Elastic Search


(Simonepontz) #1

I have a document with a date type attribute (like the following)

{
  "releaseDate": {
   "time": "1990-02-01T00:00:00+0000",
  }
}

I'm confident in doing some range query (date-math etc) so that if I'm looking for all the event in 1990, I can do the following range query

{
  "query": {
    "range": {
      "releaseDate.time": {
        "gte": "1990",
        "lte": "1991"
      }
    }
  }
}

I've noticed that elastic search allow making a full-text search (match) on dates

match queries accept text/numerics/dates, analyzes them, and constructs a query. For example:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

But I've trouble understanding how to make the right query.

for example, if I search for the following match query

{
  "query": {
    "match": {
      "attribute.releaseDate.time": "1990"
    }
  }
}

Zero results are returned from ES even if the date 1990-02-01T00:00:00 exists

But if I make the following

{
  "query": {
    "match": {
      "attribute.releaseDate.time": "1990-02"
    }
  }
}

I've found my event.

My understanding is that with the search query, ES will expand my search as 1990-01-01T00:00:00+0000 that won't match the indexed value of 1990-02-01T00:00:00+0000 while in the second query the token is the same.

This will observation will lead to my questions:

There is any way to use the following query? - events occurred in 1990, dates that contain the year 1990 (without a range) - events occurred in February of any year - events occurred in February the 5th of any year

But I haven't found any working example (the only things I've seen is here https://discuss.elastic.co/t/mapping-date-field-as-multifiled-to-provide-date-query-and-full-text-search/80691)


(Christoph) #2

Hi @simonepontz,

dates are stored as long values internally, so using full text queries with them is not the standard and most useful way of working with them. The Range-Query should be your first stop when working with dates. For queries like "all events in February of each year" it is best to also index e.g. the month-of-year into a separate field and then do the search on that. Hope this helps.


(system) #3

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