# Same query returning different result each time

**URL:** <https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230>\
**Category:** Elasticsearch\
**Created:** [August 20, 2024, 8:35pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230 "2024-08-20T20:35:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santanu112](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Santanu112](https://discuss.elastic.co/u/Santanu112)\
**Post date:** [August 20, 2024, 8:35pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/1 "2024-08-20T20:35:16Z")

</div>

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"  
}  
}  
],

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [August 20, 2024, 10:04pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/2 "2024-08-20T22:04:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 21, 2024, 12:49am UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/3 "2024-08-21T00:49:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Santanu112](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Santanu112](https://discuss.elastic.co/u/Santanu112)\
**Post date:** [August 21, 2024, 3:15pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/4 "2024-08-21T15:15:08Z")

</div>

> [@jessgarson](#):
>
> 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?

No.. all documents have same score.

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [August 21, 2024, 4:08pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/5 "2024-08-21T16:08:05Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html).

---

<div class="post-metadata">

**Author:** ![Santanu112](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Santanu112](https://discuss.elastic.co/u/Santanu112)\
**Post date:** [August 21, 2024, 6:26pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/6 "2024-08-21T18:26:17Z")

</div>

After using **preference=local** i am getting consistent result.

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [August 21, 2024, 6:29pm UTC](https://discuss.elastic.co/t/same-query-returning-different-result-each-time/365230/7 "2024-08-21T18:29:22Z")

</div>

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