# QueryParsingException With Certain Characters

**URL:** <https://discuss.elastic.co/t/queryparsingexception-with-certain-characters/3721>\
**Category:** Elasticsearch\
**Created:** [January 1, 2011, 5:58pm UTC](https://discuss.elastic.co/t/queryparsingexception-with-certain-characters/3721 "2011-01-01T17:58:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timscott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timscott/32/3220_2.png) [@timscott](https://discuss.elastic.co/u/timscott)\
**Post date:** [January 1, 2011, 5:58pm UTC](https://discuss.elastic.co/t/queryparsingexception-with-certain-characters/3721/1 "2011-01-01T17:58:19Z")

</div>

I'm getting parsing exceptions when the user enters certain characters  
in their search. For example, the following all throw:

{"query":{"field":{"Title":{"query":"^"}}}}  
{"query":{"field":{"Title":{"query":""}}}}'  
{"query":{"field":{"Title":{"query":"!"}}}}'  
{"query":{"field":{"Title":{"query":"~"}}}}'

How do I handle this? I'm not concerned whether these actually return  
results. I'm content if the search ignores these. Or I can strip  
them out. I just don't want an exception. If I strip them, how do I  
know the full list of characters to strip?

I also tried escaping them (preceding with backslash), but it still  
threw. Should I be applying some kind of encoding?

---

<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:** [January 1, 2011, 6:16pm UTC](https://discuss.elastic.co/t/queryparsingexception-with-certain-characters/3721/2 "2011-01-01T18:16:05Z")

</div>

On Sat, 2011-01-01 at 09:58 -0800, Tim Scott wrote:

> I'm getting parsing exceptions when the user enters certain characters  
> in their search. For example, the following all throw:
> 
> {"query":{"field":{"Title":{"query":"^"}}}}  
> {"query":{"field":{"Title":{"query":""}}}}'  
> {"query":{"field":{"Title":{"query":"!"}}}}'  
> {"query":{"field":{"Title":{"query":"~"}}}}'

Yes - these are all reserved characters.

> How do I handle this? I'm not concerned whether these actually return  
> results. I'm content if the search ignores these. Or I can strip  
> them out. I just don't want an exception. If I strip them, how do I  
> know the full list of characters to strip?

You can see the list here:  
[http://lucene.apache.org/java/3\_0\_0/queryparsersyntax.html](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)

> I also tried escaping them (preceding with backslash), but it still  
> threw. Should I be applying some kind of encoding?

You can escape them with a backslash, but you probably need \ in order  
to overcome shell escaping.

Which API are you using? For the Perl API, I'm about to release a module  
which filter query strings to remove constructs which can cause errors,  
and to give the programmer some control over what is allowed through.

clint

---

<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, 4:14am UTC](https://discuss.elastic.co/t/queryparsingexception-with-certain-characters/3721/3 "2017-07-06T04:14:33Z")

</div>


