# Function scoring script into linear decay

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

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)

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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)