Sort by position / best match

i'm searching against a elacsticsearch server and i am wondering if i can control the score of a result?

Today i do a search like:

query = {
    'query': {
      'multi_match': {
        'query': "Foo Bar Inc",
        'fields': ["name", "nickname"],

And if i have 3 records like:

  1. Bar and Foo Inc
  2. Bar Foo Ltd
  3. Foo Bar Inc

I would like to have number 3. with the highest score, how can i modify the search to do that?
Now 1 and 3 have the same score

The question you ask is deceivingly complicated.

Elasticsearch uses statistics to give a score and a variety of things, both in terms of what you index and how you search, will adjust the score. I'd really recommend you read and the sub-sections there so you understand what Elasticsearch is doing so you can really control it. You likely want something like where it has one block that matches terms and another block that attempts to boost an exact phrase match.

Is it possible to combine my search from above and match_phrase ?
So i both have the search where it searches for all terms, but also a match_phrase and then combine the score of the two results ?

And how can i ensure that my "search" will only return exact match?
Lets say i search for:

Foo Bar Inc

And i have this in my data:

Foo Bar Inc
Hmm Foo Bar Inc
Foo Bar Inc International
I will make a search like this in sql:
where name = "Foo Bar Inc"

If you want to match a sub-phrase within a larger set of text, you'll want to use the text field type and use the match_phrase query.

If you want an exact match of the field value, you'll want to use the keyword field type and use a match query.

If you want both to come back but have exact matches be preferred, you can use a bool query with an exact phrase or match on keywords in the "should" clause.

The "search in depth" and "proximity-relevance" pages walk through this (and the latter actually gives an example that's very close to what you've asked)

I have looked into a ES course, so i needed to learn a bit more about the topic.. :slight_smile:

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