# Querystring search on special characters

**URL:** <https://discuss.elastic.co/t/querystring-search-on-special-characters/192305>\
**Category:** Elasticsearch\
**Created:** [July 25, 2019, 7:22pm UTC](https://discuss.elastic.co/t/querystring-search-on-special-characters/192305 "2019-07-25T19:22:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diliprajamani1](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@diliprajamani1](https://discuss.elastic.co/u/diliprajamani1)\
**Post date:** [July 25, 2019, 7:22pm UTC](https://discuss.elastic.co/t/querystring-search-on-special-characters/192305/1 "2019-07-25T19:22:54Z")

</div>

Hello there, I am looking a weird behavior on one of my query, see if you can help.  
I am indexing the following 2 documents  
PUT ramptestindex/\_doc/360238  
{  
"OriginRamp" : {  
"RampID" : 360238,  
"RampCode" : "D!",  
"RampName" : "Kansas City Southern Ramp"  
}  
}  
PUT ramptestindex/\_doc/360237  
{  
"OriginRamp" : {  
"RampID" : 360237,  
"RampCode" : "D#",  
"RampName" : "Kansas City Southern Ramp"  
}  
}

And my query is  
GET ramptestindex/\_search  
{  
"query": {  
"query\_string": {  
"query": "D#\*",  
"analyze\_wildcard" : true,  
"default\_operator": "AND",  
"fields": [  
"OriginRamp.RampCode.keyword"  
]  
}  
}  
}

If my search is "D#" it pulls up the doc but when its "D!" it doesn't. It works if I used "simple\_query\_string" instead of a "query\_string" but I will be wildcarding on both ends of the search query that is not supported in simple\_query\_string.  
My search is on the keyword field so I am assuming analyzers are not an issue.  
I did a profile to see whats going on and the search with D! uses a BooleanQuery  
{  
"type" : "BooleanQuery",  
"description" : "+OriginRamp.RampCode.keyword:D -ConstantScore(DocValuesFieldExistsQuery [field=OriginRamp.RampCode.keyword])"

and the search with D# uses a TermQuery

"query" : [  
{  
"type" : "MultiTermQueryConstantScoreWrapper",  
"description" : "OriginRamp.RampCode.keyword:D#\*",

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [July 26, 2019, 12:13am UTC](https://discuss.elastic.co/t/querystring-search-on-special-characters/192305/2 "2019-07-26T00:13:38Z")

</div>

Hi @diliprajamani1

The problem is that query string use the lucene syntax and "!" is a special characters, that you need to escape.

> Lucene supports escaping special characters that are part of the query syntax. The current list special characters are
> 
> - 
> - && || ! ( ) { } ^ " ~ \* ? : \

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

Here a similar discussion:

> [@Escaping Special Characters in Wildcard Query](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/2):
>
> Hi Dawi The elasticsearch documentation says that "The wildcard query maps to lucene WildcardQuery". [http://www.elasticsearch.org/guide/reference/query-dsl/wildcard-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/wildcard-query.html) The Lucene documentation says that there is the following list of special characters: && || ! ( ) { } [] ^ " ~ \* ? : and that they can be escaped using the \ before the character. [http://lucene.apache.org/java/3\_4\_0/queryparsersyntax.html#Escaping%](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html#Escaping%25) 20Special%20Characters These special characters apply …

---

<div class="post-metadata">

**Author:** ![diliprajamani1](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@diliprajamani1](https://discuss.elastic.co/u/diliprajamani1)\
**Post date:** [July 26, 2019, 2:45pm UTC](https://discuss.elastic.co/t/querystring-search-on-special-characters/192305/3 "2019-07-26T14:45:08Z")

</div>

Thanks Gabriel.. that helps

---

<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:** [August 23, 2019, 2:52pm UTC](https://discuss.elastic.co/t/querystring-search-on-special-characters/192305/4 "2019-08-23T14:52:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
