Use of elasticsearches synonym filter's expand and lenient properties

279 views Asked by At

Can anyone explain with an example, In synonym filter use of expand and lenient attributes. I went through this but still, I didn't get it. Thank you

1

There are 1 answers

0
Amit On BEST ANSWER

Best way to understand Elasticsearch concepts is by testing it on some test index.

lenient true, Ignore the errors as explained in the same document, you can change the param to false and try to create the index with same request

{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "synonym": {
                        "tokenizer": "standard",
                        "filter": [
                            "my_stop",
                            "synonym"
                        ]
                    }
                },
                "filter": {
                    "my_stop": {
                        "type": "stop",
                        "stopwords": [
                            "bar"
                        ]
                    },
                    "synonym": {
                        "type": "synonym",
                        "lenient": false, // Note this
                        "synonyms": [
                            "foo, bar => baz"
                        ]
                    }
                }
            }
        }
    }
}

You will get following error from Elasticsearch

"reason": "Invalid synonym rule at line 1",
            "caused_by": {
                "type": "illegal_argument_exception",
                "reason": "term: bar was completely eliminated by analyzer"
            }

Error message is very clear, bar was added to stop words list which will be executed before synonym filter and removes bar from token filter hence it's not available for synonym filter.