Search code examples
elasticsearchelasticsearch-7

Does using fuzziness disable prefix search in a simple_query_string


When using simple_query_string with the prefix operator * and a fuzziness value ~N on the same word the prefix search appears seems to be disabled.

{
  "query": {
    "simple_query_string": {
      "query": "Abcd*~2",
      "fields": ["name"]
    }
  }
}

It's very obvious that prefix gets disabled whenever you set the fuzziness to 0 and the query becomes Abcd*~0 then there is no prefix search and no fuzziness.

This isn't mentioned in the docs so I'm not sure if I'm doing it wrong.

I've tried:

  • swapping the operator order: Abcd~2* -- in _explain this introduces fuzziness variations but omits the prefix operator
  • using parens for precedence: (Abcd*)~2 -- in _explain this uses the prefix but omits the fuzziness operator1
  • duplicating the word: (Abcd* Abcd~2) -- this works, it obviously shows the reunion of both queries instead of the composition of both effects2.

1 I'm assuming that in this case ~2 shouldn't be interpreted as the SLOP operator because there is no phrase (no quotes).

2 I can understand that compositing those effects may generate too many possible variants -- fuzzy adds 50 variants then prefix searches for each of those, that


Solution

  • As per query-string docs

    Mixing fuzzy and wildcard operators is not supported. When mixed, one of the operators is not applied. For example, you can search for app~1 (fuzzy) or app* (wildcard), but searches for app*~1 do not apply the fuzzy operator (~1).

    It considers either wildcard or fuzzy whichever is first. For Abcd~2* It is just returning all the documents