# ElasticSearch phrase search not working for wildcard search

**URL:** <https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543>\
**Category:** Elasticsearch\
**Created:** [December 6, 2022, 8:26am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543 "2022-12-06T08:26:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![golofetuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/golofetuk/32/114272_2.png) [@golofetuk](https://discuss.elastic.co/u/golofetuk)\
**Post date:** [December 6, 2022, 8:26am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/1 "2022-12-06T08:26:30Z")

</div>

I have a word index and I can't search phrases on elasticsearch. there is no result. I check tons of solutions but I can't implement them to my query.

My mapping looks like this;

```auto
PUT /words/_mapping
{
  "properties": {
    "text": {
      "type": "keyword"
    }
  }
}

```

(if the type is `text` everything worked as expected)

My elastic query looks like this;

```auto
GET /words/_search
{
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "query_string": {
                  "default_field": "text",
                  "default_operator": "AND",
                  "query": "*foo bar baz*"
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 100,
  "track_total_hits": true
}

```

But there is no data

| \_id | \_index | \_score | \_type | text |
| --- | --- | --- | --- | --- |
| | | | | |

I expect the record that has `foo bar baz` value,

| \_id | \_index | \_score | \_type | text |
| --- | --- | --- | --- | --- |
| drDzzYQBu3ncIuw4vn10 | words | 0.0 | \_doc | _ **foo bar baz** _ |

What is the problem? Could someone help?

---

<div class="post-metadata">

**Author:** ![hendry.lim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendry.lim/32/71328_2.png) [@hendry.lim](https://discuss.elastic.co/u/hendry.lim)\
**Post date:** [December 6, 2022, 8:28am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/2 "2022-12-06T08:28:46Z")

</div>

You should change your `text` field type to use text type family, e.g. `text` or `match_only_text` instead of `keyword`.

---

<div class="post-metadata">

**Author:** ![golofetuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/golofetuk/32/114272_2.png) [@golofetuk](https://discuss.elastic.co/u/golofetuk)\
**Post date:** [December 6, 2022, 8:43am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/3 "2022-12-06T08:43:45Z")

</div>

I can't change the type because the code will ship to the prod server and I can't modify any prod elastic index. it works on only word searching. Is there any solution I can do?

---

<div class="post-metadata">

**Author:** ![hendry.lim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendry.lim/32/71328_2.png) [@hendry.lim](https://discuss.elastic.co/u/hendry.lim)\
**Post date:** [December 6, 2022, 8:53am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/4 "2022-12-06T08:53:19Z")

</div>

Your query should match `text` field containing value `foo bar baz`.

Try to remove `default_operator`.

Just a suggestion, but you don't need a nested `bool > should` within `bool > filter`, unless if this query is part of a larger query.

---

<div class="post-metadata">

**Author:** ![golofetuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/golofetuk/32/114272_2.png) [@golofetuk](https://discuss.elastic.co/u/golofetuk)\
**Post date:** [December 6, 2022, 9:00am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/5 "2022-12-06T09:00:21Z")

</div>

It's a larger query 🙂 I can't share company material, so I modify the query.

My purpose is to search phrases or words `default\_operator' to help to[this](https://stackoverflow.com/questions/74652075/elasticsearch-wildcard-search-when-i-add-space-to-a-query-everything-falls-apar) .

---

<div class="post-metadata">

**Author:** ![hendry.lim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendry.lim/32/71328_2.png) [@hendry.lim](https://discuss.elastic.co/u/hendry.lim)\
**Post date:** [December 6, 2022, 9:19am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/6 "2022-12-06T09:19:13Z")

</div>

Your query string is matching by phrase because you are using `keyword` field type.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [December 6, 2022, 12:18pm UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/7 "2022-12-06T12:18:55Z")

</div>

With a keyword field you’ll need to use a wildcard query but there are some important caveats. This blog should give you all the background [Find strings within strings faster with the Elasticsearch wildcard field | Elastic Blog](https://www.elastic.co/blog/find-strings-within-strings-faster-with-the-new-elasticsearch-wildcard-field)

---

<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:** [January 3, 2023, 12:19pm UTC](https://discuss.elastic.co/t/elasticsearch-phrase-search-not-working-for-wildcard-search/320543/8 "2023-01-03T12:19:19Z")

</div>

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