# Unable to escape special characters for a query

**URL:** <https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278>\
**Category:** Elasticsearch\
**Created:** [April 9, 2012, 8:40pm UTC](https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278 "2012-04-09T20:40:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 9, 2012, 8:40pm UTC](https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278/1 "2012-04-09T20:40:23Z")

</div>

Hi all!

Sorry if this has been covered. I tried to do my searching first.

I have the query:

{"query":{"bool":{"must":[{"term":{"SID":"3962"}},{"term":{"BI":"True"}},{"query\_string":{"default\_field":"TA","query":"order-staging-541"}},{"term":{"TY":"2"}}]}}}

TA is a string field but when I search on it my query will also return any  
results that look like the TA contents with other special characters.

So, for example, "order-staging-541" also matches "order staging-541" and  
"order(staging-541".

I realize these are special characters but when I try to escape them as  
such:

{"query":{"bool":{"must":[{"term":{"SID":"3962"}},{"term":{"BI":"True"}},{"query\_string":{"default\_field":"TA","query":"order-staging-541"}},{"term":{"TY":"2"}}]}}}

It throws a syntax error. I am trying to follow the Lucene special  
characters escaping protocol laid out here  
[http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html) at  
the bottom but I'm still receiving syntax errors.

Any help would be greatly appreciated!!

---

<div class="post-metadata">

**Author:** ![atlaste](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atlaste/32/2928_2.png) [@atlaste](https://discuss.elastic.co/u/atlaste)\
**Post date:** [April 10, 2012, 6:34am UTC](https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278/2 "2012-04-10T06:34:40Z")

</div>

Funny enough I just queried on t-mobile and it seems to work:  
{"query":{"query\_string":{"default\_field":"text","query":"t\-mobile"}}}

The reason is because json also uses escape characters. Combined with your link I guess this is correct.

Kind regards,  
Stefan.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 10, 2012, 8:28pm UTC](https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278/3 "2012-04-10T20:28:03Z")

</div>

By default, string fields are analyzed in elasticasearch. It means that  
"order-staging-541" is indexed as 3 terms "order", "staging" and "541". You  
query is also analyzed, and it's also converted into 3 terms "order",  
"staging" and "541" no matter which special characters are in between.  
That's why they are matching. If you want to search this field exactly as  
it is, you should reindex it as "index":"not\_analyzed".

On Monday, April 9, 2012 4:40:23 PM UTC-4, JP Toto wrote:

> Hi all!
> 
> Sorry if this has been covered. I tried to do my searching first.
> 
> I have the query:
> 
> {"query":{"bool":{"must":[{"term":{"SID":"3962"}},{"term":{"BI":"True"}},{"query\_string":{"default\_field":"TA","query":"order-staging-541"}},{"term":{"TY":"2"}}]}}}
> 
> TA is a string field but when I search on it my query will also return any  
> results that look like the TA contents with other special characters.
> 
> So, for example, "order-staging-541" also matches "order staging-541" and  
> "order(staging-541".
> 
> I realize these are special characters but when I try to escape them as  
> such:
> 
> {"query":{"bool":{"must":[{"term":{"SID":"3962"}},{"term":{"BI":"True"}},{"query\_string":{"default\_field":"TA","query":"order-staging-541"}},{"term":{"TY":"2"}}]}}}
> 
> It throws a syntax error. I am trying to follow the Lucene special  
> characters escaping protocol laid out here  
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html) at  
> the bottom but I'm still receiving syntax errors.
> 
> Any help would be greatly appreciated!!

---

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 10, 2012, 11:11pm UTC](https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278/4 "2012-04-10T23:11:32Z")

</div>

Thanks, Igor! I came to that realization today. Got it fixed. I appreciate  
the feedback 🙂

On Tue, Apr 10, 2012 at 4:28 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> By default, string fields are analyzed in elasticasearch. It means that  
> "order-staging-541" is indexed as 3 terms "order", "staging" and "541". You  
> query is also analyzed, and it's also converted into 3 terms "order",  
> "staging" and "541" no matter which special characters are in between.  
> That's why they are matching. If you want to search this field exactly as  
> it is, you should reindex it as "index":"not\_analyzed".
> 
> On Monday, April 9, 2012 4:40:23 PM UTC-4, JP Toto wrote:
> 
> > Hi all!
> > 
> > Sorry if this has been covered. I tried to do my searching first.
> > 
> > I have the query:
> > 
> > {"query":{"bool":{"must":[{" **term":{"SID":"3962"}},{"term":**  
> > {"BI":"True"}},{"query\_string" **:{"default\_field":"TA","query"**  
> > :"order-staging-541"}},{"term"\*\*:{"TY":"2"}}]}}}
> > 
> > TA is a string field but when I search on it my query will also return  
> > any results that look like the TA contents with other special characters.
> > 
> > So, for example, "order-staging-541" also matches "order staging-541" and  
> > "order(staging-541".
> > 
> > I realize these are special characters but when I try to escape them as  
> > such:
> > 
> > {"query":{"bool":{"must":[{" **term":{"SID":"3962"}},{"term":**  
> > {"BI":"True"}},{"query\_string" **:{"default\_field":"TA","query"**  
> > :"order-staging-541"}},{"\*\*term":{"TY":"2"}}]}}}
> > 
> > It throws a syntax error. I am trying to follow the Lucene special  
> > characters escaping protocol laid out here [http://lucene.apache.org/](http://lucene.apache.org/)\*\*  
> > core/old\_versioned\_docs/\*\*versions/3\_0\_0/\*\*queryparsersyntax.html[http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html) at  
> > the bottom but I'm still receiving syntax errors.
> > 
> > Any help would be greatly appreciated!!

--  
JP Toto | [james.p.toto@gmail.com](mailto:james.p.toto@gmail.com) | [JP Toto | about.me](http://about.me/jptoto)

---

<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:** [July 6, 2017, 1:46am UTC](https://discuss.elastic.co/t/unable-to-escape-special-characters-for-a-query/7278/6 "2017-07-06T01:46:40Z")

</div>


