2

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I received query malformed, no field after start_object with auto_generate_phrase_queries set to true. Also, phrase query is not the solution, somebody could search, only part of date 5/1902.
the query malformed is not related to the auto_generate clause being added: you probably forgot the outermost query field. as of the issue with part of the query - that will always be the case if you use a normal match query to match dates this way
Just put that property in wrong place, sorry. Alas that didn't help. For the match query - I also can't use it, this is case for one field, I've got a lot of ones, and I hope to fix it with analyzer/tokenizer/data type.
Just to get rid of misleading information, auto_generate_phrase_queries helped. It works as expected, there was some problems with analyzer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.