'ElasticSearch - searching using full text query with hyphens in words

Elasticsearch 7.16.3

Hello, I have object repository which I am indexing i ES. Some of the objects have a hyphen in their name, e.g.: a-test, b-test and so on. When searching in searchfield for objects with hyphen in name there is a problem. Suppose that a-test is some location - while typing a-te in search field we get results of all objects related to location a-test, but we do not get location a-test itself. What is displayed is, for example, a-test-some-device. The same case occurs when you type more letters in the search field, such as a-tes and even a-test. Unfortunately, changing max_expansions from the default 50 to a larger value does not help in this case. Query type which I use in my system is Match query phrase prefix.

Hence my question, is the problem with the query? I have looked through quite a few topics about hyphen in name problems, but I don't know which way I could use in my case. I have looked through the examples:

ElasticSearch - Searching with hyphens

ElasticSearch - Searching with hyphens in name

Elasticsearch: hyphen in PrefixQuery on Keyword-analyzed field

Some solutions suggest using ngram tokenizer instead of standard tokenizer, some write about configuring standard tokenizer, some write about removing hyphen using mapping char filter (and replacing hyphen "-" with underscore "_"). I'm wondering what is the best option - since there are so many of them, in case I use full text search query, specifically Match query phrase prefix (it's possible that the problem is in query type? But I doubt that swapping to keyword would help here - I don't know how searchfield would behave and what results it would offer as auto-complete - perhaps even worse). I apologize in advance for the perhaps trivial question, but this problem has been bothering me for a long time. Thanks in advance for any comments and answers!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source