批量插入文档到 ElasticSearch,当文档存在时不更新

Bulk insert documents to ElasticSearch without updating when document exists

是否可以批量插入数据到ES而不更新文档内容(如果按Id存在)。只应插入不存在的文档,不进行任何更新。

是,使用批量 create:

POST /my_index/my_type/_bulk
{"create":{"_id":1}}
{"foo":1,"bar":"y"}
{"create":{"_id":6}}
{"foo":1,"bar":"y"}

以上请求是针对已存在的文档 1 和不存在的文档 6。该请求的输出是:

"items": [
      {
         "create": {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "1",
            "status": 409,
            "error": "DocumentAlreadyExistsException[[my_index][2] [my_type][1]: document already exists]"
         }
      },
      {
         "create": {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "6",
            "_version": 1,
            "status": 201
         }
      }
   ]