# Range query with fuzzification

**URL:** <https://discuss.elastic.co/t/range-query-with-fuzzification/73987>\
**Category:** Elasticsearch\
**Created:** [February 5, 2017, 6:32pm UTC](https://discuss.elastic.co/t/range-query-with-fuzzification/73987 "2017-02-05T18:32:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra2014](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@surendra2014](https://discuss.elastic.co/u/surendra2014)\
**Post date:** [February 5, 2017, 6:32pm UTC](https://discuss.elastic.co/t/range-query-with-fuzzification/73987/1 "2017-02-05T18:32:27Z")

</div>

Hi,

Suppose you are looking for a candidates aged between 30 and 35, a very good qualified 36 year old candidate is valid as well, certainly when few candidates are found between 30 and 35.

How can I add fuzziness to range query? Also it would be better if 36 year old has smaller score on the age condition.

I'm expecting a query which results candidates in between 30 and 35 also includes 25(30-5) to 40(35+5)

"range" : {  
"age" : {  
"gt" : 30,  
"lt" : 35,  
"fuzziness" : 5  
}  
}

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 6, 2017, 5:17am UTC](https://discuss.elastic.co/t/range-query-with-fuzzification/73987/2 "2017-02-06T05:17:22Z")

</div>

You could use a [`function_score` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html) with

1. A query that limits candidates of age between 25 and 40 with a `range` query (these are the hard bound limits)
2. A decay function on age with an `origin` of 32.5 and `offset` of 2.5, and a scale, decay and decay function (gaussian, linear, etc) of your choosing. The origin and offset will ensure 30-35 are scored equally with a decay in score after those bounds.

---

<div class="post-metadata">

**Author:** ![surendra2014](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@surendra2014](https://discuss.elastic.co/u/surendra2014)\
**Post date:** [February 6, 2017, 11:43am UTC](https://discuss.elastic.co/t/range-query-with-fuzzification/73987/3 "2017-02-06T11:43:42Z")

</div>

Hi Russ,

Thanks for the solution. I am going for the second option to control the weight of decay function. That's fine for me to have same score(75% of 15) after original bounds.

```
"function_score": {
  "query": {....},
  "functions": [
    {
      "weight": 15,
      "gauss": {
        "age": {
          "origin": 32.5,
          "scale": 5,
          "offset": 2.5,
          "decay": 0.75
        }
      }
    }
  ]
}
```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 6, 2017, 11:47am UTC](https://discuss.elastic.co/t/range-query-with-fuzzification/73987/4 "2017-02-06T11:47:27Z")

</div>

No worries, happy to help 🙂 `origin` of 30 and `offset` of 5 will equally score 25-35 and gaussian decay after that.

---

<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:** [March 6, 2017, 11:47am UTC](https://discuss.elastic.co/t/range-query-with-fuzzification/73987/5 "2017-03-06T11:47:37Z")

</div>

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