使用 ElasticSearch Bulk 动态更新和创建文档?

Using ElasticSearch Bulk to update and create documents dynamically?

我目前正在使用 elasticsearch 并且 运行 每 10 分钟执行一次 cron 作业,它将从我的数据库中找到新的 created/updated 数据并将其与 elasticsearch 同步.但是,我想使用 bulk 来同步而不是对索引中的 update/create 文档发出任意数量的请求。我正在使用由 elasticsearch 创建的 elasticsearch.js 库。

我面临 2 个不确定如何应对的挑战:

尝试从 SQL 数据库输入数据时,最好的选择是使用 Logstash's JDBC Input to do it for you (the documentation)。希望这可以为您完成所有工作。

并非所有 SQL 方案都能让这变得简单,因此对于您的具体问题:

How to use bulk to update a document if it exists and create a document if it doesn't within bulk without knowing if it exists in the index.

Bulk currently accepts four different types of sub-requests,其行为可能与您预期来自 SQL 世界的行为不同:

  • index
  • create
  • update
  • delete

第一个,index,是最常用的选项。这意味着您想要 index(动词)某些东西到 Elasticsearch 索引(名词)。但是,如果它已经存在于给定相同 _id 的索引中,那么它将替换它。其余的可能更明显。

每个子请求的行为类似于它们关联的 个人 选项(因此 update 是一个 UpdateRequestdelete 是一个 DeleteRequest,而 index 是一个 IndexRequest)。在 create 的情况下,它是 index 的特化,实际上表示 "add this if it doesn't exist, but fail it if is does exist".

How to format a large amount of JSON to run through bulk to update/create the document because bulk api expects the body to be formatted a certain way.

您应该考虑使用 Logstash 方法 任何现有的客户端语言库,例如 Python client,它应该在 cron 中运行良好。客户将为您处理格式。您首选的语言很可能已经存在。