# How to build an Elasticsearch query that will take into account the distance between words?

**URL:** <https://discuss.elastic.co/t/how-to-build-an-elasticsearch-query-that-will-take-into-account-the-distance-between-words/300886>\
**Category:** Elasticsearch\
**Created:** [March 28, 2022, 7:11pm UTC](https://discuss.elastic.co/t/how-to-build-an-elasticsearch-query-that-will-take-into-account-the-distance-between-words/300886 "2022-03-28T19:11:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![asil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asil/32/97607_2.png) [@asil](https://discuss.elastic.co/u/asil)\
**Post date:** [March 28, 2022, 7:11pm UTC](https://discuss.elastic.co/t/how-to-build-an-elasticsearch-query-that-will-take-into-account-the-distance-between-words/300886/1 "2022-03-28T19:11:45Z")

</div>

I'm running with `elasticsearch:7.6.2`

I have an index with 4 simple documents:

```auto
    PUT demo_idx/_doc/1
    {
      "content": "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
    }

    PUT demo_idx/_doc/2
    {
      "content": "Distributed tmp nature, simple REST APIs, speed, and scalability"
    }

    PUT demo_idx/_doc/3
    {
      "content": "Distributed nature, simple REST APIs, speed, and scalability"
    }

    PUT demo_idx/_doc/4
    {
      "content": "Distributed tmp tmp nature"
    }

```

I want to search for the text: `distributed nature` and get the following results order:

```auto
Doc id: 3 
Doc id: 1
Doc id: 2
Doc id: 4

```

i.e documents with exact match (doc 3 & doc 1) will be displayed before documents with small slop (doc 2) and documents with big slop match will be last displayed (doc 4)

I have tried the following search query:

```auto
"query": {
            "bool": {
                "must":
                    [{
                        "match_phrase": {
                            "content": {
                                "query": query,
                                "slop": 2
                            }
                        }
                    }]
            }
        }

```

But it didnt gave me the required results.

I got the following results:

```auto
Doc id: 3 ,Score: 0.22949813
Doc id: 4 ,Score: 0.15556586
Doc id: 1 ,Score: 0.15401536 
Doc id: 2 ,Score: 0.14397088

```

How can I write the query in order to get the results I want to ?

---

<div class="post-metadata">

**Author:** ![CoreyFournier](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@CoreyFournier](https://discuss.elastic.co/u/CoreyFournier)\
**Post date:** [March 28, 2022, 8:14pm UTC](https://discuss.elastic.co/t/how-to-build-an-elasticsearch-query-that-will-take-into-account-the-distance-between-words/300886/2 "2022-03-28T20:14:16Z")

</div>

The only way I can think of doing this is having many queries (ORed) with different slops set where you change the constant\_score boost to be larger when the slop is lower.

> **[Constant score query | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html)**

---

<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:** [April 25, 2022, 8:14pm UTC](https://discuss.elastic.co/t/how-to-build-an-elasticsearch-query-that-will-take-into-account-the-distance-between-words/300886/3 "2022-04-25T20:14:18Z")

</div>

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