# Wildcard is not working as expected

**URL:** <https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [May 24, 2024, 4:38am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117 "2024-05-24T04:38:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkatesh\_aamanchi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkatesh_aamanchi/32/134767_2.png) [@venkatesh\_aamanchi](https://discuss.elastic.co/u/venkatesh_aamanchi)\
**Post date:** [May 24, 2024, 4:38am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/1 "2024-05-24T04:38:33Z")

</div>

Hello,

I am using "wildcard" for matching fields of type text with a specific pattern.

```auto
    "query": {
        "wildcard": {
            "description": {
                "value": "objective*",
                "case_insensitive": true,
            }
        }
    }
}

```

It should only match descriptions like "objective: To monitor...". But, it is also matching descriptions like "Target: the **objective** of ..."

Basically, its performing search for _objective_ instead of objective\*.

I tried using query\_string, regexp inplace of wildcard but they are not working.

mapping of the column is as follows:

```auto
"description": {
    "fields": {
        "keyword": {
            "ignore_above": 256,
            "type": "keyword"
       }
    },
    "type": "text"
}

```

---

<div class="post-metadata">

**Author:** ![piergm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piergm/32/130921_2.png) [@piergm](https://discuss.elastic.co/u/piergm)\
**Post date:** [May 24, 2024, 6:47am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/2 "2024-05-24T06:47:52Z")

</div>

Hello @venkatesh_aamanchi,  
I think in this case you'll need to use the prefix query ([see docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html)). With the prefix query you can search a field that starts with `objective`.  
Your query should look something like:

````auto
GET /_search
{
  "query": {
    "prefix": {
      "description": {
        "value": "objective",
        "case_insensitive": true
      }
    }
  }
}```
````

---

<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:** [May 24, 2024, 8:15am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/3 "2024-05-24T08:15:36Z")

</div>

You need to search within the description.keyword field instead. As the description field is analyzed and broken into tokens.

---

<div class="post-metadata">

**Author:** ![venkatesh\_aamanchi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkatesh_aamanchi/32/134767_2.png) [@venkatesh\_aamanchi](https://discuss.elastic.co/u/venkatesh_aamanchi)\
**Post date:** [May 24, 2024, 8:28am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/4 "2024-05-24T08:28:17Z")

</div>

Hi @piergm, Thanks for the response. "prefix" is used check against text to see if there are any words that start with the value that was provided in query. It does not work in this. But, I still tried and result is same as "_objective_".

Any idea why wildcard is not working properly in my case?

---

<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:** [May 24, 2024, 8:32am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/5 "2024-05-24T08:32:42Z")

</div>

The wildcard character means 0 or more characters of any type, so is working as expected. To get the result you want you may need to search the keyword field for `*objective:*`, but be aware that this type of wildcard queries can be very slow.

---

<div class="post-metadata">

**Author:** ![venkatesh\_aamanchi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkatesh_aamanchi/32/134767_2.png) [@venkatesh\_aamanchi](https://discuss.elastic.co/u/venkatesh_aamanchi)\
**Post date:** [May 24, 2024, 8:36am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/6 "2024-05-24T08:36:06Z")

</div>

@dadoonet The combination of wildcard with description.keyword actually worked. Thank you

---

<div class="post-metadata">

**Author:** ![venkatesh\_aamanchi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkatesh_aamanchi/32/134767_2.png) [@venkatesh\_aamanchi](https://discuss.elastic.co/u/venkatesh_aamanchi)\
**Post date:** [May 24, 2024, 8:40am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/7 "2024-05-24T08:40:56Z")

</div>

@Christian_Dahlqvist I am new to elasticsearch. But, wildcard is supposed to match the pattern right? Actually, I tried query "query\_string" and "regex" in place of wildcard, the search still yielded wrong results.

---

<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:** [May 24, 2024, 8:51am UTC](https://discuss.elastic.co/t/wildcard-is-not-working-as-expected/360117/8 "2024-05-24T08:51:33Z")

</div>

It will depend onthe mapping of the field. If you are querying a field mapped as `text` the tokens will be indexed individually. I believe the default analyser will remove certain characters, so `objective:` will be indexed as `objective`. If you search for `objective*` the wildcard means 0 or more characters and you get the matches you see. When you search the field mapped as `keyword` the whole string is indexed as a single token. If you now search for `objective*` you will only find matched that start with `objective` and have any number of additional characters (incuding 0 additional characters).

If you think in terms of regular expressions I believe `objective*` in a wildcard query is equivalent to `$objective.*`.
