# Question about wildcard query

**URL:** <https://discuss.elastic.co/t/question-about-wildcard-query/269129>\
**Category:** Elasticsearch\
**Created:** [April 2, 2021, 6:32pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129 "2021-04-02T18:32:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anyla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anyla/32/77345_2.png) [@anyla](https://discuss.elastic.co/u/anyla)\
**Post date:** [April 2, 2021, 6:32pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/1 "2021-04-02T18:32:56Z")

</div>

Hi all,  
It seems that there is something about wildcard queries that it is not clear to me  
As far as I understand, Wildcard queries are term queries so. the search term is not analysed itself and it is "compared" against the terms in the inverted index.  
So suppose that I define an index using the default mappings and I put this document

```
POST my_test/_doc/1
{
  "test_test": "I dream of butterflies instead"
}

```

and run a wildcard query (I'm just testing the behavior, I know that it is not a good practice to put a \* at the front of the search term)

```
GET my_test/_search
{
  "query": {
    "wildcard": {
      "test_test": {
        "value":"*butterflies*"
      }
    }
  }
}

```

I get one hit and it is the expected behaviour.  
But if I execute the following

```
GET my_test/_search
{
  "query": {
    "wildcard": {
      "test_test": {
        "value":"*Butterflies*"
      }
    }
  }
}

```

I also get a hit, which I did not expect to have (as far as the search term is not analysed)

What am I understanding wrong?  
Thank you very much  
Regards  
Anny

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 2, 2021, 6:59pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/2 "2021-04-02T18:59:47Z")

</div>

What is the mapping for the field? Which version of Elasticsearch are you using?

---

<div class="post-metadata">

**Author:** ![anyla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anyla/32/77345_2.png) [@anyla](https://discuss.elastic.co/u/anyla)\
**Post date:** [April 2, 2021, 7:13pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/3 "2021-04-02T19:13:04Z")

</div>

7.12, the mapping is the default mapping.  
A text field with the standard analyzer

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 2, 2021, 7:55pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/4 "2021-04-02T19:55:20Z")

</div>

If you are using the default mapping the analyzed field is lowercased, which explains the case insensitivity in your queries. If you queries `test_test.keyword` you would probably get a different result.

---

<div class="post-metadata">

**Author:** ![anyla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anyla/32/77345_2.png) [@anyla](https://discuss.elastic.co/u/anyla)\
**Post date:** [April 2, 2021, 8:41pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/5 "2021-04-02T20:41:12Z")

</div>

Hi Christian,  
Yes I understand that, what I'm not is why my search term is lower cased. It is supposed that the analyzer does not apply to the search term because is a term level query  
Thank you

---

<div class="post-metadata">

**Author:** ![anyla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anyla/32/77345_2.png) [@anyla](https://discuss.elastic.co/u/anyla)\
**Post date:** [April 3, 2021, 8:36am UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/6 "2021-04-03T08:36:07Z")

</div>

Furthermore, If I create another index with the same field but this time using a custom analyzer (standard tokenizer + stop filter), then the query with

```
GET my_test2/_search
{
  "query": {
    "wildcard": {
      "test_test": {
        "value":"*Butterflies*"
      }
      
    }
  }
}

```

Does not return any hit, which leads me to think that the search term is analyzed.

I found in this post a user with a similar situation

> [@Wildcard search case insensitive](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/2):
>
> So in short. When you index a field with a text type and default analyzer, it is indexed in lowercase and broken into tokens. Aaa Bbb became aaa, bbb. When you search with a wildcard query (which is a bad idea), you must use the same case as was indexed. So aa\* should work where Aa\* shouldn't. For keywords, they are indexed as Aaa Bbb, so searching for Aa\* will match. Note that Bb\* won't match. With a lowercase normalizer, aa\* will match. Aa\*, Bb\* or bb\* won't match.

Look at the 2nd and 4th posts. I don't know if @dadoonet found the explanation for that case

Thank you  
Regards  
Ana

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 6, 2021, 4:16pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/7 "2021-04-06T16:16:23Z")

</div>

May be @jimczi or @jpountz might know?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 7, 2021, 1:09pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/8 "2021-04-07T13:09:40Z")

</div>

This is a bug - I opened [Wildcard queries on text fields don't obey rules for case sensitivity · Issue #71403 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/71403)

---

<div class="post-metadata">

**Author:** ![anyla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anyla/32/77345_2.png) [@anyla](https://discuss.elastic.co/u/anyla)\
**Post date:** [April 7, 2021, 2:56pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/9 "2021-04-07T14:56:00Z")

</div>

Thank you Mark , Thank you David

---

<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:** [May 5, 2021, 2:56pm UTC](https://discuss.elastic.co/t/question-about-wildcard-query/269129/10 "2021-05-05T14:56:31Z")

</div>

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