# Boost results that starts with exact query

**URL:** <https://discuss.elastic.co/t/boost-results-that-starts-with-exact-query/352959>\
**Category:** Elasticsearch\
**Created:** [February 9, 2024, 4:59pm UTC](https://discuss.elastic.co/t/boost-results-that-starts-with-exact-query/352959 "2024-02-09T16:59:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MMkMkMk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmkmkmk/32/131620_2.png) [@MMkMkMk](https://discuss.elastic.co/u/MMkMkMk)\
**Post date:** [February 9, 2024, 4:59pm UTC](https://discuss.elastic.co/t/boost-results-that-starts-with-exact-query/352959/1 "2024-02-09T16:59:01Z")

</div>

Hello, I'm trying to have a full-text query that boost results that starts with the entered query.

For instance i have 2 book titles: "Viva Harry Potter" and "Harry Potter and the whatever stone".

If the user search for "harry potter" i want the second one to have a better score.

This is my query:

```auto

GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "analyzer": "italian",
            "query": "harry potter",
            "fields": [
              "post_title",
              "author"
            ]
          }
        },
        {
          "match_phrase": {
            "post_title": {
              "query": "harry potter",
              "boost": 2
            }
          }
        },
        {
          "match_phrase_prefix": {
            "post_title": {
              "query": "harry potter",
              "boost": 50
            }
          }
        },
        {
          "match_phrase": {
            "author": {
              "query": "harry potter",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

```

So, what am i doing wrong here?

---

<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:** [February 10, 2024, 9:25am UTC](https://discuss.elastic.co/t/boost-results-that-starts-with-exact-query/352959/2 "2024-02-10T09:25:46Z")

</div>

I think you assume the match phrase prefix is used to match from the beginning of the field?  
It matches _anywhere_ in the field and was a (poor) attempt at a tool for offering auto complete of the word after the one being typed.

It sounds like you want to prefer documents with the words at the beginning and for that you should look at the [span family](https://www.elastic.co/guide/en/elasticsearch/reference/current/span-queries.html) of queries, in particular the “span first” query.

You have the right idea with using bool query and boosts to organise fall-back matching options so stick with that for organising the multiple other clauses.

---

<div class="post-metadata">

**Author:** ![MMkMkMk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmkmkmk/32/131620_2.png) [@MMkMkMk](https://discuss.elastic.co/u/MMkMkMk)\
**Post date:** [February 12, 2024, 8:57am UTC](https://discuss.elastic.co/t/boost-results-that-starts-with-exact-query/352959/3 "2024-02-12T08:57:17Z")

</div>

Thank you! That was exactly what i was looking for.

---

<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:** [March 11, 2024, 8:57am UTC](https://discuss.elastic.co/t/boost-results-that-starts-with-exact-query/352959/4 "2024-03-11T08:57:19Z")

</div>

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