# Sorting based on a script calculated field value

**URL:** https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889
**Category:** Elasticsearch
**Created:** [May 28, 2012, 1:24pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889 "2012-05-28T13:24:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![manoj\_2](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@manoj\_2](https://discuss.elastic.co/u/manoj_2)
#### Post date: [May 28, 2012, 1:24pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889/1 "2012-05-28T13:24:56Z")

</div>

Hi Kimchy,

We want to introduce a feature to our product, I am interested to know if  
Scripting could do anything in this. We have a field which has a numeric  
value. I believe we can do math operations on a field using script. I would  
like to know, if it is possible to sort the hits based on the calculated  
script value?

Thanks in advance!  
Manoj

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [May 28, 2012, 4:17pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889/2 "2012-05-28T16:17:16Z")

</div>

On Mon, 2012-05-28 at 06:24 -0700, Manoj wrote:

> Hi Kimchy,
> 
> We want to introduce a feature to our product, I am interested to know  
> if Scripting could do anything in this. We have a field which has a  
> numeric value. I believe we can do math operations on a field using  
> script. I would like to know, if it is possible to sort the hits based  
> on the calculated script value?

yes it is.

See script based sorting on this page:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

> Thanks in advance!  
> Manoj

---

<div class="post-metadata">

### Author: ![manoj\_2](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@manoj\_2](https://discuss.elastic.co/u/manoj_2)
#### Post date: [May 29, 2012, 5:02am UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889/3 "2012-05-29T05:02:27Z")

</div>

Thanks for directing me to the link Clinton,

Is this too possible in scripting,

I have a score value, and an Id for every hit. I need to find the number  
of hits we get for every id (similar to faceting the particular id) and  
then multiply the score of every article with the count (number of hits  
with the id) of the id. And then, applying sort on the final calculated  
value

To be more clear, I have tried to explain with a sample data below

Score id number\_of\_articles\_with\_the\_id finalvalue  
5 1234 9  
9\*5=45

I am interested to know if the above criteria could be achieved in a single  
call to the index?

On Monday, May 28, 2012 9:47:16 PM UTC+5:30, Clinton Gormley wrote:

> On Mon, 2012-05-28 at 06:24 -0700, Manoj wrote:
> 
> > Hi Kimchy,
> > 
> > We want to introduce a feature to our product, I am interested to know  
> > if Scripting could do anything in this. We have a field which has a  
> > numeric value. I believe we can do math operations on a field using  
> > script. I would like to know, if it is possible to sort the hits based  
> > on the calculated script value?
> 
> yes it is.
> 
> See script based sorting on this page:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> 
> clint
> 
> > Thanks in advance!  
> > Manoj

---

<div class="post-metadata">

### Author: ![manoj\_2](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@manoj\_2](https://discuss.elastic.co/u/manoj_2)
#### Post date: [June 5, 2012, 7:19am UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889/4 "2012-06-05T07:19:39Z")

</div>

Everyone, any update about this!

On Tuesday, May 29, 2012 10:32:27 AM UTC+5:30, Manoj wrote:

> Thanks for directing me to the link Clinton,
> 
> Is this too possible in scripting,
> 
> I have a score value, and an Id for every hit. I need to find the number  
> of hits we get for every id (similar to faceting the particular id) and  
> then multiply the score of every article with the count (number of hits  
> with the id) of the id. And then, applying sort on the final calculated  
> value
> 
> To be more clear, I have tried to explain with a sample data below
> 
> Score id number\_of\_articles\_with\_the\_id finalvalue  
> 5 1234 9  
> 9\*5=45
> 
> I am interested to know if the above criteria could be achieved in a  
> single call to the index?
> 
> On Monday, May 28, 2012 9:47:16 PM UTC+5:30, Clinton Gormley wrote:
> 
> > On Mon, 2012-05-28 at 06:24 -0700, Manoj wrote:
> > 
> > > Hi Kimchy,
> > > 
> > > We want to introduce a feature to our product, I am interested to know  
> > > if Scripting could do anything in this. We have a field which has a  
> > > numeric value. I believe we can do math operations on a field using  
> > > script. I would like to know, if it is possible to sort the hits based  
> > > on the calculated script value?
> > 
> > yes it is.
> > 
> > See script based sorting on this page:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> > 
> > clint
> > 
> > > Thanks in advance!  
> > > Manoj

---

<div class="post-metadata">

### Author: ![chimpsarehungry](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chimpsarehungry](https://discuss.elastic.co/u/chimpsarehungry)
#### Post date: [April 11, 2014, 5:04pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889/5 "2014-04-11T17:04:23Z")

</div>

This is exactly what I want to do ... any update how you solved it?

---

<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: [July 6, 2017, 1:36am UTC](https://discuss.elastic.co/t/sorting-based-on-a-script-calculated-field-value/7889/6 "2017-07-06T01:36:26Z")

</div>


