


















Recently when I was working with Elasticsearch, I found this weird issue that the document count for the newly created index is not correct. Also there are problems with reindex, if you reindex the source index to destination index, there is nothing in the destination index despite no errors.
Here is a short code to reproduce the issue:
from elasticsearch import Elasticsearch
# set up client
es_client = Ealsticsearch(...)
n = 100
index_name = "my_index"
if es_client.indices.exists(index=index_name):
es_client.indices.delete(index=index_name)
docs = []
for i in range(n):
doc = {"title": f"this is document {i}"}
docs.append(doc)
for doc in docs:
es_client.index(index=index_name, document=doc)
response = es_client.count(index=index_name)
print("count: ", response)In the above code, we create an index my_index and try to index 100 documents into it.
The print out message shows the number of document in my_index is actually not 100.
However, if you wait for a few seconds and run the count API from kibana or you run the count API only,
the number of doc in my_index is correct.
This has something to do with the inner workings of Elasticsearch.
After immediately indexing document to the index, they are not available for search.
Elasticsearch will refresh the index every index.refresh_interval1, which is by default set to 1s.
Only after the refresh, you can search/find the document.
There are two solutions for this:
refresh to wait_for. This will make sure there is a refresh before your search operation.此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。