Startwith query not working if string has hyphen (-)

Hello All,

I am writing a query to fetch items using start with query ... the query returns nothing if the input string has hyphen.

Below is working:
{"query":{"bool":{"must":[{"term":{"CompanyId":2}},{"query_string":{"query":"Sales*","fields":["department"]}}]}},"size":100,"timeout":"10s"}

Below is not working:
{"query":{"bool":{"must":[{"term":{"CompanyId":2}},{"query_string":{"query":"Sales-A*","fields":["department"]}}]}},"size":100,"timeout":"10s"}

What should I do to make it work?

Please share suggestions .. thanks in advance.

That's most likely a question of analyzer.

Have a look at how elasticsearch indexed your document using the _analyze API.
Note that wildcard queries are not analyzed and must exactly match with what you have in the inverted index.

If you don't succeed, please provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

thanks for quick response and sorry ... your reply is not clear to me .. I created the index using elasticsearch.net with default settings .. elasticserch-nest was returning correct results .. now I have a requirement to fetch the result through API call and facing these issue (first time working on API queries) .. I just noticed that term query is also not returning result ..

{"query":{"bool":{"must":[{"term":{"CompanyId":2}},{"term":{"department":"sales-A"}}]}},"size":50,"timeout":"10s"}

Please help.

I can't without a script I can run in Kibana Dev console. Could you build one please? Outside the context of .Net.

If you are using the default settings, then you will be using the standard
analyzer, which uses the standard tokenizer [1]. The standard tokenizer
will split terms on hyphens. In addition, you should not be using term
queries on analyzed fields, unless you know what you are doing. It would be
a good time to understand how analysis works in Elasticsearch/Lucene [2].

[1]
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-tokenizer.html
[2]
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.