Querystring search on special characters

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#*",

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

Here a similar discussion:

Thanks Gabriel.. that helps

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