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.

有帮助吗?

解决方案

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

其他提示

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
      }
   }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top