# When i search for document after index a document. It returns empty but it returns document with sleep of 2 seconds between creating and fetching

**URL:** <https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480>\
**Category:** Elasticsearch\
**Created:** [July 25, 2018, 4:38am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480 "2018-07-25T04:38:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sangeeth\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Sangeeth\_Kumar](https://discuss.elastic.co/u/Sangeeth_Kumar)\
**Post date:** [July 25, 2018, 4:38am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480/1 "2018-07-25T04:38:31Z")

</div>

I am using Elasticsearch . I am trying to connect Elasticsearch with python.

I can create index with document. When i tried to fetch the same once successful creation done it returns empty.

If i make the code to sleep for 2 seconds after creating the data in Elasticsearch, then it returns actual data.

Is there any interval required to create and search the same data?

Sample code:

es\_client.index(index=index\_id, doc\_type=doc\_type, id=doc\_id, body=body)

returns:  
{'\_index': 'account\_001', '\_type': 'UnitTest', '\_id': '9f48ae128e4811e88c4b0242ac120013', '\_version': 1, 'result': 'created', '\_shards': {'total': 2, 'successful': 2, 'failed': 0}, 'created': True}

es\_client.search(index=index\_id, doc\_type=doc\_type, body=query, filter\_path=filter\_path)

returns {}

---

<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:** [July 25, 2018, 5:23am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480/2 "2018-07-25T05:23:00Z")

</div>

When a document is indexed into Elasticsearch, it is written to the transaction log. At this point it is not yet available for searching. Making data from the transaction log available for searching is done when a segment is created through a refresh. As this is an expensive operation, it is only done periodically. The frequency is determined by the [refresh\_interval](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/index-modules.html#dynamic-index-settings) of the index, which defaults to 1 second.

---

<div class="post-metadata">

**Author:** ![Sangeeth\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Sangeeth\_Kumar](https://discuss.elastic.co/u/Sangeeth_Kumar)\
**Post date:** [July 25, 2018, 5:34am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480/3 "2018-07-25T05:34:30Z")

</div>

Thank you for the answer. Is there anyway to ensure the indexing a document is successful before start searching it?

---

<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:** [July 25, 2018, 5:38am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480/4 "2018-07-25T05:38:03Z")

</div>

When you index, you can instruct it to[not return until a refresh has occurred](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/docs-refresh.html). You can also force a refresh when you index, but this can dramatically reduce your indexing throughput.

---

<div class="post-metadata">

**Author:** ![Sangeeth\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Sangeeth\_Kumar](https://discuss.elastic.co/u/Sangeeth_Kumar)\
**Post date:** [July 25, 2018, 5:45am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480/5 "2018-07-25T05:45:12Z")

</div>

Thank you Christain\_Dahlqvist. It helped me well

---

<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:** [August 22, 2018, 5:45am UTC](https://discuss.elastic.co/t/when-i-search-for-document-after-index-a-document-it-returns-empty-but-it-returns-document-with-sleep-of-2-seconds-between-creating-and-fetching/141480/6 "2018-08-22T05:45:21Z")

</div>

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