# Script based sorting

**URL:** <https://discuss.elastic.co/t/script-based-sorting/3939>\
**Category:** Elasticsearch\
**Created:** [February 17, 2011, 1:44pm UTC](https://discuss.elastic.co/t/script-based-sorting/3939 "2011-02-17T13:44:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nabizan](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nabizan](https://discuss.elastic.co/u/nabizan)\
**Post date:** [February 17, 2011, 1:44pm UTC](https://discuss.elastic.co/t/script-based-sorting/3939/1 "2011-02-17T13:44:26Z")

</div>

hi lets asume that in SQL you select your data with this order

ORDER BY 1000 \* round((if (Age\>22,(Age-22),(22-Age))+1)/2)-Profil ASC

since im rewriting my search process to elasticsearch and i want to preserve  
all functionallity i have to get this right. but im going totally blind here with this  
sort order. any help would be really appreciated

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 20, 2011, 1:11am UTC](https://discuss.elastic.co/t/script-based-sorting/3939/2 "2011-02-20T01:11:13Z")

</div>

You can use sort by script ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)) to get that. But, going in the way of trying to replace an RDBMs functionality with elasticsearch is the wrong way to try and use elasticsearch.  
On Thursday, February 17, 2011 at 3:44 PM, nabizan wrote:

> hi lets asume that in SQL you select your data with this order
> 
> ORDER BY 1000 \* round((if (Age\>22,(Age-22),(22-Age))+1)/2)-Profil ASC
> 
> ## since im rewriting my search process to elasticsearch and i want to preserve all functionallity i have to get this right. but im going totally blind here with this sort order. any help would be really appreciated
> 
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/script-based-sorting-tp2519175p2519175.html](http://elasticsearch-users.115913.n3.nabble.com/script-based-sorting-tp2519175p2519175.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![nabizan](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nabizan](https://discuss.elastic.co/u/nabizan)\
**Post date:** [February 21, 2011, 7:45am UTC](https://discuss.elastic.co/t/script-based-sorting/3939/3 "2011-02-21T07:45:29Z")

</div>

okej but could you help me a little more here? i have no idea how to write this particular script sort ... i have to preserve all the functionality. thanks

---

<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, 4:11am UTC](https://discuss.elastic.co/t/script-based-sorting/3939/4 "2017-07-06T04:11:47Z")

</div>


