# Query\_string queries and special characters

**URL:** <https://discuss.elastic.co/t/query-string-queries-and-special-characters/13344>\
**Category:** Elasticsearch\
**Created:** [August 27, 2013, 12:13pm UTC](https://discuss.elastic.co/t/query-string-queries-and-special-characters/13344 "2013-08-27T12:13:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curt\_Kohler](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@Curt\_Kohler](https://discuss.elastic.co/u/Curt_Kohler)\
**Post date:** [August 27, 2013, 12:13pm UTC](https://discuss.elastic.co/t/query-string-queries-and-special-characters/13344/1 "2013-08-27T12:13:38Z")

</div>

We've been using query\_string queries with ElasticSearch as part of a quick  
proof of concept in the company. While we have had pretty good success, we  
have seen some things we don't understand as well. Hoping someone can  
shed some light/confirm some suspicions.

We have field (call it field1) in our content for which we defined a  
mapping with a custom analyzer (keyword tokenizer, lower case filter). The  
field has textual data (including some non-alphanum characters such as '-'  
and '/'). An example might be: Fksdj-hfge/76543-89-0. Running the  
\_analyze endpoint shows that it is being treated as one token and has been  
lowercased as expected. In the index, every document has a unique value in  
this field. We also have a default all field set on the index.

When we submit a query like this: field1:Fksdj-hfge/76543-89-0 we get no  
answers  
When I escape the '/' like this: field1:Fksdj-hfge/76543-89-0 it finds  
the document.

Based on the results, I assume that unlike match queries, query\_string  
doesn't apply the analyzer from the field being searched to the query.  
Assuming that is true, some questions:

What analyzer is used by query\_string queries to process the search string  
by default?  
Do you need to escape any special/non-alphanum character to get it to pass  
through the query parser (assuming we let it use it's default analyzer)?  
I assume the analyzer parameter on the query\_string query refers to the  
query parser's analyzer, will the query\_string query select the correct  
analyzer for the specified field once it gets past parsing the query?

Thanks,  
Curt

PS: I know I can use term queries, however we are trying to hook into an  
existing system that is providing Lucene syntax queries and were trying to  
avoid the extra development for the proof of concept.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 27, 2013, 4:45pm UTC](https://discuss.elastic.co/t/query-string-queries-and-special-characters/13344/2 "2013-08-27T16:45:33Z")

</div>

Query\_string passes the query straight through to the Lucene parser, so it  
acts just like the Lucene QueryParser. Specifically, the parser will:

1. Tokenize your query with it's own tokenizer (not the one in your  
analyzer) so as to find tokens/phrases and special characters
2. Rewrite your query to use any special operations (fuzzy, wildcard,  
etc)
3. Pass the tokens/phrases to the field's analyzer for analysis.

So to answer your question, you'll have to escape special characters (a  
full list can be found here\<[Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Escaping)  
Special Characters\>) so that the QueryParser keeps them as part of the  
token. Once the query has made it through the "query parsing" phase, the  
leftover tokens will be passed to the fields analyzer (or whatever analyzer  
you specify in your query).

-Zach

On Tuesday, August 27, 2013 8:13:38 AM UTC-4, Curt Kohler wrote:

> We've been using query\_string queries with Elasticsearch as part of a  
> quick proof of concept in the company. While we have had pretty good  
> success, we have seen some things we don't understand as well. Hoping  
> someone can shed some light/confirm some suspicions.
> 
> We have field (call it field1) in our content for which we defined a  
> mapping with a custom analyzer (keyword tokenizer, lower case filter). The  
> field has textual data (including some non-alphanum characters such as '-'  
> and '/'). An example might be: Fksdj-hfge/76543-89-0. Running the  
> \_analyze endpoint shows that it is being treated as one token and has been  
> lowercased as expected. In the index, every document has a unique value in  
> this field. We also have a default all field set on the index.
> 
> When we submit a query like this: field1:Fksdj-hfge/76543-89-0 we get no  
> answers  
> When I escape the '/' like this: field1:Fksdj-hfge/76543-89-0 it finds  
> the document.
> 
> Based on the results, I assume that unlike match queries, query\_string  
> doesn't apply the analyzer from the field being searched to the query.  
> Assuming that is true, some questions:
> 
> What analyzer is used by query\_string queries to process the search string  
> by default?  
> Do you need to escape any special/non-alphanum character to get it to pass  
> through the query parser (assuming we let it use it's default analyzer)?  
> I assume the analyzer parameter on the query\_string query refers to the  
> query parser's analyzer, will the query\_string query select the correct  
> analyzer for the specified field once it gets past parsing the query?
> 
> Thanks,  
> Curt
> 
> PS: I know I can use term queries, however we are trying to hook into an  
> existing system that is providing Lucene syntax queries and were trying to  
> avoid the extra development for the proof of concept.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:19am UTC](https://discuss.elastic.co/t/query-string-queries-and-special-characters/13344/3 "2017-07-06T02:19:30Z")

</div>


