Mandeep Singh
Mandeep Singh

Reputation: 8224

Elasticsearch: update existing document by inserting elements to its array fields

Consider the following document

{
  "title":  "My first blog entry",
  "text":   "Starting to get the hang of this...",
  "tags": [ "testing" ], 
  "views":  0 
}

I need to run kind of an upsert operation. If I encounter data like

{
    "id": 1,
    "tags": [ "new tag" ]
}

I want to update the existing document with same id. So result should be :

{
    "id": 1,
    "title":  "My first blog entry",
    "text":   "Starting to get the hang of this...",
    "tags": [ "testing", "new tag" ], 
    "views":  0 
}

If the document with same id does not exist, I want to create a new one.

Now in databases like mongoDB, I could use update with $addToSet or $push operation. I could not find similar operation in Elasticsearch.

I read that it can be done by writing scripts in groovy. However, this needs to be done on a file containing 200 million records. I am not sure if I can use groovy in combination with bulk API. Is it possible ?

Upvotes: 8

Views: 7238

Answers (1)

Vineeth Mohan
Vineeth Mohan

Reputation: 19253

You dont need to use bulk API for this. You can use an upsert request. Upsert request can ALSO be embedded in the bulk request.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  "script": "if (ctx._source.tags.contains(\"tags\")) {ctx._source.tags += tag;} else {ctx._source.tags = [tag]}",
  "params": {
    "tag": "newTag"
  },
  "upsert": {
    "title": "My first blog entry",
    "text": "Starting to get the hang of this...",
    "tags": [
      "newTag"
    ],
    "views": 0
  }
}'

Upvotes: 8

Related Questions