# Sort by position / best match

**URL:** https://discuss.elastic.co/t/sort-by-position-best-match/106232
**Category:** Elasticsearch
**Created:** [November 2, 2017, 6:08pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232 "2017-11-02T18:08:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [November 2, 2017, 6:08pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/1 "2017-11-02T18:08:29Z")

</div>

Hello,  
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

---

<div class="post-metadata">

### Author: ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)
#### Post date: [November 2, 2017, 10:57pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/2 "2017-11-02T22:57:19Z")

</div>

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 [https://www.elastic.co/guide/en/elasticsearch/guide/current/search-in-depth.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/search-in-depth.html) and the sub-sections there so you understand what Elasticsearch is doing so you can really control it. You likely want something like [https://www.elastic.co/guide/en/elasticsearch/guide/current/proximity-relevance.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/proximity-relevance.html) where it has one block that matches terms and another block that attempts to boost an exact phrase match.

---

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [November 3, 2017, 3:11pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/3 "2017-11-03T15:11:26Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [November 4, 2017, 11:57am UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/4 "2017-11-04T11:57:52Z")

</div>

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  
etc...  
I will make a search like this in sql:  
where name = "Foo Bar Inc"

---

<div class="post-metadata">

### Author: ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)
#### Post date: [November 4, 2017, 3:08pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/5 "2017-11-04T15:08:00Z")

</div>

If you want to match a sub-phrase within a larger set of text, you'll want to use the [text](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html) field type and use the [match\_phrase](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html) query.

If you want an exact match of the field value, you'll want to use the [keyword](https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html) field type and use a [match](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) query.

If you want both to come back but have exact matches be preferred, you can use a [bool](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) 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)

---

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [November 29, 2017, 12:12pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/6 "2017-11-29T12:12:09Z")

</div>

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

---

<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: [December 27, 2017, 12:13pm UTC](https://discuss.elastic.co/t/sort-by-position-best-match/106232/7 "2017-12-27T12:13:01Z")

</div>

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