Hi
I have a query where I have a script to calculate a number - the l2-distance between a vector. I would like to sort the output.
Here is an example query:
POST /my_index/_search
{
// some filter query will be here
"script_fields" : {
"l2-distance" : {
"script" : {
"inline" : "double total = 0; for (int i = 0; i < doc['vector'].length && i < params['inVector'].length; ++i) { double nextDiff = Math.pow(doc['vector'][i] - params.inVector[i], 2);total += nextDiff; } return total;",
"lang" : "painless",
"params" : {
"inVector" : [
// the vector to calculate distance to.
// eg: 0.1,0.5,///
]
}
}
}
},
"sort" : {
"l2-distance" : {"order":"asc"}
},
"from": 0,
"size": 10
}
The sort is failing as l2-distance is not a known field. Using script sort
works but the performance is obviously terrible compared to running the script as is.
Any help would be greatly appreciated!
Thanks