# Can you get different results from replica and primary if queried at the same time?

**URL:** <https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732>\
**Category:** Elasticsearch\
**Created:** [February 5, 2023, 12:13pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732 "2023-02-05T12:13:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diya\_Al\_Mahameed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diya_al_mahameed/32/116785_2.png) [@Diya\_Al\_Mahameed](https://discuss.elastic.co/u/Diya_Al_Mahameed)\
**Post date:** [February 5, 2023, 12:13pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/1 "2023-02-05T12:13:36Z")

</div>

in our system we get different responses and I speculate it caused by "Adaptive Replica Selection".

the two queries are different but they should fetch the same document .

The first query check if the document have been deleted and from the logs I see it's returning 0 results.  
`
{"query":{"bool":{"must":[{"terms":{"id":[65164]}}],"filter":[{"range":{"updated_at":{"gte":0}}}]}}}
`

the second query check using the date, and it's returning 1 result.  
`
{"query":{"bool":{"must":[{"terms":{"employee_id":[163872]}},{"range":{"date":{"lte":19389}}}]}}}
`

weirdly the second query still returning the deleted document, even though we only run it after the first one(Guard Query).

is it caused by the replica or it's something else ?

Appreciate any help, Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [February 5, 2023, 12:38pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/2 "2023-02-05T12:38:01Z")

</div>

I am confused as the two queries filter on different fields (`id` vs `employee_id` and `updated_at` vs `date`). How are these related? What are the mappings for the fields used in the queries? What does the document you are retrieving look like?

---

<div class="post-metadata">

**Author:** ![Diya\_Al\_Mahameed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diya_al_mahameed/32/116785_2.png) [@Diya\_Al\_Mahameed](https://discuss.elastic.co/u/Diya_Al_Mahameed)\
**Post date:** [February 5, 2023, 12:47pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/3 "2023-02-05T12:47:03Z")

</div>

Hi @Christian_Dahlqvist,

both query the same index, and the data look like this  
`
{
id: 65164 -> integer
employee_id: 163872 -> integer
updated_at: 321450402302 -> long
date: 19123 -> integer
}
`

index settings:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/4/d445772dfe75f5d325ad8dfb9416835d2fbd2f5c.png)

If the document got deleted, and we ensured that using the first query how it's still being fetched ?

Thanks for helping

---

<div class="post-metadata">

**Author:** ![Diya\_Al\_Mahameed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diya_al_mahameed/32/116785_2.png) [@Diya\_Al\_Mahameed](https://discuss.elastic.co/u/Diya_Al_Mahameed)\
**Post date:** [February 5, 2023, 2:23pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/4 "2023-02-05T14:23:02Z")

</div>

@Christian_Dahlqvist

I've read this, [Reading and Writing documents | Elasticsearch Guide [8.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-replication.html#_a_few_simple_implications)

**Read unacknowledged** section imply this, but I need someone to tell for sure it's correct 😕

---

<div class="post-metadata">

**Author:** ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)\
**Post date:** [February 5, 2023, 3:04pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/5 "2023-02-05T15:04:41Z")

</div>

Based on what I can [find](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html#delete-distributed), delete operations should follow the same model as [writing documents](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-replication.html#basic-write-model). Therefore, you shouldn't get a successful response when deleting until all in-sync replicas have acknowledged the delete. Elasticsearch [should only search](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-replication.html#_failures) in-sync shards.

Something you might be running into is shard refresh delays, where one shard might've refreshed and removed the document from search, while the other shard might not have refreshed yet, and still have the document available for search. This [thread](https://discuss.elastic.co/t/how-refresh-works-between-primary-and-replica-shards/219181/2) goes into the topic a bit more.  
You might want to try passing the `refresh=true` [query parameter](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html#docs-delete-api-query-params) on the delete request to see if that helps resolve what you're seeing.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 5, 2023, 4:13pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/6 "2023-02-05T16:13:19Z")

</div>

Exactly how are you deleting the document?

Please show the command/query.

---

<div class="post-metadata">

**Author:** ![Diya\_Al\_Mahameed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diya_al_mahameed/32/116785_2.png) [@Diya\_Al\_Mahameed](https://discuss.elastic.co/u/Diya_Al_Mahameed)\
**Post date:** [February 5, 2023, 4:20pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/7 "2023-02-05T16:20:14Z")

</div>

Thank you @BenB196, the thread you mentioned does answer my question 🥰

Hi @stephenb, I'm using Kafka elasticsearch sink connector and not sure about the inner implementation 😅

> **[GitHub - confluentinc/kafka-connect-elasticsearch: Kafka Connect...](https://github.com/confluentinc/kafka-connect-elasticsearch)**
>
> Kafka Connect Elasticsearch connector. Contribute to confluentinc/kafka-connect-elasticsearch development by creating an account on GitHub.

---

<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:** [March 5, 2023, 4:20pm UTC](https://discuss.elastic.co/t/can-you-get-different-results-from-replica-and-primary-if-queried-at-the-same-time/324732/8 "2023-03-05T16:20:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
