我需要在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 been created and deleted really fast)
所需版本是对文档建立索引后由Elasticsearch返回的版本。
该算法有效,但是您可以看到它远非完美。
有人有更好的解决方案吗?目前,自动缩放是 Not Acceptable 答案。
最佳答案
正如GuillaumeMassé所说,一个解决方案将在Elasticsearch中合并https://github.com/elastic/elasticsearch/issues/1063#issuecomment-223368867
因此,我建议您等待内置功能,而不要实现自定义解决方案。