문제

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