# Mismatch Between Query Result and Index Immediately After Re-Indexing

**URL:** <https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419>\
**Category:** Elasticsearch\
**Created:** [January 19, 2024, 8:06am UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419 "2024-01-19T08:06:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![safakkbilici](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/safakkbilici/32/136311_2.png) [@safakkbilici](https://discuss.elastic.co/u/safakkbilici)\
**Post date:** [January 19, 2024, 8:06am UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/1 "2024-01-19T08:06:19Z")

</div>

Hello community,

I have an index and our post-indexing step, we re-index the index and after, the pipeline (written in Java) uses an aggregation query to fetch product attributes (for example categories and their counts), then we cache these values. I noticed after that the values between index and the cache are not same.

I believe that the problem is we try to fetch categories immediately after re-indexing and index may be not ready to be read (meaning, re-indexing may not be in a completely finished state). Some of the answers includes Refresh API with Java:

```java
IndexRequest indexRequest = new IndexRequest(indexAlias);
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

```

but my operation is not actually a IndexRequest, it is SearchRequest since we are passing an aggregation query to the index.

How can I know that my index is fully ready to be read its most up-to-date data?

---

<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:** [January 19, 2024, 10:29am UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/2 "2024-01-19T10:29:22Z")

</div>

In that case, you will need to force a refresh I'm afraid.

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/main/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L277>

---

<div class="post-metadata">

**Author:** ![safakkbilici](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/safakkbilici/32/136311_2.png) [@safakkbilici](https://discuss.elastic.co/u/safakkbilici)\
**Post date:** [January 19, 2024, 10:55am UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/4 "2024-01-19T10:55:40Z")

</div>

Does force refresh have side effects? Will I be able to read the most current data this way?

---

<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:** [January 19, 2024, 11:39am UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/5 "2024-01-19T11:39:05Z")

</div>

> [@safakkbilici](#):
>
> Will I be able to read the most current data this way?

Yes.

> [@safakkbilici](#):
>
> Does force refresh have side effects?

yes. If you do that very frequently, it will have an impact on the number of segments generated and then the disk IOs.

More details on that at [Near real-time search | Elasticsearch Guide [8.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/near-real-time.html) and [Refresh API | Elasticsearch Guide [8.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html). Highlighting some content:

> Refreshes are resource-intensive. To ensure good cluster performance, we recommend waiting for Elasticsearch’s periodic refresh rather than performing an explicit refresh when possible.
> 
> If your application workflow indexes documents and then runs a search to retrieve the indexed document, we recommend using the [index API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html)'s `refresh=wait_for` query parameter option. This option ensures the indexing operation waits for a periodic refresh before running the search.

---

<div class="post-metadata">

**Author:** ![safakkbilici](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/safakkbilici/32/136311_2.png) [@safakkbilici](https://discuss.elastic.co/u/safakkbilici)\
**Post date:** [January 19, 2024, 1:03pm UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/7 "2024-01-19T13:03:22Z")

</div>

My re-indexing pipeline is a scheduled job and executes once a day. So, for each index, I am going to refresh only once per day, and it is just for an aggregation query to cache categories of documents. Does refreshing this way cause some problems for my users' searches after that?

Btw, I couldn't find `refresh=wait_for` option in Python. Do you know that can I use that with `.indices().refres(index=index)`?

Thanks for your patience and answers!

---

<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:** [January 19, 2024, 3:05pm UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/8 "2024-01-19T15:05:05Z")

</div>

> [@safakkbilici](#):
>
> So, for each index, I am going to refresh only once per day, and it is just for an aggregation query to cache categories of documents. Does refreshing this way cause some problems for my users' searches after that?

No that looks good to me.

> [@safakkbilici](#):
>
> Btw, I couldn't find `refresh=wait_for` option in Python. Do you know that can I use that with `.indices().refres(index=index)`?

No sorry. I don't know. But some other colleagues here might be able to answer. 😉

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 19, 2024, 7:49pm UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/9 "2024-01-19T19:49:10Z")

</div>

> [@safakkbilici](#):
>
> Btw, I couldn't find `refresh=wait_for` option in Python. Do you know that can I use that with `.indices().refres(index=index)`?

Do you need refresh after updating some doc it would be like this

```
    es.index(index=index_name, body=doc, refresh="wait_for")

```

If you need to force the refresh it would be like this:

```
   es.indices.refresh(index=index_name)

```

---

<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:** [February 16, 2024, 7:49pm UTC](https://discuss.elastic.co/t/mismatch-between-query-result-and-index-immediately-after-re-indexing/351419/10 "2024-02-16T19:49:13Z")

</div>

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