4

I'm using ElasticSearch for letting users search through text fields, consisting of joined tag strings. The query looks like this and it works nicely:

{
    'query' : {
        'query_string' : {
            'query' : 'user query with +bool AND operators',
            'default_operator' : 'AND',
            'fields' : ['tag_string'],
            'analyzer' : 'my_analyzer'
        }
    }
}

However, I'd like to enable fuzzy matching so that British English and American spelling are covered. E.g. I'd like to get the same results for "gray" and "grey" or for "color" and "colour".

This can be done by the user by using the fuzzy operator "~" - so searching for "color~" matches both "color" and "colour". But that should be done automatically ... yet, the search query may contain bool operators and thus, may be complex.

2 Answers 2

3

You can either use the fuzzy query:

{
    "fuzzy" : { "user" : "ki" }
}

Or use the fuzziness factor in a match query. Another way to achieve what you want in your example is to use synonyms. With synonyms you can tell elasticsearch to store synonyms to your words along with the original words, e.g. gray will be stored as gray and grey.

Here is a in-depth description of the synonyms: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-synonym-tokenfilter.html

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

1 Comment

Thanks! Could you explain a bit more about how the fuzziness factor works? Are those heavy requests or is performance okay with them? Synonyms may be tedious, since we're working with 20 languages :-P
2

Another example of fuzzy search (if you gonna use it)

POST /IndexName/TypeName/_search?size=200
{
   "query": {
      "fuzzy": {
         "FieldName": {
            "value": "gray",
            "fuzziness": 2,
            "prefix_length": 1,
            "boost": 5
         }
      }
   }
}

for multi word search use fuzzy_like_this

POST /IndexName/TypeName/_search?size=200
{
   "query": {
      "fuzzy_like_this": {
         "fields": ["FieldName1","FieldName2"],
         "like_text": "user query with +bool AND operators",
         "max_query_terms": 12,
         "fuzziness": 0.5
      }
   }
}

Comments

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.