# Function Scoring

**URL:** <https://discuss.elastic.co/t/function-scoring/14597>\
**Category:** Elasticsearch\
**Created:** [November 26, 2013, 1:31pm UTC](https://discuss.elastic.co/t/function-scoring/14597 "2013-11-26T13:31:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vinay\_Gurram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinay_gurram/32/1818_2.png) [@Vinay\_Gurram](https://discuss.elastic.co/u/Vinay_Gurram)\
**Post date:** [November 26, 2013, 1:31pm UTC](https://discuss.elastic.co/t/function-scoring/14597/1 "2013-11-26T13:31:37Z")

</div>

Hi  
I want to search for a hotel based on distance. I would like to give boost  
based on the range . Like 5 for 0-10km, 3 for 10-100km, 1 for greater than  
100km .  
I proceded with Function Score Query

"function\_score": {  
"functions": [  
{

```
        "boost_factor": "5",
        "filter": {geo Range Filter(0-10)}
    },

    {

        "boost_factor": "3",
        "filter": {geo Range Filter(10-100)}
    },{

        "boost_factor": "1",
        "filter": {geo Range Filter(100-1000)}
    },

],
"query": {...},
"score_mode": "first"

```

}

I have a few questions.

1. Does this query calculate the distance thrice for same document because  
of 3 filters? .
2. how I use decay function to implement the same functionality?

Thank you,  
Vinay

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Britta\_Weber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/britta_weber/32/1113_2.png) [@Britta\_Weber](https://discuss.elastic.co/u/Britta_Weber)\
**Post date:** [November 27, 2013, 9:29pm UTC](https://discuss.elastic.co/t/function-scoring/14597/2 "2013-11-27T21:29:46Z")

</div>

Hi Vinay,

the distance is indeed computed three times.  
You can use a decay function to achieve a continuous decay. Below is  
an example for a query that scores the docs with field value "num"  
5/3/1 with score 5/3/1 and interpolates in between. Everything below 1  
is scored as one. Unsure which kind of curve fits best in your case  
though. Also, you would still need two range filters, one for the  
documents that are all scored as 1 and one for the decay function.  
Hope that helps.

Cheers,  
Britta

POST testidx/test/1  
{  
"num": 5  
}

POST testidx/test/2  
{  
"num": 3  
}

POST testidx/test/3  
{  
"num": 1  
}  
POST testidx/test/4  
{  
"num": 0  
}

POST testidx/test/\_search  
{  
"query": {  
"function\_score": {  
"functions": [  
{  
"filter": {  
"range": {  
"num": {  
"gte": 1,  
"lte": 5  
}  
}  
},  
"linear": {  
"num": {  
"origin": 5,  
"scale": 4,  
"decay": 0.2  
}  
}  
},  
{  
"filter": {  
"range": {  
"num": {  
"lt": 1  
}  
}  
},  
"boost\_factor": 0.2  
}  
],  
"boost": 5  
}  
}  
}

On Tue, Nov 26, 2013 at 2:31 PM, Vinay Gurram  
[g.vinayvenkatakrishna@gmail.com](mailto:g.vinayvenkatakrishna@gmail.com) wrote:

> Hi  
> I want to search for a hotel based on distance. I would like to give boost  
> based on the range . Like 5 for 0-10km, 3 for 10-100km, 1 for greater than  
> 100km .  
> I proceded with Function Score Query
> 
> "function\_score": {  
> "functions": [  
> {
> 
> ```
> "boost_factor": "5",
> "filter": {geo Range Filter(0-10)}
> },
> 
> {
> 
> "boost_factor": "3",
> "filter": {geo Range Filter(10-100)}
> },{
> 
> "boost_factor": "1",
> "filter": {geo Range Filter(100-1000)}
> },
> 
> ],
> "query": {...},
> "score_mode": "first"
> 
> ```
> 
> }
> 
> I have a few questions.
> 
> 1. Does this query calculate the distance thrice for same document because  
> of 3 filters? .
> 2. how I use decay function to implement the same functionality?
> 
> Thank you,  
> Vinay
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALhJbBiXD1AgT%2BFZzKUQVN6YOYCOgq5APhvdBePtkLLb3y3waw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALhJbBiXD1AgT%2BFZzKUQVN6YOYCOgq5APhvdBePtkLLb3y3waw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:04am UTC](https://discuss.elastic.co/t/function-scoring/14597/3 "2017-07-06T02:04:25Z")

</div>


