elasticsearch "block until refresh"/"wait for doc to be searchable" 备选方案
elasticsearch "block until refresh"/"wait for doc to be searchable" alternatives
我需要在 Elasticsearch 中 index/update 一个文档并等待它可搜索(已完成刷新)。 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返回的版本。
此算法有效,但您会发现它远非完美。
首先它会在有压力的情况下对elasticsearch进行更多的调用,这不是一个很好的主意。
我看到当文档被删除一段时间后,elastic search 会重置版本号。如果由于某种原因函数错过了那个,我们可能会等到文档再次达到这个版本。 (这就是为什么我还添加了超时)。
有人有更好的解决方案吗?自动缩放目前不是一个可接受的答案。
正如 Guillaume Massé 所说,一个解决方案即将合并到 Elasticsearch https://github.com/elastic/elasticsearch/issues/1063#issuecomment-223368867
因此我建议等待内置功能,而不是实施自定义解决方案。
我需要在 Elasticsearch 中 index/update 一个文档并等待它可搜索(已完成刷新)。 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返回的版本。
此算法有效,但您会发现它远非完美。
首先它会在有压力的情况下对elasticsearch进行更多的调用,这不是一个很好的主意。
我看到当文档被删除一段时间后,elastic search 会重置版本号。如果由于某种原因函数错过了那个,我们可能会等到文档再次达到这个版本。 (这就是为什么我还添加了超时)。
有人有更好的解决方案吗?自动缩放目前不是一个可接受的答案。
正如 Guillaume Massé 所说,一个解决方案即将合并到 Elasticsearch https://github.com/elastic/elasticsearch/issues/1063#issuecomment-223368867
因此我建议等待内置功能,而不是实施自定义解决方案。