# Option to search matching phrase with post fix and prefix while maintaining the order in Elasticsearch 7.x

**URL:** <https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161>\
**Category:** Elasticsearch\
**Created:** [July 25, 2023, 9:00am UTC](https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161 "2023-07-25T09:00:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vithu](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vithu](https://discuss.elastic.co/u/Vithu)\
**Post date:** [July 25, 2023, 9:00am UTC](https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161/1 "2023-07-25T09:00:08Z")

</div>

I have a requirement to modify Elasticsearch query from 1.x to 7.x. The 1.x query is given below:

```auto
{
  "query": {
    "filtered": {
      "filter": {
        "and": {
          "filters": [
            {
              "terms": {
                "header": [
                  "target"
                ]
              }
            },
            {
              "range": {
                "date": {
                  "from": "2010-07-23",
                  "to": "2010-07-23",
                  "include_lower": true,
                  "include_upper": true
                }
              }
            },
            {
              "or": {
                "filters": [
                  {
                    "query": {
                      "match": {
                        "message": {
                          "query": "word1 word2 word3",
                          "type": "phrase",
                          "operator": "AND"
                        }
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

```

This query returns document if it contains `word1 word2 word3` and also if the document has `xxxxword1 word2 word3` or `word1 word2 word3xxxx` or `xxxword1 word2 word3xxx`.

Since the AND, OR operators support is deprecated in later Elasticsearch versions, I'm using should and must keywords and created below query to be complaint with 7.x:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "header": [
              "target"
            ]
          }
        },
        {
          "range": {
            "date": {
              "from": "2010-07-23",
              "to": "2010-07-23",
              "include_lower": true,
              "include_upper": true
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "message": {
                    "query": "word1 word2 word3",
                    "analyzer": "keyword_analyzer"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

But the new query does not return if the document contain `xxxxword1 word2 word3` or `word1 word2 word3xxxx` or `xxxword1 word2 word3xxx`.

I tried adding `"type": "bool_prefix"` to the new query. but it gives more results than expected. is there any alternative to achieve this requirement?

Also, is there any performance issue in using match\_phrase instead of multi\_match with single field.

---

<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:** [July 25, 2023, 10:12am UTC](https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161/2 "2023-07-25T10:12:05Z")

</div>

Welcome!

Is the order of the terms important for you? Like if you have `word2 word1`, it should not match?

---

<div class="post-metadata">

**Author:** ![Vithu](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vithu](https://discuss.elastic.co/u/Vithu)\
**Post date:** [July 25, 2023, 10:22am UTC](https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161/3 "2023-07-25T10:22:31Z")

</div>

yes correct. the words in the phrase should be in order.

`word2 word1` should not be matched

---

<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:** [July 25, 2023, 11:35am UTC](https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161/4 "2023-07-25T11:35:56Z")

</div>

Ok. In order to move forward, could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can **copy and paste in Kibana dev console** , click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Have a look at the [Elastic Stack and Solutions Help · Forums and Slack | Elastic](https://elastic.co/community/help) page. It contains also lot of useful information on how to ask for help.

That way, we can start from the script and try to adjust it to find a solution.

---

<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, 2023, 11:36am UTC](https://discuss.elastic.co/t/option-to-search-matching-phrase-with-post-fix-and-prefix-while-maintaining-the-order-in-elasticsearch-7-x/339161/5 "2023-08-22T11:36:43Z")

</div>

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