Effectively partial update array field in ElasticSearch

Hi elastic experts, I'm wondering if there is a way to effectively partial update an element in an array field? For example, a document has a commands field like this:

"commands": [
  {
    "id": 1000,
    "content": "abc",
  },
  {
    "id": 1001,
    "content": "bcd",
  },
  {
    "id": 1002,
    "content": "cde",
  }
]

When updating the index, I can only have the commands that needs to be updated but not all commands. I tried to use update with script, like this:

ctx._source.commands.removeIf(r -> params.commandsToUpdateIds.contains(r.id));
ctx._source.shell.commands.addAll(params.commandsToUpdate);

This doesn't work well when the commands list is big(more than 3000 commands), I got a lot of version conflict errors, and the update lag is long.

I'm wondering if there is a good way to do partial update for array fields?

Internally so far there is NO notion of partial update in Elasticsearch. Even if you ask to update only several fields, Elasticsearch first reads the previous version of the document, then constructs a new document based on the previous version and new values for fields, and then reindexes the document. If the document has been changed in between, you will get a version conflict.

The only way I can see to model your problem in Elasticsearch is to use separate documents of each command id, for example by using join field.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.