marcinn
marcinn

Reputation: 1938

Bulk insert documents to ElasticSearch without updating when document exists

Is it possible to bulk insert data to ES without updating document content if exists by Id. Only not existing documents should be inserted, without any updates.

Upvotes: 3

Views: 2560

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Yes, use bulk create:

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

The above request is for doc 1, which exists already and doc 6 which doesn't exist. The output of that request is:

"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
         }
      }
   ]

Upvotes: 2

Related Questions