Search code examples
elasticsearchnestlevenshtein-distancefuzzy-search

Elastic search with fuzziness more than 2 characters (Distance)


I am trying to match text fields. I am expecting results if it has 60% plus matching.

by Fuzziness we can give only 2 distance. With this Elastic Db has record with description 'theeventsfooddrinks' and i am trying to match 'theeventsfooddrinks123', This doesn't matches.

'theeventsfooddrinks12'=> matches

'theeventsfooddri'=> Doesn't matches

'321eventsfooddrinks'=> Doesn't matches

I want elastic to match it 'eventsfooddrinks'

Any change requiring more than 2 steps is not matching


Solution

  • I think fuzzy queries are inappropriate to your case. Fuzziness is a way to solve problem of little misspellings that human can make while typing his query. Human brain can easily skip substitution of some letter in the middle of word without loosing of overall meaning of phrase. The similar behavior we expect from search engine.

    Try to use regular partial maching with ngrams analyzer:

        PUT my_index
        {
            "settings": {
                "analysis": {
                    "filter": {
                        "trigrams_filter": {
                            "type": "ngram",
                            "min_gram": 3,
                            "max_gram": 3
                        }
                    },
                    "analyzer": {
                        "trigrams": {
                            "type": "custom",
                            "tokenizer": "standard",
                            "filter": [
                                "lowercase",
                                "trigrams_filter"
                            ]
                        }
                    }
                }
            }, 
            "mappings": {
                "my_type": {
                    "properties": {
                        "my_field": {
                            "type": "text",
                            "analyzer": "trigrams"
                        }
                    }
                }
            }
        }
    
        GET my_index/my_type/_search
        {
            "query": {
                "match": {
                    "my_field": {
                        "query": "eventsfooddrinks",
                        "minimum_should_match": "60%"
                    }
                }
            }
        }