How to replace objects in documents?

This may be a bug, but if not it's undocumented behavior (see Object field type). 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:

{
  "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?

I'm pretty sure that if you do:

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

And then

GET index/_doc/1

You won't see b anymore in your document.

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):

$ 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.

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

You should use the index API IMO.

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

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