# Function scoring script into linear decay

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

<div class="post-metadata">

**Author:** ![Hasham\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hasham_ahmad/32/1939_2.png) [@Hasham\_Ahmad](https://discuss.elastic.co/u/Hasham_Ahmad)\
**Post date:** [November 14, 2013, 1:23pm UTC](https://discuss.elastic.co/t/function-scoring-script-into-linear-decay/14406/1 "2013-11-14T13:23:26Z")

</div>

Hello all, I would appreciate a little help in developing a function\_score  
query based on the following data:

[https://gist.github.com/MrHash/0c04294032af78a8e485](https://gist.github.com/MrHash/0c04294032af78a8e485)

Given a starting point of Track A, how can I find all other tracks that  
occur in playlist1 (since Track A appears in that playlist at position 1)  
and order by the number after the colon? I believe its possible to do a  
string split in a script and then use a linear decay function.

The function\_score should return the following documents in this order as  
determined by their distance from origin Track A (position 1).

Track D (distance = 1)  
Track B (distance = 2)  
Track C (distance = 3)

Your advice is much appreciated. TIA,

Hash

--  
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:** ![Hasham\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hasham_ahmad/32/1939_2.png) [@Hasham\_Ahmad](https://discuss.elastic.co/u/Hasham_Ahmad)\
**Post date:** [December 1, 2013, 3:00pm UTC](https://discuss.elastic.co/t/function-scoring-script-into-linear-decay/14406/2 "2013-12-01T15:00:47Z")

</div>

Solved and presented in order to help others with the same problem.

{  
"query": {  
"function\_score": {  
"filter": {  
"bool": {  
"should": [  
{  
"prefix": {  
"positions": "playlist1"  
}  
}  
]  
}  
},  
"functions": [  
{  
"script\_score": {  
"params": {  
"tracklist": "playlist1",  
"origin": "1"  
},  
"script": "for(position : doc['positions'].values) {  
parts = position.split(':'); if(parts[0] == tracklist) { return  
abs(Integer.parseInt(parts[1])-origin); } } return 0;"  
}  
}  
],  
"boost\_mode": "replace"  
}  
},  
"sort": {  
"\_score": {  
"order": "asc"  
}  
}  
}

Positions should not not analyzed and functions and filters can be repeated  
for calculating multiple origins, perhaps with the addition of an "avg"  
score\_mode.

On Thursday, 14 November 2013 14:23:26 UTC+1, Hasham Ahmad wrote:

> Hello all, I would appreciate a little help in developing a function\_score  
> query based on the following data:
> 
> [https://gist.github.com/MrHash/0c04294032af78a8e485](https://gist.github.com/MrHash/0c04294032af78a8e485)
> 
> Given a starting point of Track A, how can I find all other tracks that  
> occur in playlist1 (since Track A appears in that playlist at position 1)  
> and order by the number after the colon? I believe its possible to do a  
> string split in a script and then use a linear decay function.
> 
> The function\_score should return the following documents in this order as  
> determined by their distance from origin Track A (position 1).
> 
> Track D (distance = 1)  
> Track B (distance = 2)  
> Track C (distance = 3)
> 
> Your advice is much appreciated. TIA,
> 
> Hash

--  
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/af45c32e-e5f5-4758-a689-ce754808626d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/af45c32e-e5f5-4758-a689-ce754808626d%40googlegroups.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-script-into-linear-decay/14406/3 "2017-07-06T02:04:00Z")

</div>

Closed on July 6, 2017, 2:04am UTC


