惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
J
Java Code Geeks
博客园 - 聂微东
量子位
C
Check Point Blog
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
B
Blog
罗磊的独立博客
腾讯CDC
GbyAI
GbyAI
博客园 - 【当耐特】
A
About on SuperTechFans
M
MIT News - Artificial intelligence
U
Unit 42
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Index refresh issue in Elasticsearch
2024-07-25 · via jdhao's digital space

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:

  • after the indexing operation, use the refresh API to refresh the index manually.
  • However, the refresh operation may be expensive. We can also wait for the periodic refresh of Elasticsearch. If you use the bulk index or index API, we can set the parameter refresh to wait_for. This will make sure there is a refresh before your search operation.

References#