# Confused about query\_string and the use of wildcards

**URL:** <https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103>\
**Category:** Elasticsearch\
**Created:** [March 15, 2011, 7:52pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103 "2011-03-15T19:52:52Z")\
**Posts on this page:** 16\
**Page:** 2

<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:** [March 16, 2011, 3:29pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/21 "2011-03-16T15:29:58Z")

</div>

Hi Enrique

> All my confusion is about the expected behaviour of the wildcard  
> queries. So let's say, if my user wants to search for "iPhone" and I  
> run a query with wildcards "_iPhone_" then:

The wildcards should work as you expect them to work - from your  
example, you expect them to find any word containing "iPhone", and that  
is correct.

> 1. As it is working now, it will not analyze the search term, but just  
> use iPhone as the token itself, therefore not finding "iPhone" which  
> has a token of "iphon".

If it weren't analyzed, then the term resulting from "_iPhone_" would be  
"iPhone" not "iphone" so even case would make a difference.

> 1. As I expected it to be, i.e. the "_iPhone_" is analyzed into  
> "_iphon_" and then executed the search, and "iPhone" results are  
> returned.

This is the correct behaviour.

Try it out without the snowball stemmer:

> <https://gist.github.com/clintongormley/fa32a01ff47b2c2377d9>

You'll see that it works as you expect.

clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 16, 2011, 3:35pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/22 "2011-03-16T15:35:15Z")

</div>

Try:

{  
"query" : {  
"field" : {  
"\_all" : "_iphone_"  
}  
}  
}

and you'll see that it doesn't work either...

On Wed, Mar 16, 2011 at 4:29 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hi Enrique
> 
> > All my confusion is about the expected behaviour of the wildcard  
> > queries. So let's say, if my user wants to search for "iPhone" and I  
> > run a query with wildcards "_iPhone_" then:
> 
> The wildcards should work as you expect them to work - from your  
> example, you expect them to find any word containing "iPhone", and that  
> is correct.
> 
> > 1. As it is working now, it will not analyze the search term, but just  
> > use iPhone as the token itself, therefore not finding "iPhone" which  
> > has a token of "iphon".
> 
> If it weren't analyzed, then the term resulting from "_iPhone_" would be  
> "iPhone" not "iphone" so even case would make a difference.
> 
> > 1. As I expected it to be, i.e. the "_iPhone_" is analyzed into  
> > "_iphon_" and then executed the search, and "iPhone" results are  
> > returned.
> 
> This is the correct behaviour.
> 
> Try it out without the snowball stemmer:
> 
> [gist:fa32a01ff47b2c2377d9 · GitHub](https://gist.github.com/fa32a01ff47b2c2377d9)
> 
> You'll see that it works as you expect.
> 
> 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:** [March 16, 2011, 3:37pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/23 "2011-03-16T15:37:40Z")

</div>

> {  
> "query" : {  
> "field" : {  
> "\_all" : "_iphone_"  
> }  
> }  
> }
> 
> and you'll see that it doesn't work either...

It works for me:

# [Wed Mar 16 16:36:17 2011] Protocol: http, Server: 192.168.5.103:9200

curl -XGET '[http://127.0.0.1:9200/test/\_search?pretty=1](http://127.0.0.1:9200/test/_search?pretty=1)' -d '  
{  
"query" : {  
"field" : {  
"\_all" : "_iphone_"  
}  
}  
}  
'

# [Wed Mar 16 16:36:17 2011] Response:

# {

# "hits" : {

# "hits" : [

# {

# "\_source" : {

# "text" : "I have in iPhone"

# },

# "\_score" : 1,

# "\_index" : "test",

# "\_id" : "O9tKwG64Td2\_lRhfBm\_jag",

# "\_type" : "doc"

# }

# ],

# "max\_score" : 1,

# "total" : 1

# },

# "timed\_out" : false,

# "\_shards" : {

# "failed" : 0,

# "successful" : 5,

# "total" : 5

# },

# "took" : 2

# }

What version of ES are you using? What other settings do you have?

clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 16, 2011, 3:46pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/24 "2011-03-16T15:46:43Z")

</div>

This is ES 0.16.0.SNAPSHOT, and IMHO your example works because you didn't  
use a Spanish analyzer, but the standard one, which basically generates same  
tokens as words (it doesn't stem as Spanish does).

On Wed, Mar 16, 2011 at 4:37 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> > {  
> > "query" : {  
> > "field" : {  
> > "\_all" : "_iphone_"  
> > }  
> > }  
> > }
> > 
> > and you'll see that it doesn't work either...
> 
> It works for me:
> 
> # [Wed Mar 16 16:36:17 2011] Protocol: http, Server: 192.168.5.103:9200
> 
> curl -XGET '[http://127.0.0.1:9200/test/\_search?pretty=1](http://127.0.0.1:9200/test/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "field" : {  
> "\_all" : "_iphone_"  
> }  
> }  
> }  
> '
> 
> # [Wed Mar 16 16:36:17 2011] Response:
> 
> # {
> 
> # "hits" : {
> 
> # "hits" : [
> 
> # {
> 
> # "\_source" : {
> 
> # "text" : "I have in iPhone"
> 
> # },
> 
> # "\_score" : 1,
> 
> # "\_index" : "test",
> 
> # "\_id" : "O9tKwG64Td2\_lRhfBm\_jag",
> 
> # "\_type" : "doc"
> 
> # }
> 
> # ],
> 
> # "max\_score" : 1,
> 
> # "total" : 1
> 
> # },
> 
> # "timed\_out" : false,
> 
> # "\_shards" : {
> 
> # "failed" : 0,
> 
> # "successful" : 5,
> 
> # "total" : 5
> 
> # },
> 
> # "took" : 2
> 
> # }
> 
> What version of ES are you using? What other settings do you have?
> 
> clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 16, 2011, 3:55pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/25 "2011-03-16T15:55:04Z")

</div>

The more I think about this the more I get convinced that the issue is with  
analyzers using stemmers, and the fact that the search ignores the stemmer  
when parsing the query terms...

On Wed, Mar 16, 2011 at 4:46 PM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> This is ES 0.16.0.SNAPSHOT, and IMHO your example works because you didn't  
> use a Spanish analyzer, but the standard one, which basically generates same  
> tokens as words (it doesn't stem as Spanish does).
> 
> On Wed, Mar 16, 2011 at 4:37 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:
> 
> > > {  
> > > "query" : {  
> > > "field" : {  
> > > "\_all" : "_iphone_"  
> > > }  
> > > }  
> > > }
> > > 
> > > and you'll see that it doesn't work either...
> > 
> > It works for me:
> > 
> > # [Wed Mar 16 16:36:17 2011] Protocol: http, Server: 192.168.5.103:9200
> > 
> > curl -XGET '[http://127.0.0.1:9200/test/\_search?pretty=1](http://127.0.0.1:9200/test/_search?pretty=1)' -d '  
> > {  
> > "query" : {  
> > "field" : {  
> > "\_all" : "_iphone_"  
> > }  
> > }  
> > }  
> > '
> > 
> > # [Wed Mar 16 16:36:17 2011] Response:
> > 
> > # {
> > 
> > # "hits" : {
> > 
> > # "hits" : [
> > 
> > # {
> > 
> > # "\_source" : {
> > 
> > # "text" : "I have in iPhone"
> > 
> > # },
> > 
> > # "\_score" : 1,
> > 
> > # "\_index" : "test",
> > 
> > # "\_id" : "O9tKwG64Td2\_lRhfBm\_jag",
> > 
> > # "\_type" : "doc"
> > 
> > # }
> > 
> > # ],
> > 
> > # "max\_score" : 1,
> > 
> > # "total" : 1
> > 
> > # },
> > 
> > # "timed\_out" : false,
> > 
> > # "\_shards" : {
> > 
> > # "failed" : 0,
> > 
> > # "successful" : 5,
> > 
> > # "total" : 5
> > 
> > # },
> > 
> > # "took" : 2
> > 
> > # }
> > 
> > What version of ES are you using? What other settings do you have?
> > 
> > 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:** [March 16, 2011, 3:56pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/26 "2011-03-16T15:56:36Z")

</div>

On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:

> The more I think about this the more I get convinced that the issue is  
> with analyzers using stemmers, and the fact that the search ignores  
> the stemmer when parsing the query terms...

Exactly. That's why I said there is a bug.

clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 16, 2011, 3:59pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/27 "2011-03-16T15:59:05Z")

</div>

And just to make it more clear, the bug would be ONLY when using  
wildcards...

But I'm afraid this is a Lucene bug, but Shay can comment on it in the  
issue...

In the meantime, a workaround is to analyze the search words manually before  
composing the query, which is easy to implement but adds some hassle to the  
process, obviously.

On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> 
> > The more I think about this the more I get convinced that the issue is  
> > with analyzers using stemmers, and the fact that the search ignores  
> > the stemmer when parsing the query terms...
> 
> Exactly. That's why I said there is a bug.
> 
> clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 16, 2011, 5:40pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/28 "2011-03-16T17:40:24Z")

</div>

Hi,

Yea, wildcard text does not get analyzed when used in a query string. This is for the simple reason that analysis might produce more than one token and then it can be confusing as to where to apply the wildcard element.

Having said that, I think I saw somewhere a query parser that did analysis on wildcard. I need to find it. Maybe it can be added as an option.

-shay.banon  
On Wednesday, March 16, 2011 at 5:59 PM, Enrique Medina Montenegro wrote:

> And just to make it more clear, the bug would be ONLY when using wildcards...
> 
> But I'm afraid this is a Lucene bug, but Shay can comment on it in the issue...
> 
> In the meantime, a workaround is to analyze the search words manually before composing the query, which is easy to implement but adds some hassle to the process, obviously.
> 
> On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk) wrote:
> 
> > On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> > 
> > > The more I think about this the more I get convinced that the issue is  
> > > with analyzers using stemmers, and the fact that the search ignores  
> > > the stemmer when parsing the query terms...
> > 
> > Exactly. That's why I said there is a bug.
> > 
> > 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:** [March 16, 2011, 5:44pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/29 "2011-03-16T17:44:28Z")

</div>

> Yea, wildcard text does not get analyzed when used in a query  
> string. This is for the simple reason that analysis might produce more  
> than one token and then it can be confusing as to where to apply the  
> wildcard element.

Gaah - so after all that, I was wrong!

Apologies Enrique

clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 16, 2011, 5:50pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/30 "2011-03-16T17:50:15Z")

</div>

No apologies, that's what users' lists are made for... discussion, right?

In any case, I think that the workaround explained below in this thread  
could work fine if in need of stemmizing a wildcard search 😉

On Wed, Mar 16, 2011 at 6:44 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> > Yea, wildcard text does not get analyzed when used in a query  
> > string. This is for the simple reason that analysis might produce more  
> > than one token and then it can be confusing as to where to apply the  
> > wildcard element.
> 
> Gaah - so after all that, I was wrong!
> 
> Apologies Enrique
> 
> clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 16, 2011, 5:51pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/31 "2011-03-16T17:51:27Z")

</div>

Shay,

What would be an example of a word producing more than one token?

Thanks.

On Wed, Mar 16, 2011 at 6:40 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> Hi,
> 
> Yea, wildcard text does not get analyzed when used in a query string.  
> This is for the simple reason that analysis might produce more than one  
> token and then it can be confusing as to where to apply the wildcard  
> element.
> 
> Having said that, I think I saw somewhere a query parser that did  
> analysis on wildcard. I need to find it. Maybe it can be added as an option.
> 
> -shay.banon
> 
> On Wednesday, March 16, 2011 at 5:59 PM, Enrique Medina Montenegro wrote:
> 
> And just to make it more clear, the bug would be ONLY when using  
> wildcards...
> 
> But I'm afraid this is a Lucene bug, but Shay can comment on it in the  
> issue...
> 
> In the meantime, a workaround is to analyze the search words manually  
> before composing the query, which is easy to implement but adds some hassle  
> to the process, obviously.
> 
> On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:
> 
> On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> 
> > The more I think about this the more I get convinced that the issue is  
> > with analyzers using stemmers, and the fact that the search ignores  
> > the stemmer when parsing the query terms...
> 
> Exactly. That's why I said there is a bug.
> 
> clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 16, 2011, 7:46pm UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/32 "2011-03-16T19:46:22Z")

</div>

It really depends on the analyzer you use, and because its unknown on the query parser level, I guess it is not supported. There are analyzers that can result in multiple tokens, like ngram ones.  
On Wednesday, March 16, 2011 at 7:51 PM, Enrique Medina Montenegro wrote:

> Shay,
> 
> What would be an example of a word producing more than one token?
> 
> Thanks.
> 
> On Wed, Mar 16, 2011 at 6:40 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > Hi,
> > 
> > Yea, wildcard text does not get analyzed when used in a query string. This is for the simple reason that analysis might produce more than one token and then it can be confusing as to where to apply the wildcard element.
> > 
> > Having said that, I think I saw somewhere a query parser that did analysis on wildcard. I need to find it. Maybe it can be added as an option.
> > 
> > -shay.banon  
> > On Wednesday, March 16, 2011 at 5:59 PM, Enrique Medina Montenegro wrote:
> > 
> > > And just to make it more clear, the bug would be ONLY when using wildcards...
> > > 
> > > But I'm afraid this is a Lucene bug, but Shay can comment on it in the issue...
> > > 
> > > In the meantime, a workaround is to analyze the search words manually before composing the query, which is easy to implement but adds some hassle to the process, obviously.
> > > 
> > > On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk) wrote:
> > > 
> > > > On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> > > > 
> > > > > The more I think about this the more I get convinced that the issue is  
> > > > > with analyzers using stemmers, and the fact that the search ignores  
> > > > > the stemmer when parsing the query terms...
> > > > 
> > > > Exactly. That's why I said there is a bug.
> > > > 
> > > > clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 17, 2011, 9:06am UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/33 "2011-03-17T09:06:20Z")

</div>

As commented below in this thread, I eventually went for the workaround of  
analyzing the search terms before enclosing them between wildcards and  
executing the 'query\_string'. As my analyzer is a simple spanish snowball, I  
guess I shouldn't get more than 1 token per word, but in any case, my code  
would only pick up the first one.

So my question is: would this be a good feature to include in ES? Basically  
there could be a property/parameter in the 'query\_string' (and even in the  
'wildcard' query) that would tell ES to analyze the search terms before  
querying when they are enclosed between wildcards (or just have left or  
right wildcard). By default it would be set to false, so regular behaviour  
applies.

On Wed, Mar 16, 2011 at 8:46 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> It really depends on the analyzer you use, and because its unknown on the  
> query parser level, I guess it is not supported. There are analyzers that  
> can result in multiple tokens, like ngram ones.
> 
> On Wednesday, March 16, 2011 at 7:51 PM, Enrique Medina Montenegro wrote:
> 
> Shay,
> 
> What would be an example of a word producing more than one token?
> 
> Thanks.
> 
> On Wed, Mar 16, 2011 at 6:40 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> > Hi,
> > 
> > Yea, wildcard text does not get analyzed when used in a query string.  
> > This is for the simple reason that analysis might produce more than one  
> > token and then it can be confusing as to where to apply the wildcard  
> > element.
> > 
> > Having said that, I think I saw somewhere a query parser that did  
> > analysis on wildcard. I need to find it. Maybe it can be added as an option.
> > 
> > -shay.banon
> > 
> > On Wednesday, March 16, 2011 at 5:59 PM, Enrique Medina Montenegro wrote:
> > 
> > And just to make it more clear, the bug would be ONLY when using  
> > wildcards...
> > 
> > But I'm afraid this is a Lucene bug, but Shay can comment on it in the  
> > issue...
> > 
> > In the meantime, a workaround is to analyze the search words manually  
> > before composing the query, which is easy to implement but adds some hassle  
> > to the process, obviously.
> > 
> > On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley \<[clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)
> > 
> > > wrote:
> > 
> > On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> > 
> > > The more I think about this the more I get convinced that the issue is  
> > > with analyzers using stemmers, and the fact that the search ignores  
> > > the stemmer when parsing the query terms...
> > 
> > Exactly. That's why I said there is a bug.
> > 
> > clint

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 17, 2011, 9:59am UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/34 "2011-03-17T09:59:22Z")

</div>

Shay,

I have filed an enhancement for your consideration to allow pre-analysis of  
search terms in a wildcard 'query\_string' query:

> <https://github.com/elastic/elasticsearch/issues/787>
>
> Add a flag called \`analyze\_wildcard\` to both \`query\_string\` and \`field\` queries,… once set, a best effort will be made to analyze wildcard and prefix queries as well.
> 
> More details:
> 
> When we use an analyzer that stems terms into tokens, and then later we want to search against those analyzed terms using a wildcard, by default the search terms are not analyzed, as that analysis could lead into several tokens and the search engine would not be sure which one to use:
> 
> http://www.jguru.com/faq/view.jsp?EID=538312
> 
> However, in certain circumstances, when the liability of the search can be somehow constrained in favor of better expected results, it would be nice to tell the search engine to analyze the wildcard terms before executing the search, therefore allowing for a more precise search (at least, expected).
> 
> Let's put here an example with the Spanish analyzer (which uses the snowball stemmer):
> \- We index the phrase "I have an iPhone"
> \- We index the phrase "I love the triad iPad/iPhone/iPod"
> \- We index the phrase "I found the perfect combination: iPhone/MP3"
> 
> If we use the standard current 'query\_string', when searching for "\*phone\*", we will only get the last phrase, due to the way in which the terms have been analyzed:
> 
> "I have an iPhone":
> 
> {"tokens":\[{"token":"i","start\_offset":0,"end\_offset":1,"type":"\<ALPHANUM\>","position":1},{"token":"hav","start\_offset":2,"end\_offset":6,"type":"\<ALPHANUM\>","position":2},{"token":"an","start\_offset":7,"end\_offset":9,"type":"\<ALPHANUM\>","position":3},{"token":"iphon","start\_offset":10,"end\_offset":16,"type":"\<ALPHANUM\>","position":4}\]}
> 
> "I love the triad iPad/iPhone/iPod":
> 
> {"tokens":\[{"token":"i","start\_offset":0,"end\_offset":1,"type":"\<ALPHANUM\>","position":1},{"token":"lov","start\_offset":2,"end\_offset":6,"type":"\<ALPHANUM\>","position":2},{"token":"the","start\_offset":7,"end\_offset":10,"type":"\<ALPHANUM\>","position":3},{"token":"tri","start\_offset":11,"end\_offset":16,"type":"\<ALPHANUM\>","position":4},{"token":"ipad","start\_offset":17,"end\_offset":21,"type":"\<ALPHANUM\>","position":5},{"token":"iphon","start\_offset":22,"end\_offset":28,"type":"\<ALPHANUM\>","position":6},{"token":"ipod","start\_offset":29,"end\_offset":33,"type":"\<ALPHANUM\>","position":7}\]}
> 
> "I found the perfect combination: iPhone/MP3":
> 
> {"tokens":\[{"token":"i","start\_offset":0,"end\_offset":1,"type":"\<ALPHANUM\>","position":1},{"token":"found","start\_offset":2,"end\_offset":7,"type":"\<ALPHANUM\>","position":2},{"token":"the","start\_offset":8,"end\_offset":11,"type":"\<ALPHANUM\>","position":3},{"token":"perfect","start\_offset":12,"end\_offset":19,"type":"\<ALPHANUM\>","position":4},{"token":"combination","start\_offset":20,"end\_offset":31,"type":"\<ALPHANUM\>","position":5},{"token":"iphone/mp3","start\_offset":33,"end\_offset":43,"type":"\<NUM\>","position":6}\]}
> 
> See how the latter stems "iPhone/MP3" as "iphone/mp3"? Hence this is the only one matching a 'query\_string' equal to "\*phone\*" (and similar 'unexpected' results occur when using just one leading or trailing wildcard as well).
> 
> This result would be dissapointing for the user, as she'd expect at least something like "iPhone" or even "telephone" to be returned as a result, but due to fact that the Spanish analyzer will always remove the trailing 'e' from most of the words, it won't find them.
> 
> So enhancement would be to provide a mechanism, in the form of a parameter, for instance, in the 'query\_string', that would tell the ES query parser to analyze those search terms surrounded by wildcards (i.e. either enclosed completely, or just with a leading or trailing wildcard).
> 
> Following our previous example, a 'query\_string' for "\*phone\*" would be actually analyzed in the Spanish analyzer as "\*phon\*" therefore returning absolutely all the phrases previously created, which would be the expected and reasonable behaviour from a user's perspective. Of course, it could have some side effects on other searches, but as a parameter, it would be up to the search designer to either use it or not.

Not sure how to label it as enhancement, so left it as issue so far.

Looking forward to hearing your feedback on it.

Regards.

On Thu, Mar 17, 2011 at 10:06 AM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> As commented below in this thread, I eventually went for the workaround of  
> analyzing the search terms before enclosing them between wildcards and  
> executing the 'query\_string'. As my analyzer is a simple spanish snowball, I  
> guess I shouldn't get more than 1 token per word, but in any case, my code  
> would only pick up the first one.
> 
> So my question is: would this be a good feature to include in ES? Basically  
> there could be a property/parameter in the 'query\_string' (and even in the  
> 'wildcard' query) that would tell ES to analyze the search terms before  
> querying when they are enclosed between wildcards (or just have left or  
> right wildcard). By default it would be set to false, so regular behaviour  
> applies.
> 
> On Wed, Mar 16, 2011 at 8:46 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> > It really depends on the analyzer you use, and because its unknown on  
> > the query parser level, I guess it is not supported. There are analyzers  
> > that can result in multiple tokens, like ngram ones.
> > 
> > On Wednesday, March 16, 2011 at 7:51 PM, Enrique Medina Montenegro wrote:
> > 
> > Shay,
> > 
> > What would be an example of a word producing more than one token?
> > 
> > Thanks.
> > 
> > On Wed, Mar 16, 2011 at 6:40 PM, Shay Banon \<[shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> > 
> > > wrote:
> > 
> > > Hi,
> > > 
> > > Yea, wildcard text does not get analyzed when used in a query string.  
> > > This is for the simple reason that analysis might produce more than one  
> > > token and then it can be confusing as to where to apply the wildcard  
> > > element.
> > > 
> > > Having said that, I think I saw somewhere a query parser that did  
> > > analysis on wildcard. I need to find it. Maybe it can be added as an option.
> > > 
> > > -shay.banon
> > > 
> > > On Wednesday, March 16, 2011 at 5:59 PM, Enrique Medina Montenegro wrote:
> > > 
> > > And just to make it more clear, the bug would be ONLY when using  
> > > wildcards...
> > > 
> > > But I'm afraid this is a Lucene bug, but Shay can comment on it in the  
> > > issue...
> > > 
> > > In the meantime, a workaround is to analyze the search words manually  
> > > before composing the query, which is easy to implement but adds some hassle  
> > > to the process, obviously.
> > > 
> > > On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley \<  
> > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\> wrote:
> > > 
> > > On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> > > 
> > > > The more I think about this the more I get convinced that the issue is  
> > > > with analyzers using stemmers, and the fact that the search ignores  
> > > > the stemmer when parsing the query terms...
> > > 
> > > Exactly. That's why I said there is a bug.
> > > 
> > > clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 17, 2011, 10:13am UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/35 "2011-03-17T10:13:24Z")

</div>

Got it. Certainly possible.  
On Thursday, March 17, 2011 at 11:59 AM, Enrique Medina Montenegro wrote:

> Shay,
> 
> I have filed an enhancement for your consideration to allow pre-analysis of search terms in a wildcard 'query\_string' query:
> 
> [Query: Provide an option to analyze wildcard/prefix in query\_string / field queries · Issue #787 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/787)
> 
> Not sure how to label it as enhancement, so left it as issue so far.
> 
> Looking forward to hearing your feedback on it.
> 
> Regards.
> 
> On Thu, Mar 17, 2011 at 10:06 AM, Enrique Medina Montenegro [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com) wrote:
> 
> > As commented below in this thread, I eventually went for the workaround of analyzing the search terms before enclosing them between wildcards and executing the 'query\_string'. As my analyzer is a simple spanish snowball, I guess I shouldn't get more than 1 token per word, but in any case, my code would only pick up the first one.
> > 
> > So my question is: would this be a good feature to include in ES? Basically there could be a property/parameter in the 'query\_string' (and even in the 'wildcard' query) that would tell ES to analyze the search terms before querying when they are enclosed between wildcards (or just have left or right wildcard). By default it would be set to false, so regular behaviour applies.
> > 
> > On Wed, Mar 16, 2011 at 8:46 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > 
> > > It really depends on the analyzer you use, and because its unknown on the query parser level, I guess it is not supported. There are analyzers that can result in multiple tokens, like ngram ones.  
> > > On Wednesday, March 16, 2011 at 7:51 PM, Enrique Medina Montenegro wrote:
> > > 
> > > > Shay,
> > > > 
> > > > What would be an example of a word producing more than one token?
> > > > 
> > > > Thanks.
> > > > 
> > > > On Wed, Mar 16, 2011 at 6:40 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > Yea, wildcard text does not get analyzed when used in a query string. This is for the simple reason that analysis might produce more than one token and then it can be confusing as to where to apply the wildcard element.
> > > > > 
> > > > > Having said that, I think I saw somewhere a query parser that did analysis on wildcard. I need to find it. Maybe it can be added as an option.
> > > > > 
> > > > > -shay.banon  
> > > > > On Wednesday, March 16, 2011 at 5:59 PM, Enrique Medina Montenegro wrote:
> > > > > 
> > > > > > And just to make it more clear, the bug would be ONLY when using wildcards...
> > > > > > 
> > > > > > But I'm afraid this is a Lucene bug, but Shay can comment on it in the issue...
> > > > > > 
> > > > > > In the meantime, a workaround is to analyze the search words manually before composing the query, which is easy to implement but adds some hassle to the process, obviously.
> > > > > > 
> > > > > > On Wed, Mar 16, 2011 at 4:56 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk) wrote:
> > > > > > 
> > > > > > > On Wed, 2011-03-16 at 16:55 +0100, Enrique Medina Montenegro wrote:
> > > > > > > 
> > > > > > > > The more I think about this the more I get convinced that the issue is  
> > > > > > > > with analyzers using stemmers, and the fact that the search ignores  
> > > > > > > > the stemmer when parsing the query terms...
> > > > > > > 
> > > > > > > Exactly. That's why I said there is a bug.
> > > > > > > 
> > > > > > > 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:09am UTC](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103/36 "2017-07-06T04:09:48Z")

</div>

Closed on July 6, 2017, 4:09am UTC



[Previous page](https://discuss.elastic.co/t/confused-about-query-string-and-the-use-of-wildcards/4103.md?page=1)
