Elasticsearch appends object fields on update instead of overwriting

I have a problem when trying to overwrite value of field of object type in ES document. I use update request with the following document:

{
      "uid": "911e50cf-7ff6-48e2-999c-313490a0748e",
      "name": "name1",
      "category": ["cat1", "cat2"],
      "embeddedModel": {
        "modelOne": {
          "terms": {
            "tags": {
              "2959": "tag-1",
              "2956": "tag-2"
            }
          }
        }
      }
}

Then, I want to update the document with following:

{
      "uid": "911e50cf-7ff6-48e2-999c-313490a0748e",
      "name": "name1",
      "category": ["cat1"],
      "embeddedModel": {
        "modelOne": {
          "terms": {
            "tags": {
              "2959": "tag-1"
            }
          }
        }
      }
}

The only difference is in tags object and category array. As a result I expected to have only one tag in tags object but there are still two tags as in my first request. It seems that it works this way only for object types while for arrays (like "category" array") and simple types (like "name" field) it works correctly and overwrite the value instead of appending values to it.
I know that I can use index request instead of update request but I want to avoid it because I want to change only a small part of the document while the rest of it remains unchanged. Is it some kind of bug? My Elasticsearch version is 7.10.0.

Hi @lukasz.p , welcome!

It's not a bug, it's expected behavior since you're partially updating an object.

For your scenario, you could use a script to update the embeddedModel field completely, like the example below:

POST /idx-name/_update/id-document
{
  "script": {
    "source": """
      ctx._source.category = params.category;
      ctx._source.embeddedModel = params.embeddedModel;
    """,
    "params": {
      "category": [
        "cat1"
      ],
      "embeddedModel": {
        "modelOne": {
          "terms": {
            "tags": {
              "2959": "tag-1"
            }
          }
        }
      }
    }
  }
}

Hi @RabBit_BR , thanks for your reply. To use the script is the only solution in my case? Is it possible to set a mapping for embeddedModel field in a way that its value is always being overwritten during update request?

No, as you mentioned before, you can use the index API. This way, the document will be completely updated.

I don't know to tell you the truth, I suspect that the options are only with the use of the index or update api.

Thanks a lot. I will try to use the script.