# Multi\_match with phrase\_prefix is not working although a token has the prefix in it

**URL:** <https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729>\
**Category:** Elastic Search\
**Created:** [July 24, 2024, 4:04pm UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729 "2024-07-24T16:04:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansun](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@ansun](https://discuss.elastic.co/u/ansun)\
**Post date:** [July 24, 2024, 4:04pm UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/1 "2024-07-24T16:04:06Z")

</div>

Hello,

I am facing a strange situation with multi\_match.  
This is the simplified multi\_match query I am trying

```auto
{
    "multi_match": {
        "query": "202201",
        "type": "phrase_prefix",
        "fields": [
          "file_name"
        ]
      }
}

```

If I query with `202201` then I see `20220101_Legal Document_5678.pdf` from the result. but if I query with `2022` I don't see the file any longer.

So I analyzed the file\_name `20220101_Legal Document_5678.pdf` and here's the tokens

```auto
{
  "tokens": [
    {
      "token": "20220101_legal",
      "start_offset": 0,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "document_5678",
      "start_offset": 15,
      "end_offset": 28,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "pdf",
      "start_offset": 29,
      "end_offset": 32,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}

```

as you see the first token starts with 2022 so it should be matched to the query with 2022. shouldn't it?  
But the result of the query with 2022 doesn't include that file. Can someone explain the reason?

Thank you for your help in advance!  
Best,  
Ansun

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [July 24, 2024, 7:44pm UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/2 "2024-07-24T19:44:02Z")

</div>

What version of Elasticsearch are you using?

In 8.14.1, I can't reproduce this, because the following snippet:

```auto
POST test/_doc
{
  "file_name": "20220101_Legal Document_5678.pdf"
}

POST test/_search
{
  "query": {
    "multi_match": {
      "query": "2022",
      "type": "phrase_prefix", 
      "fields": ["file_name"]
    }
  }
}

```

will return the expected result. Even `20` and `2` returns the expected result.

It would be helpful to provide more information - perhaps the mappings, more specific query, and version to start, and whether you still return that just further down the result set that you expect.

---

<div class="post-metadata">

**Author:** ![Musab\_Dogan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/musab_dogan/32/70691_2.png) [@Musab\_Dogan](https://discuss.elastic.co/u/Musab_Dogan)\
**Post date:** [July 24, 2024, 7:53pm UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/3 "2024-07-24T19:53:38Z")

</div>

It's because of the `max_expansions` value. By default it's set to **50**. So when you send query with longer characters (eg. "202201") you will see the results. You can increase the `max_expansion` value the but it can hurt the performance.

See my screenshot, both queries are showing the results because I have only one doc in test index.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/a/0a36fde92e496bda690f1395d8d5f02e2e6cec53.jpeg)

See the notes from official documentation: [Match phrase prefix query | Elasticsearch Guide [8.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html#match-phrase-prefix-autocomplete)

As a workaround you can choose one of the followings.

1. Increase the `max_expansion` value in your query.
2. Use [edge\_ngram tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenizer.html) - This will also tune the query speed. (recommended)
3. Use [prefix query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html) - there is no `max_expansion` limit for the prefix query but it can be slower than `phrase_prefix`.

---

<div class="post-metadata">

**Author:** ![ansun](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@ansun](https://discuss.elastic.co/u/ansun)\
**Post date:** [July 25, 2024, 7:38am UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/4 "2024-07-25T07:38:13Z")

</div>

Hi Kathleen,

Thank you for your reply!  
I am using ES 7.13.2.

The mapping for file\_name is

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

```

---

<div class="post-metadata">

**Author:** ![ansun](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@ansun](https://discuss.elastic.co/u/ansun)\
**Post date:** [July 25, 2024, 7:43am UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/5 "2024-07-25T07:43:33Z")

</div>

Hi Musab,

Thank you for your answer! Increasing max\_expansions to 100 resolved the problem. But do you have an idea why query with `2022` doesn't show the file `20220101_Legal Document_5678.pdf`? From what I understood, the filename has the exact match with the prefix `2022`, so it doesn't even need to expand the query.

Or Does ES expand the query to compare to the whole token (`20220101_legal` ) ?

---

<div class="post-metadata">

**Author:** ![ansun](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@ansun](https://discuss.elastic.co/u/ansun)\
**Post date:** [July 25, 2024, 11:34am UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/6 "2024-07-25T11:34:50Z")

</div>

Just want to answer to my question above. [Match phrase prefix query | Elasticsearch Guide [8.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html#match-phrase-prefix-autocomplete) already explains how it works. so ES expands the phrase in the query with the suggestion it fuzzies. so it means we need `20220101_Legal` to be generated with the expansion.

---

<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:** [August 22, 2024, 11:34am UTC](https://discuss.elastic.co/t/multi-match-with-phrase-prefix-is-not-working-although-a-token-has-the-prefix-in-it/363729/7 "2024-08-22T11:34:57Z")

</div>

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