# Can I caculate data in the sorting result?

**URL:** https://discuss.elastic.co/t/can-i-caculate-data-in-the-sorting-result/131391
**Category:** Elasticsearch
**Created:** [May 11, 2018, 4:27am UTC](https://discuss.elastic.co/t/can-i-caculate-data-in-the-sorting-result/131391 "2018-05-11T04:27:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bichdieu1011](https://avatars.discourse-cdn.com/v4/letter/b/f04885/32.png) [@bichdieu1011](https://discuss.elastic.co/u/bichdieu1011)
#### Post date: [May 11, 2018, 4:27am UTC](https://discuss.elastic.co/t/can-i-caculate-data-in-the-sorting-result/131391/1 "2018-05-11T04:27:27Z")

</div>

Hi everyone,

Now I have a problem when I want to calculate data base on the result of sorting.  
Example, I have a data below:  
{Id : 1, value : 2}  
{Id :2 , value : 1}

I wrote the query below :  
GET /\_search  
{  
"script\_fields" : {  
"test1" : {  
"script" : {  
"lang" : "painless",  
"source" : "params.t = doc['value'].value + t; return t;",  
"params" : {  
"t" : 0  
}  
}  
}  
},  
"sort":{  
"id" : "desc"  
}  
}

and the return result is :  
[ {id : 2, test1 : 3}  
{id: 1, test1 : 2}]

As my knowledge, I think the script is run before sorting run, but I want to have the result is sorting by the Id first and after that, the script to calculate "test1" will run. How can I do that?

Below is the expected result :  
[{id : 2, test1 : 1},  
{id : 1, test1 : 3}]

---

<div class="post-metadata">

### Author: ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)
#### Post date: [May 11, 2018, 5:37am UTC](https://discuss.elastic.co/t/can-i-caculate-data-in-the-sorting-result/131391/2 "2018-05-11T05:37:13Z")

</div>

HI,

Will that make any difference if sorting is done earlier or afterwards? Because id field is not modified at all by your script.

I don't think so we can move in that direction. There is a simple reason for that, Elasticsearch shards are distributed in nature. So if you have lets say 5 shards. These 5 shards can be distributed across multiple data nodes. Executing the script on data nodes makes it much more distributed as each data node only executes it over shards it holds. When you perform a sort operation, the documents are sorted on the data node first and also at the coordinating node after aggregation.

So if you want to run these scripts after sorting, you'll have to make the scripts run over coordinating nodes, which may not be a feasible option as it'll convert a potential parallel/distributed task into a serial one.

Hope it answers to your question.

---

<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: [June 8, 2018, 5:37am UTC](https://discuss.elastic.co/t/can-i-caculate-data-in-the-sorting-result/131391/3 "2018-06-08T05:37:21Z")

</div>

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