Remove a field from the document is not successful

Hi, I reference

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : "ctx._source.remove("name_of_field")"
}'

Test document inside the type below the message field to delete

curl -XPOST 'localhost:9200/test/type/_update' -d '{
"script" : "ctx._source.remove("message")"
}

"script" : "ctx.type.remove(\"message\")"
"script" : "test.type.remove(\"message\")"
"script" : "type.remove(\"message\")"
"script" : "remove(\"message\")"

But they are not successful, will add the script field .

Thanks.

Hi,

you need to specify the document id in the path. Otherwise you create a new document with the id "_update".

First of all, turn on dynamic scripting by adding the following line to your elasticsearch.yml and restart the server:

script.inline: true

Here's a complete example:

Insert a document:

POST /delete-example/type/1
{
    "host": "localhost",
    "field": "delete me"
}

The resulting document:

GET /delete-example/type/1
{
   "_index": "delete-example",
   "_type": "type",
   "_id": "1",
   "_version": 1,
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   },
   "created": true
}

Delete the field "field":

POST /delete-example/type/1/_update
{
"script" : "ctx._source.remove(\"field\")"
}

The resulting document (note that the field "field" is gone).

GET /delete-example/type/1
{
   "_index": "delete-example",
   "_type": "type",
   "_id": "1",
   "_version": 2,
   "found": true,
   "_source": {
      "host": "localhost"
   }
}

If you want to remove the same field for multiple documents, you should look at the delete-by-query plugin.

Daniel

1 Like