# Sorting by score range and date

**URL:** <https://discuss.elastic.co/t/sorting-by-score-range-and-date/13838>\
**Category:** Elasticsearch\
**Created:** [October 3, 2013, 6:27pm UTC](https://discuss.elastic.co/t/sorting-by-score-range-and-date/13838 "2013-10-03T18:27:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cliveholloway](https://avatars.discourse-cdn.com/v4/letter/c/51bf81/32.png) [@cliveholloway](https://discuss.elastic.co/u/cliveholloway)\
**Post date:** [October 3, 2013, 6:27pm UTC](https://discuss.elastic.co/t/sorting-by-score-range-and-date/13838/1 "2013-10-03T18:27:39Z")

</div>

I've been trying to break results up into two groups:

- those with a score over 0.5
- those with a score under 0.5

and then sort each of these sections by date, descending.

I've tried multiple approaches, but can't seem to find the right syntax.  
Can anyone point me in the right direction?

"sort": [  
"\_score",  
{ "date": { "order": "desc" } }  
]

In Perl, I'd sort something like this (untested pseudo code):

sort { $a-\>{score}\>0.5 \<=\> $b-\>{score}\>0.5 || $a-\>{date} cmp $b-\>{date} }

How would I do something similar in elasticsearch?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 4, 2013, 2:46pm UTC](https://discuss.elastic.co/t/sorting-by-score-range-and-date/13838/2 "2013-10-04T14:46:41Z")

</div>

It will not be possible to sort in such a way out-of-the-box with  
elasticsearch. Do all your results need to be sorted or just the top K? Are  
the actual scores important (probably not if you then sort by date)? You  
can apply a custom score where you apply a boost factor, to the document  
score, based on date. The scores will then sort themselves.

--  
Ivan

On Thu, Oct 3, 2013 at 11:27 AM, Clive Holloway [clive.holloway@gmail.com](mailto:clive.holloway@gmail.com)wrote:

> I've been trying to break results up into two groups:
> 
> - those with a score over 0.5
> - those with a score under 0.5
> 
> and then sort each of these sections by date, descending.
> 
> I've tried multiple approaches, but can't seem to find the right syntax.  
> Can anyone point me in the right direction?
> 
> "sort": [  
> "\_score",  
> { "date": { "order": "desc" } }  
> ]
> 
> In Perl, I'd sort something like this (untested pseudo code):
> 
> sort { $a-\>{score}\>0.5 \<=\> $b-\>{score}\>0.5 || $a-\>{date} cmp $b-\>{date} }
> 
> How would I do something similar in elasticsearch?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 4, 2013, 7:00pm UTC](https://discuss.elastic.co/t/sorting-by-score-range-and-date/13838/3 "2013-10-04T19:00:58Z")

</div>

You can achieve it with custom\_score query or if you are using version  
0.90.4 or higher you can use new function\_score query:

{  
"query": {  
"function\_score": {  
"query": {  
.... your query goes here ....  
},  
"functions": [{  
"script\_score" : {  
"script" : "(\_score \> threshold) ? 1.0 : 0.0",  
"params": {  
"threshold": 0.5  
}  
}  
}],  
"boost\_mode": "replace"  
}  
},  
"sort": [  
"\_score",  
{ "date": { "order": "desc" } }  
]  
}

I would be careful with selecting the threshold though. Different queries  
might generate results in quite different ranges.

On Thursday, October 3, 2013 2:27:39 PM UTC-4, Clive Holloway wrote:

> I've been trying to break results up into two groups:
> 
> - those with a score over 0.5
> - those with a score under 0.5
> 
> and then sort each of these sections by date, descending.
> 
> I've tried multiple approaches, but can't seem to find the right syntax.  
> Can anyone point me in the right direction?
> 
> "sort": [  
> "\_score",  
> { "date": { "order": "desc" } }  
> ]
> 
> In Perl, I'd sort something like this (untested pseudo code):
> 
> sort { $a-\>{score}\>0.5 \<=\> $b-\>{score}\>0.5 || $a-\>{date} cmp $b-\>{date} }
> 
> How would I do something similar in elasticsearch?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:13am UTC](https://discuss.elastic.co/t/sorting-by-score-range-and-date/13838/4 "2017-07-06T02:13:43Z")

</div>


