# Effectively partial update array field in ElasticSearch

**URL:** https://discuss.elastic.co/t/effectively-partial-update-array-field-in-elasticsearch/288984
**Category:** Elasticsearch
**Created:** [November 11, 2021, 10:49am UTC](https://discuss.elastic.co/t/effectively-partial-update-array-field-in-elasticsearch/288984 "2021-11-11T10:49:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fatcloud](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatcloud/32/93873_2.png) [@fatcloud](https://discuss.elastic.co/u/fatcloud)
#### Post date: [November 11, 2021, 10:49am UTC](https://discuss.elastic.co/t/effectively-partial-update-array-field-in-elasticsearch/288984/1 "2021-11-11T10:49:41Z")

</div>

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:

```auto
"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:

```auto
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?

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [November 15, 2021, 11:22pm UTC](https://discuss.elastic.co/t/effectively-partial-update-array-field-in-elasticsearch/288984/2 "2021-11-15T23:22:40Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html).

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 13, 2021, 11:22pm UTC](https://discuss.elastic.co/t/effectively-partial-update-array-field-in-elasticsearch/288984/3 "2021-12-13T23:22:59Z")

</div>

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