我需要在Elasticsearch中索引/更新文档并等待它可搜索(刷新已完成).Github上有一个相关的问题:https://github.com/elasticsearch/elasticsearch/issues/1063
我不会强制刷新,因为它会影响索引性能,我需要经常执行此操作.我按照Github问题的描述尝试等待1秒钟.只要Elasticsearch没有压力,它就能很好地运行,但是当没有多少RAM(偶尔可能会发生)时,我看到刷新需要5到6秒.因此我尝试了另一种方式.
我在后端编写了一个辅助函数,等待"可搜索"文档达到给定版本.这很简单:
- GET the document with realtime=false
- if there is a result
- if result.version >= wanted.version.
Return
- else
wait a little more and retry
- else if the doc is not found
- HEAD the document with realtime=true (test if the doc exists in the transaction log)
- if the doc is found (then it has just been created)
wait a little more and retry
- else
Return. (the doc might have …Run Code Online (Sandbox Code Playgroud)