# Escaping characters in a JSON query?

**URL:** <https://discuss.elastic.co/t/escaping-characters-in-a-json-query/4815>\
**Category:** Elasticsearch\
**Created:** [July 8, 2011, 12:23am UTC](https://discuss.elastic.co/t/escaping-characters-in-a-json-query/4815 "2011-07-08T00:23:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drv\_](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@drv\_](https://discuss.elastic.co/u/drv_)\
**Post date:** [July 8, 2011, 12:23am UTC](https://discuss.elastic.co/t/escaping-characters-in-a-json-query/4815/1 "2011-07-08T00:23:56Z")

</div>

Hi all, I've been having difficulty creating a JSON query to find  
fields that have multiple colons ':' in the query string. Multiple  
colons results in a query parse failure, the console log shows  
Caused by: org.apache.lucene.queryParser.ParseException: Cannot parse  
'ends:with:colon': Encountered " ":" ": "" at line 1, column 9.

Although I can run wildcard queries or fuzzy queries, I'd like to be  
able to do an exact match on the query string. Does anyone know how  
to escape the characters in a query, or else have suggestions about  
how to work around this?

Full gist recreation: [https://gist.github.com/1070840](https://gist.github.com/1070840)

Thanks!  
scott

---

<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:** [July 8, 2011, 7:33am UTC](https://discuss.elastic.co/t/escaping-characters-in-a-json-query/4815/2 "2011-07-08T07:33:12Z")

</div>

Hi Scott

On Thu, 2011-07-07 at 17:23 -0700, drv\_ wrote:

> Hi all, I've been having difficulty creating a JSON query to find  
> fields that have multiple colons ':' in the query string. Multiple  
> colons results in a query parse failure, the console log shows  
> Caused by: org.apache.lucene.queryParser.ParseException: Cannot parse  
> 'ends:with:colon': Encountered " ":" ": "" at line 1, column 9.
> 
> Although I can run wildcard queries or fuzzy queries, I'd like to be  
> able to do an exact match on the query string. Does anyone know how  
> to escape the characters in a query, or else have suggestions about  
> how to work around this?

Yes, the lucene query string syntax, while useful for power users,  
really needs to be parsed and corrected before being sent to ES.

You have three options here:

1. I have written a Perl module Elasticsearch::QueryParser  
which will correct errors in query strings, and allow you  
to filter out particular elements (eg field: prefix) from  
your query string

[http://beta.metacpan.org/module/ElasticSearch::QueryParser](http://beta.metacpan.org/module/ElasticSearch::QueryParser)

1. You can write your own parser in the language of your choice

2. Simplest option: use the 'text' family of queries, which don't  
have the advanced features of the lucene query syntax (eg:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

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:01am UTC](https://discuss.elastic.co/t/escaping-characters-in-a-json-query/4815/3 "2017-07-06T04:01:19Z")

</div>


