Question about wildcard query

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

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

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

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.

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

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

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

Thank you
Regards
Ana

May be @jimczi or @jpountz might know?

This is a bug - I opened Wildcard queries on text fields don't obey rules for case sensitivity · Issue #71403 · elastic/elasticsearch · GitHub

3 Likes

Thank you Mark , Thank you David