Hi,
I have a search within my index that currently works, I want to get the professionals that have a subscription first (is_subscribed BOOLEAN) and then follow with the rest of users. I achieve this using the query:
POST /my_index/professionals/_search
{
"from":0,
"size":40,
"query":{
"constant_score":{
"filter":{
"bool":{
"should":[
{
"term":{
"primary_locality_id":4
}
},
{
"term":{
"secondary_locality_id":4
}
}
]
}
}
}
},
"sort":[
{
"is_subscribed":{
"order":"desc"
}
}
]
}
This is giving the expected output, but to be fair to our users we would like to randomly seed this results while keeping the prioritised items (in this case, is_subscribed: true) on the top and follow with the rest. Googling, I ran into the function_score using a random_score and a seed in order to randomise the items. I wrote this query:
{
"from":0,
"size":57,
"query":{
"function_score":{
"query":{
"constant_score":{
"filter":{
"bool":{
"should":[
{
"term":{
"primary_locality_id":4
}
},
{
"term":{
"secondary_locality_id":4
}
}
]
}
}
}
},
"random_score":{
"seed":1484879348
}
}
},
"sort":[
{
"is_subscribed":{
"order":"desc"
}
}
]
}
That will return always the same results, no matter what the seed is. I know this is happening because of my this part:
"sort":[
{
"is_subscribed": {
"order":"desc"
}
}
]
This is affecting the function_score
, as if I remove then the results are actually randomised and they respect the different random_score.seed
value. But that doesn't fix my issue, as I still need to prioritise those subscribed users.
I've read through the documentation and Googled and couldn't find a way of combining both in a way the sort has better priority. What I am missing out? What's the way of first sorting and then randomising?
Thanks in advance,