# How to replace objects in documents?

**URL:** https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657
**Category:** Elasticsearch
**Created:** [July 26, 2021, 5:39pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657 "2021-07-26T17:39:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![nahh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nahh/32/83181_2.png) [@nahh](https://discuss.elastic.co/u/nahh)
#### Post date: [July 26, 2021, 5:39pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657/1 "2021-07-26T17:39:47Z")

</div>

This may be a bug, but if not it's undocumented behavior (see [Object field type](https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html)). I accidentally created some objects with properties I don't want them to have, and I need to remove them. The object is dynamic and looks like:

```auto
{
  "a": 1,
  "b": 2
}

```

I need to remove `b`. When I try to update the object with `{"a": 3}` it just replaces `a` and unexpectedly leaves `b` in place. How do I replace the object entirely? Or remove the property?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 26, 2021, 5:57pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657/2 "2021-07-26T17:57:46Z")

</div>

I'm pretty sure that if you do:

```auto
PUT index/_doc/1
{
  "a": 3
}

```

And then

```
GET index/_doc/1

```

You won't see `b` anymore in your document.

---

<div class="post-metadata">

### Author: ![nahh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nahh/32/83181_2.png) [@nahh](https://discuss.elastic.co/u/nahh)
#### Post date: [July 26, 2021, 6:37pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657/3 "2021-07-26T18:37:33Z")

</div>

I wasn't entirely clear -- the object is inside the document, so it's `{"obj": {"a": 1, "b": 2}}`

Removing the property doesn't seem to be enough. Here's an example (I'm using the bulk API, but it should work the same as the regular API):

```bash
$ curl -XPUT -H 'content-type: application/json' -d $'
{
  "mappings": {
    "properties": {
      "obj": {
        "type": "object",
        "dynamic": true
      }
    }
  }
}' localhost:9200/foo
{"acknowledged":true,"shards_acknowledged":true,"index":"foo"}

$ curl -XPOST -H 'content-type: application/json' -d '{"obj": {"a": 1, "b": 2}}' localhost:9200/foo/_doc/1
{"_index":"foo","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

$ curl -XPOST -H 'content-type: application/x-ndjson' -d $'
quote> { "update": { "_id": "1", "_index": "foo" } }
quote> { "doc": { "obj": { "a": 3 } } }
quote> ' localhost:9200/foo/_bulk
{"took":9,"errors":false,"items":[{"update":{"_index":"foo","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1,"status":200}}]}

$ curl localhost:9200/foo/_doc/1\?pretty
{
  "_index" : "foo",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "obj" : {
      "a" : 3,
      "b" : 2
    }
  }
}

```

I forgot to mention I'm on ES 7.13.0.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 26, 2021, 6:57pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657/4 "2021-07-26T18:57:38Z")

</div>

That's the expected behavior. You are not replacing the object `obj` but the field `obj.a`.

You should use the index API IMO.

---

<div class="post-metadata">

### Author: ![nahh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nahh/32/83181_2.png) [@nahh](https://discuss.elastic.co/u/nahh)
#### Post date: [July 26, 2021, 7:15pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657/5 "2021-07-26T19:15:32Z")

</div>

I see, using `index` instead of `update` when using the bulk API does indeed do what I need. Thank you!

---

<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: [August 23, 2021, 7:16pm UTC](https://discuss.elastic.co/t/how-to-replace-objects-in-documents/279657/6 "2021-08-23T19:16:03Z")

</div>

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