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

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)

{'_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 {}

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 of the index, which defaults to 1 second.

1 Like

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

When you index, you can instruct it tonot return until a refresh has occurred. You can also force a refresh when you index, but this can dramatically reduce your indexing throughput.

Thank you Christain_Dahlqvist. It helped me well

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