Sort the Results by the Result of a Script

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!

There is a big difference of running a script on a part of the resultset, like 10 documents (script field) and on the whole resultset to sort it (script sort).

The only way to speed it up is to compute at index time.

Is it possible to filter the results of the script - using post_filter?

eg: only return results where the script result: l2-distance) > 150?

If doable I’m pretty sure it will have similar behavior.

I wonder if you could do something with range fields and filter a bit the result set to reduce the number of documents to process.

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