# Startwith query not working if string has hyphen (-)

**URL:** <https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146>\
**Category:** Elasticsearch\
**Created:** [February 16, 2018, 10:36am UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146 "2018-02-16T10:36:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmathur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nmathur/32/21451_2.png) [@nmathur](https://discuss.elastic.co/u/nmathur)\
**Post date:** [February 16, 2018, 10:36am UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146/1 "2018-02-16T10:36:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 16, 2018, 10:50am UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146/2 "2018-02-16T10:50:30Z")

</div>

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](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![nmathur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nmathur/32/21451_2.png) [@nmathur](https://discuss.elastic.co/u/nmathur)\
**Post date:** [February 16, 2018, 11:08am UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146/3 "2018-02-16T11:08:42Z")

</div>

thanks for quick response and sorry ... your reply is not clear to me .. I created the index using [elasticsearch.net](http://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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 16, 2018, 1:32pm UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146/4 "2018-02-16T13:32:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 16, 2018, 4:14pm UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146/5 "2018-02-16T16:14:34Z")

</div>

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](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](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 16, 2018, 4:14pm UTC](https://discuss.elastic.co/t/startwith-query-not-working-if-string-has-hyphen/120146/6 "2018-03-16T16:14:43Z")

</div>

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