Elasticsearch 重新索引竞争条件

Elasticsearch Reindexing race condition

你好 elasticsearch users/experts,

我在理解 Elasticsearch 的重新索引 api 的竞争条件问题时遇到了一些麻烦,想知道是否有人找到了解决方案。

我找了很多地方都没有找到任何明确的解决方案(大多数解决方案可以追溯到 reindex api 之前)。

您可能知道,重新索引文档的(现在)标准方法(例如,在更改映射之后)是使用别名。 假设别名指向 "old_index"。然后我们使用新映射创建一个名为 "new_index" 的新索引,我们调用 reindex api 将文档从 'old_index' 重新索引到 'new_index' 然后切换别名指向new_index(并删除指向 old_index 的别名指针)。这似乎是重建索引的标准方式,这也是我在最近访问的几乎所有网站上看到的方式。

我的问题如下,对于使用这种方法,虽然我不想停机(所以用户应该仍然可以搜索文档),但我仍然希望能够在 ElasticSearch 中注入文档重建索引过程正在进行中:

  1. 如果在重建索引过程进行时文档仍会传入(这可能会花费很多时间),重建索引过程将如何确保将文档引入旧索引(以便能够搜索在重新索引过程进行时为它)但仍会正确地重新索引到新索引?
  2. 如果一个文档在旧索引中被修改,在重新索引(映射到新索引)之后,当重新索引过程正在运行时,ElasticSearch 如何确保在新索引中也考虑到此修改?
  3. (类似于2.)如果在旧索引中删除了一条记录,在它被重新索引(映射到新索引)之后,当重新索引过程正在进行时,ElasticSearch如何确保这个删除也被删除在新索引中考虑了吗?

基本上,在无法承受对文档进行任何索引错误的情况下,如何才能继续确保重建索引不会出现上述任何问题?

有人知道吗?如果没有任何停机时间的解决方案,在这种情况下我们将如何处理最少的停机时间?

提前致谢!

抱歉,如果它太冗长,但我的两分钱:

If documents would still be incoming while the reindexing process is working (which would probably take a lot of time), how would the reindexing process ensure that the document would be ingested in the old index (to be able to search for it while the reindexing process is working) but still would be correctly reindexed to the new index?

当从源到目标重新索引时,别名将(并且必须)仍然指向 source_index。该索引的所有 modifications/changes 都以独立的方式发生,这些 updates/deletes 应该会立即产生影响。

假设 source_index 的状态从 t 变为 t+1

如果您有 运行 在 tdest_index 的重建索引作业,它仍会消耗 source_indext 的快照数据。您需要再次 运行 重新索引作业以获得 source_index 的最新数据,即 dest_indext+1 处的数据。

source_index 的摄取和从 source_indexdestination_index 的摄取都是独立的 transactions/processes。

重新索引作业将永远始终保证运行source_indexdest_index之间的一致性。

If a document is modified in the old index, after it has been reindexed (mapped to the new index), while the reindexing process is working, how would ElasticSearch ensure that this modification is also taken account in the new index?

它不会在新索引中考虑,因为重建索引将使用时间 tsource_index 的快照。

您需要重新执行索引重建。对于这种一般方法,将有一个调度程序,它每隔几个小时就会保持 运行ning 重建索引过程。

您可以每隔几分钟(如果您使用调度程序)或实时(如果您使用任何基于事件的方法)在 source_index 发生 updates/deletes。

但是对于完整索引(从 source_indexdest_index),将其安排为一天一次或两次,因为这是一个昂贵的过程。

(Similar to 2.) If a record is deleted in the old index, after it has been reindexed (mapped to the new index), while the reindexing process is working, how would ElasticSearch ensure that this removal is also taken account in the new index?

同样,您需要 运行 一个新的 job/reindexing 进程。

Version_type:外部

顺便说一下,在重建索引期间您可以做的一件有趣的事情是利用 version_type:external,这将确保只有 updated/missing 来自 source_index 的文档将在 dest_index

中重新编制索引

您可以参考此LINK了解更多信息

POST _reindex
{
  "source": {
    "index": "source_index"
  },
  "dest": {
    "index": "dest_index",
    "version_type": "external"
  }
}