# Substituting Match Phrase Prefix Query with a MUST combination of Match Phrase and Prefix

**URL:** <https://discuss.elastic.co/t/substituting-match-phrase-prefix-query-with-a-must-combination-of-match-phrase-and-prefix/343218>\
**Category:** Elasticsearch\
**Created:** [September 18, 2023, 4:21am UTC](https://discuss.elastic.co/t/substituting-match-phrase-prefix-query-with-a-must-combination-of-match-phrase-and-prefix/343218 "2023-09-18T04:21:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliyanamu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aliyanamu/32/125740_2.png) [@aliyanamu](https://discuss.elastic.co/u/aliyanamu)\
**Post date:** [September 18, 2023, 4:21am UTC](https://discuss.elastic.co/t/substituting-match-phrase-prefix-query-with-a-must-combination-of-match-phrase-and-prefix/343218/1 "2023-09-18T04:21:20Z")

</div>

Hi,

I am using match phrase prefix for suggestion and querying search result. I'm using this for searching employee name, skill name, etc... basically name / title field which is not long.  
When I'm searching name like "john d", I'm expecting results such as ["John Doe", "John Dwayne McKinsey", ...] but no results due to the limitation of `max_expansions`.

Match phrase prefix is as the name suggest, the combination of match phrase and prefix. Hence I'm wondering if I can substitute the query with below snippet.  
2 words - 'john d'

```auto
{
  query: {
    bool: {
      must: [
        {
          match_phrase: { name: 'john' },
        },
        {
          prefix: { name: 'd' },
        }
      ]
    }
  }
}

```

3 words - 'john dwayne m'

```auto
{
  query: {
    bool: {
      must: [
        {
          match_phrase: { name: 'john dwayne' },
        },
        {
          prefix: { name: 'm' },
        }
      ]
    }
  }
}

```

or if there's only one word, only prefix is used

```auto
{
  query: {
    bool: {
      must: [
        {
          prefix: { name: 'joh' },
        }
      ]
    }
  }
}

```

I need opinion on above substitute query and whether it will have hidden issue or not?

To note:  
This works on my case where the terms/words order is somewhat unimportant. As far as I observe, the response time between `match_phrase_prefix` and above query are no different.

Thank you

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [September 19, 2023, 12:24pm UTC](https://discuss.elastic.co/t/substituting-match-phrase-prefix-query-with-a-must-combination-of-match-phrase-and-prefix/343218/2 "2023-09-19T12:24:55Z")

</div>

Hi @aliyanamu,

Welcome to the community! [From the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html) the last term of the provided text is treated as a prefix. So your examples above would behave equivalently.

> [@aliyanamu](#):
>
> the response time between `match_phrase_prefix` and above query are no different.

Have you tried running your queries through the [Search Profiler](https://www.elastic.co/guide/en/kibana/current/xpack-profiler.html) to look at the differences?

---

<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:** [October 17, 2023, 12:25pm UTC](https://discuss.elastic.co/t/substituting-match-phrase-prefix-query-with-a-must-combination-of-match-phrase-and-prefix/343218/3 "2023-10-17T12:25:40Z")

</div>

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