Same query returning different result each time

I have more than 50 documents with same score cause they have same name . I am searching top 10 document using "size" and "sort" parameter . Is it ok that every time i am getting different top 10 document ?

"size": "10",
"from": 0,
"sort": [
{
"_score": {
"order": "desc"
}
}
],

Thanks for reaching out here, @Santanu112. My guess is that since you are sorting based on _score and documents have identical scores, the order in which they are returned can indeed vary from query to query. Do the returned results have different values for the _score associated with them?

Also, if you have primary + replica shards, that could explain as well. For one request, it goes to the primary and gives a first list of results. Then it goes to the replica and a second list of results might be returned.

To have a consistent list, you could add another sort parameter, after the _score, like the _doc maybe?

1 Like

No.. all documents have same score.

1 Like

Thanks for following up, @Santanu112. That would make sense since there won't be a consistent order if the documents have the same score.

As @dadoonet pointed out, having your index distributed across multiple shards can also impact the order in which it gets returned. Adding an additional sorting parameter would be your best here. Our documention on sorting might be helpful here.

After using preference=local i am getting consistent result.

1 Like

Thanks so much for your sharing your solution here, @Santanu112 .