# Sort the Results by the Result of a Script

**URL:** https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973
**Category:** Elasticsearch
**Created:** [November 24, 2017, 6:45am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973 "2017-11-24T06:45:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![joshuabarth](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@joshuabarth](https://discuss.elastic.co/u/joshuabarth)
#### Post date: [November 24, 2017, 6:45am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973/1 "2017-11-24T06:45:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 24, 2017, 7:15am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973/2 "2017-11-24T07:15:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![joshuabarth](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@joshuabarth](https://discuss.elastic.co/u/joshuabarth)
#### Post date: [November 24, 2017, 10:57am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973/3 "2017-11-24T10:57:30Z")

</div>

Is it possible to filter the results of the script - using post\_filter?

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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 24, 2017, 11:53am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973/4 "2017-11-24T11:53:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 24, 2017, 11:54am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973/5 "2017-11-24T11:54:45Z")

</div>

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.

---

<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: [December 22, 2017, 11:54am UTC](https://discuss.elastic.co/t/sort-the-results-by-the-result-of-a-script/108973/6 "2017-12-22T11:54:46Z")

</div>

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