# How to take word order into account in match\_phrase query

**URL:** <https://discuss.elastic.co/t/how-to-take-word-order-into-account-in-match-phrase-query/237448>\
**Category:** Elasticsearch\
**Created:** [June 17, 2020, 10:08am UTC](https://discuss.elastic.co/t/how-to-take-word-order-into-account-in-match-phrase-query/237448 "2020-06-17T10:08:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehdi89](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mehdi89/32/70522_2.png) [@mehdi89](https://discuss.elastic.co/u/mehdi89)\
**Post date:** [June 17, 2020, 10:08am UTC](https://discuss.elastic.co/t/how-to-take-word-order-into-account-in-match-phrase-query/237448/1 "2020-06-17T10:08:52Z")

</div>

Suppose that my index have two documents:

1. "get my money"
2. "my money get here"

When I do a regular match query for "get my money", both documents match correctly but they get equal scores. However, I want the order of words to be significant during scoring. In other words, I want "get my money" to have a higher score.

So I tried putting my match query inside the must clause of a bool query and included a match\_phrase (with the same query string). This seems to score hits correctly until I do a search with "how do I get my money". In that case, match\_phrase query doesn't seem to match, and the hits are returned with equal scores again.

How can I construct my index/query so that it takes word order into account but does not require all searched words to exist in document?

> Index mapping with test data

```auto
PUT test-index
{
  "mappings": {
      "properties" : {
        "keyword" : {
          "type" : "text",
          "similarity": "boolean"
        }
      }
    }
}

```

```auto
POST test-index/_doc/
{
    "keyword" : "get my money"
}
POST test-index/_doc/
{
    "keyword" : "my money get here"
}

```

> Query "How do I get my money" - Doesn't work as needed

```auto
GET /test-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "keyword": "how do i get my money"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "keyword": {
              "query": "how do i get my money"
            }
          }
        }
      ]
    }
  }
}

```

> Results (Both documents scored same)

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 3.0,
    "hits" : [
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "6Xy8wXIB3NtI_ttPGBoV",
        "_score" : 3.0,
        "_source" : {
          "keyword" : "get my money"
        }
      },
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "6ny8wXIB3NtI_ttPGBpV",
        "_score" : 3.0,
        "_source" : {
          "keyword" : "my money get here"
        }
      }
    ]
  }
}

```

Thank you

---

<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:** [July 15, 2020, 10:08am UTC](https://discuss.elastic.co/t/how-to-take-word-order-into-account-in-match-phrase-query/237448/2 "2020-07-15T10:08:56Z")

</div>

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