vmeln
vmeln

Reputation: 1309

Search on date string in ElasticSearch

I have date field, which is stored with following mapping:

"RecordedDate": {
                  "type": "string",
                  "analyzer": "DateAndNumber"
               }

Here is analyzer:

 "DateAndNumber": {
                     "pattern": "([/\\-]+)",
                     "type": "pattern"
                  }

When I'm searching with following query:

"query": {
    "query_string": {
        "default_field": "RecordedDate", 
      "query": "RecordedDate:2\\/2\\/1902",
      "default_operator": "and"
}

I've got a lot of irrelevant search results which contains 2 and 1902 terms. E.g: 2/18/1902, 5/2/1902 instead of 2/2/1902.

Is there any way to make the analyzer take term position/offset into account as well?

Upvotes: 2

Views: 1388

Answers (1)

Michele Palmia
Michele Palmia

Reputation: 2462

You can set auto_generate_phrase_queries to true in the query you suggestef. You can find more details in the docs. If that doesn't work for you, you could also use a phrase query directly - a phrase query does exactly what you're asking for.

If you only have dates in your DateAndNumber field, using a date type for the field might be the best option though.

Upvotes: 1

Related Questions