# How to properly escape special characters?

**URL:** <https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793>\
**Category:** Elasticsearch\
**Created:** [February 23, 2012, 6:36pm UTC](https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793 "2012-02-23T18:36:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [February 23, 2012, 6:36pm UTC](https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793/1 "2012-02-23T18:36:15Z")

</div>

I'm using 0.18.7. How do I properly escape and search on special characters  
in a query\_string query?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [February 24, 2012, 8:55am UTC](https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793/2 "2012-02-24T08:55:16Z")

</div>

On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:

> I'm using 0.18.7. How do I properly escape and search on special  
> characters in a query\_string query?

[http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_4\_0/queryparsersyntax.html#Escaping%20Special%20Characters](http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters)

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [February 25, 2012, 2:03pm UTC](https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793/3 "2012-02-25T14:03:35Z")

</div>

Thanks. I was aware of this doc but apparently you can't escape just a  
single character for the query which is what I was trying. For example, if  
the index has docs with the string [b] and you search on [ no results are  
returned. However if you search [b] results are returned. Not sure if  
this is expected?

On Friday, February 24, 2012 3:55:16 AM UTC-5, Clinton Gormley wrote:

> On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:
> 
> > I'm using 0.18.7. How do I properly escape and search on special  
> > characters in a query\_string query?
> 
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters)

On Friday, February 24, 2012 3:55:16 AM UTC-5, Clinton Gormley wrote:

> On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:
> 
> > I'm using 0.18.7. How do I properly escape and search on special  
> > characters in a query\_string query?
> 
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters)

On Friday, February 24, 2012 3:55:16 AM UTC-5, Clinton Gormley wrote:

> On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:
> 
> > I'm using 0.18.7. How do I properly escape and search on special  
> > characters in a query\_string query?
> 
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters)

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [February 25, 2012, 2:44pm UTC](https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793/4 "2012-02-25T14:44:17Z")

</div>

On Sat, 2012-02-25 at 06:03 -0800, Shane Witbeck wrote:

> Thanks. I was aware of this doc but apparently you can't escape just a  
> single character for the query which is what I was trying. For  
> example, if the index has docs with the string [b] and you search on  
> [no results are returned. However if you search [b] results are  
> returned. Not sure if this is expected?

] is also a reserved character, so both the [and] need to be escaped.  
This is nothing to do with ES. It is how the Lucene query parser works

clint

> On Friday, February 24, 2012 3:55:16 AM UTC-5, Clinton Gormley wrote:  
> On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:  
> \> I'm using 0.18.7. How do I properly escape and search on  
> special  
> \> characters in a query\_string query?
> 
> ```
> http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters
> 
> ```
> 
> On Friday, February 24, 2012 3:55:16 AM UTC-5, Clinton Gormley wrote:  
> On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:  
> \> I'm using 0.18.7. How do I properly escape and search on  
> special  
> \> characters in a query\_string query?
> 
> ```
> http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters
> 
> ```
> 
> On Friday, February 24, 2012 3:55:16 AM UTC-5, Clinton Gormley wrote:  
> On Thu, 2012-02-23 at 10:36 -0800, Shane Witbeck wrote:  
> \> I'm using 0.18.7. How do I properly escape and search on  
> special  
> \> characters in a query\_string query?
> 
> ```
> http://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters
> 
> ```

---

<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, 3:38am UTC](https://discuss.elastic.co/t/how-to-properly-escape-special-characters/6793/5 "2017-07-06T03:38:21Z")

</div>


