# Elasticsearch strange behaviour for queries straight after insertion

**URL:** <https://discuss.elastic.co/t/elasticsearch-strange-behaviour-for-queries-straight-after-insertion/14246>\
**Category:** Elasticsearch\
**Created:** [November 4, 2013, 5:58pm UTC](https://discuss.elastic.co/t/elasticsearch-strange-behaviour-for-queries-straight-after-insertion/14246 "2013-11-04T17:58:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dario\_Rossi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dario_rossi/32/1672_2.png) [@Dario\_Rossi](https://discuss.elastic.co/u/Dario_Rossi)\
**Post date:** [November 4, 2013, 5:58pm UTC](https://discuss.elastic.co/t/elasticsearch-strange-behaviour-for-queries-straight-after-insertion/14246/1 "2013-11-04T17:58:28Z")

</div>

I am writing some integration tests for an app that uses Elasticsearch and  
I am experiencing a strange behaviour. If I insert a document and I query  
straight after that, I get different results every time. I suspect that,  
albeit the insertion itself will return, the indexing itself doesn't take  
place synchronously and, because of that, the query will experiment a race  
condition with unpredictable results.

_If this is the case:_ is there a way to synchronize, so that when I run my  
queries, I know that they are ready and successful???

More details: I am using elasticsearch embedded and the query is a simple  
filter. The only odd thing is that I am using template files for the  
document model.

EDIT: I even tried to GET the document by ID after insertion, but the  
queries still return random results (unless I put a thread Sleep to wait  
some seconds).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Matteo\_Moci](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Matteo\_Moci](https://discuss.elastic.co/u/Matteo_Moci)\
**Post date:** [November 4, 2013, 6:03pm UTC](https://discuss.elastic.co/t/elasticsearch-strange-behaviour-for-queries-straight-after-insertion/14246/2 "2013-11-04T18:03:26Z")

</div>

Hi Dario,  
The first thing that comes to mind: did you set the refresh parameter [1]  
to true?

[1]

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Mon, Nov 4, 2013 at 6:58 PM, Dario Rossi [darioros@gmail.com](mailto:darioros@gmail.com) wrote:

> I am writing some integration tests for an app that uses Elasticsearch and  
> I am experiencing a strange behaviour. If I insert a document and I query  
> straight after that, I get different results every time. I suspect that,  
> albeit the insertion itself will return, the indexing itself doesn't take  
> place synchronously and, because of that, the query will experiment a race  
> condition with unpredictable results.
> 
> _If this is the case:_ is there a way to synchronize, so that when I run  
> my queries, I know that they are ready and successful???
> 
> More details: I am using elasticsearch embedded and the query is a simple  
> filter. The only odd thing is that I am using template files for the  
> document model.
> 
> EDIT: I even tried to GET the document by ID after insertion, but the  
> queries still return random results (unless I put a thread Sleep to wait  
> some seconds).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Matteo Moci  
[http://it.linkedin.com/in/matteomoci](http://it.linkedin.com/in/matteomoci)

> **[Matteo Moci on about.me](https://about.me/matteomoci)**
>
> I am a software engineer in Rome, Italy. View my repo.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 2:08am UTC](https://discuss.elastic.co/t/elasticsearch-strange-behaviour-for-queries-straight-after-insertion/14246/3 "2017-07-06T02:08:58Z")

</div>


