ElasticSearch use "best match" of ngram terms instead of "synonym"?
I know this question is old, but just in case...
you should be able to use the minimumShouldMatch clause on the trigram query to specify how many trigrams must match for a record to be considered a hit. you could use something like "3<75%", which means "if there 3 or less trigrams, then 100% must match. are there 4 or more trigrams, then 75% must match"
Perhaps you have already found the reason, but ali12345
is matching alice wang
because the analyzer at search time is the same one used for index time, including ngrams.
Such that:
At index time: for text alice wang
, these terms are created [ali
, lic
, ice
, ...]
At search time: for text ali12345
, these terms are created [ali
, li1
, i12
, ...]
As we can see we have a match with term ali
To avoid this problem, ElasticSearch provides the possibility to specify a different analyzer for search time. In the mapping for field name
you can add another property search_analyzer
that is normally very much similar to the main analyzer
but without an ngram tokenfilter. This would prevent [ali
, li1
, i12
] from being generated during search analysis resulting in 0 matches to alice wang
Feel free to look into more details and explanations on this page: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html