Sorting by score range and date


(cliveholloway) #1

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.
For more options, visit https://groups.google.com/groups/opt_out.


(Ivan Brusic) #2

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.comwrote:

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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.


(Igor Motov) #3

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.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #4