# Escaping Special Characters in Wildcard Query

**URL:** <https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819>\
**Category:** Elasticsearch\
**Created:** [November 8, 2011, 8:53pm UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819 "2011-11-08T20:53:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawi/32/2439_2.png) [@dawi](https://discuss.elastic.co/u/dawi)\
**Post date:** [November 8, 2011, 8:53pm UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/1 "2011-11-08T20:53:48Z")

</div>

Hi,

my question is how to escape special characters in a wildcard query.

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%20Special%20Characters](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters)

I have tried every form of escaping I can imagine but I was not able to  
search for \* and ? using a wildcard query.

Anybody any hint or is it simply not possible?

Regards, Daniel

---

<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:** [November 9, 2011, 8:39am UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/2 "2011-11-09T08:39:11Z")

</div>

Hi Dawi

> The elasticsearch documentation says that "The wildcard query maps to  
> lucene WildcardQuery".  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](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.  
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html#Escaping%25)  
> 20Special%20Characters

These special characters apply to the query\_string/field query, not to  
the wildcard query. The only special characters in the wildcard query  
are \* and ?

> I have tried every form of escaping I can imagine but I was not able  
> to search for \* and ? using a wildcard query.

I'm guessing that the field that you are trying to search against is  
analyzed with the standard analyzer? In which case, most punctuation is  
removed, so characters like \* will not exist in your terms, and thus  
won't be searchable

Depending on what your data is, it make make sense to set your field to  
{ index: not\_analyzed}

clint

---

<div class="post-metadata">

**Author:** ![dawi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawi/32/2439_2.png) [@dawi](https://discuss.elastic.co/u/dawi)\
**Post date:** [November 9, 2011, 9:53am UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/3 "2011-11-09T09:53:37Z")

</div>

Hi Clint,

thanks for your answer!

I am not using the standard analyzer, instead I am using the  
following analyzer configuration for the index:

index:  
analysis:  
analyzer:  
default:  
tokenizer : keyword  
filter : lowercase

The following script may help to understand and reproduce my problems:

#!/bin/sh

curl -XPUT [http://localhost:9200/index/type/1](http://localhost:9200/index/type/1) -d '{ "name": "010" }'  
curl -XPUT [http://localhost:9200/index/type/2](http://localhost:9200/index/type/2) -d '{ "name": "0\*0" }'

echo  
echo "###############################################################"  
echo "term-query: one result, ok, works as expected"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "term" : { "name" : "0\*0" } }  
}'

echo "###############################################################"  
echo "wildcard-query: two results, ok, works as expected"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "wildcard" : { "name" : "0\*" } }  
}'

echo "???????????????????????????????????????????????????????????????"  
echo "wildcard-query: expecting one result, how can this be achieved???"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "wildcard" : { "name" : "0\*\*" } }  
}'

I have tried nearly any forms of escaping, and of course this could be a  
problem of shell escape sequences.  
But I don't think it is because I have the same problems using the Java API  
with wildcardQuery("name", "0\*0").

I am afraid, but is it possible that the answer is that I cannot search for

- and ? using wildcard queries?

Best Regards,  
Daniel

Am Mittwoch, 9. November 2011 09:39:11 UTC+1 schrieb Clinton Gormley:

> Hi Dawi
> 
> > The elasticsearch documentation says that "The wildcard query maps to  
> > lucene WildcardQuery".
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](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.  
> > [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html#Escaping%25)  
> > 20Special%20Characters
> 
> These special characters apply to the query\_string/field query, not to  
> the wildcard query. The only special characters in the wildcard query  
> are \* and ?
> 
> > I have tried every form of escaping I can imagine but I was not able  
> > to search for \* and ? using a wildcard query.
> 
> I'm guessing that the field that you are trying to search against is  
> analyzed with the standard analyzer? In which case, most punctuation is  
> removed, so characters like \* will not exist in your terms, and thus  
> won't be searchable
> 
> Depending on what your data is, it make make sense to set your field to  
> { index: not\_analyzed}
> 
> clint

---

<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:** [November 9, 2011, 10:56am UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/4 "2011-11-09T10:56:35Z")

</div>

Hiya

> 

> I am afraid, but is it possible that the answer is that I cannot  
> search for \* and ? using wildcard queries?

Correct.

But you can use the query\_string/field queries with \* to achieve what  
you want.

Those queries DO understand lucene query syntax

clint

> Best Regards,  
> Daniel
> 
> Am Mittwoch, 9. November 2011 09:39:11 UTC+1 schrieb Clinton Gormley:  
> 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
>     
> > 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%
> > 20Special%20Characters
>     
> These special characters apply to the query_string/field
> query, not to
> the wildcard query. The only special characters in the
> wildcard query
> are * and ?
>     
> > I have tried every form of escaping I can imagine but I was
> not able
> > to search for * and ? using a wildcard query.
>     
> I'm guessing that the field that you are trying to search
> against is
> analyzed with the standard analyzer? In which case, most
> punctuation is
> removed, so characters like * will not exist in your terms,
> and thus
> won't be searchable
>     
> Depending on what your data is, it make make sense to set your
> field to
> { index: not_analyzed}
>     
> clint
> 
> ```

---

<div class="post-metadata">

**Author:** ![dawi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawi/32/2439_2.png) [@dawi](https://discuss.elastic.co/u/dawi)\
**Post date:** [November 9, 2011, 1:43pm UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/5 "2011-11-09T13:43:43Z")

</div>

Hi Clint,

thanks for this information. Then I will use the query\_string query for my  
purpose.

Until I don't use the wildcard as first character this search behaves  
exactly as I want.

A search for 0\*0 matches document 0_0.  
A search for 0\*_ matches document 0\*0.  
A search for _10 delivers document 010.  
But  
A search for \*0 delivers both documents 010 and 00.  
A search for \* delivers both documents 010 and 0_0.

Is this behavior intended? Or is this a bug? Or am I doing something wrong?

curl -XPUT [http://localhost:9200/index/type/1](http://localhost:9200/index/type/1) -d '{ "name": "010" }'  
curl -XPUT [http://localhost:9200/index/type/2](http://localhost:9200/index/type/2) -d '{ "name": "0\*0" }'

echo  
echo "###############################################################"  
echo "wildcard-query: one result, ok, works as expected"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "query\_string" : {  
"default\_field" : "name",  
"query" : "0\*0"  
} }  
}'

echo  
echo "###############################################################"  
echo "wildcard-query: one result, ok, works as expected"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "query\_string" : {  
"default\_field" : "name",  
"query" : "0\*\*"  
} }  
}'

echo  
echo "###############################################################"  
echo "wildcard-query: one result, ok, works as expected"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "query\_string" : {  
"default\_field" : "name",  
"allow\_leading\_wildcard" : "true",  
"query" : "\*10"  
} }  
}'

echo  
echo "???????????????????????????????????????????????????????????????"  
echo "wildcard-query: one result, not ok, returns all documents"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "query\_string" : {  
"default\_field" : "name",  
"allow\_leading\_wildcard" : "true",  
"query" : "\*\*0"  
} }  
}'

echo  
echo "???????????????????????????????????????????????????????????????"  
echo "wildcard-query: one result, not ok, returns all documents"  
curl -XGET [http://localhost:9200/index/type/\_search?pretty=true](http://localhost:9200/index/type/_search?pretty=true) -d '{  
"query" : { "query\_string" : {  
"default\_field" : "name",  
"allow\_leading\_wildcard" : "true",  
"query" : "\*\*\*"  
} }  
}'

Best Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![dawi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawi/32/2439_2.png) [@dawi](https://discuss.elastic.co/u/dawi)\
**Post date:** [November 9, 2011, 2:22pm UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/6 "2011-11-09T14:22:43Z")

</div>

Hi clint,

in addition to the curl commands I have written a small java test  
class: [https://gist.github.com/1351559](https://gist.github.com/1351559)

Best regards,  
Daniel

---

<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, 3:49am UTC](https://discuss.elastic.co/t/escaping-special-characters-in-wildcard-query/5819/7 "2017-07-06T03:49:17Z")

</div>


