Boost results that starts with exact query

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:


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?

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 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.

1 Like

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

1 Like

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