# Boost relevance score based on word proximity in Elasticsearch

**URL:** <https://discuss.elastic.co/t/boost-relevance-score-based-on-word-proximity-in-elasticsearch/87883>\
**Category:** Elasticsearch\
**Created:** [June 1, 2017, 10:40am UTC](https://discuss.elastic.co/t/boost-relevance-score-based-on-word-proximity-in-elasticsearch/87883 "2017-06-01T10:40:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashit\_pupu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashit_pupu/32/101113_2.png) [@ashit\_pupu](https://discuss.elastic.co/u/ashit_pupu)\
**Post date:** [June 1, 2017, 10:40am UTC](https://discuss.elastic.co/t/boost-relevance-score-based-on-word-proximity-in-elasticsearch/87883/1 "2017-06-01T10:40:09Z")

</div>

I am working in a project where I need to provide boost based on Proximity in Elasticsearch. The Requirement states that let's say we have a field called statement in index so doc 1 has following value in statement

```
 "statement":["this is a dog",
              "it is brown in colour"
              "it is very fluffy"]

```

and doc 2 has statement as :-

```
 "statement":["this is a brown fluffy dog",
              "it plays in the garden"]

```

Let's say I do a query of "beautifull fluffy dog" then the result I am getting is both doc 1 and 2 with both having same relevance. But what I have to achieve is the doc 2 should come with higher relevance than doc 1 because in doc 2 the first statement is having fluffy and dog is same sentence, whereas in doc 1 its scattered over the values of statement. I am using phrase query with slop as 10 and have "position\_increment\_gap"as 100 in mapping. Using ES version 2.3.0

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [June 5, 2017, 8:12am UTC](https://discuss.elastic.co/t/boost-relevance-score-based-on-word-proximity-in-elasticsearch/87883/2 "2017-06-05T08:12:54Z")

</div>

We don't have queries that both boost on proximity and allow some query terms to be missing. If document 1 had `beautiful` amoung its terms, should it rank better than doc 2 because it contains all query terms, or worse because document 2 has better proximity for 2 of the query terms?

You could try to use SHOULD clauses in order to boost the score of documents based on proximity of adjacent query terms:

```auto
GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "statement": "beautiful fluffy dog"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "statement": {
              "query": "beautiful fluffy",
              "slop": 10
            }
          }
        },
        {
          "match_phrase": {
            "statement": {
              "query": "fluffy dog",
              "slop": 10
            }
          }
        }
      ]
    }
  }
}

```

Or alternatively use [rescoring](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-rescore.html) if it slows down your queries too much.

---

<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 3, 2017, 8:12am UTC](https://discuss.elastic.co/t/boost-relevance-score-based-on-word-proximity-in-elasticsearch/87883/3 "2017-07-03T08:12:57Z")

</div>

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