Elasticsearch6.2.1 remove field

Is there any method remove field from some docs when don't know doc id or don't specified doc id? Like higher version provide delete_by_query API.

If you mean remove from the mapping, it's not possible.
If you mean remove from documents, update by query should help.

You should also upgrade your cluster to at least 6.8 or 7.7.

I also found update_by_query api, but i am poor at English,so I cann't get full meaning from the official document, Could you please give me some example?

Hi @jerry-chen-315

Here a simple example that may help you to understand the update_by_query:

First create 3 documents:

POST example/doc/1
{
  "field1": 123,
  "field2": 456
}

POST example/doc/2
{
  "field1": 123,
  "field2": 456
}

POST example/doc/3
{
  "field1": 987,
  "field2": 456
}

Check that you have your 3 documents, with 2 of them (id 1 and 2) with field1 equal to 123

GET example/doc/_search

Now let's say that you want to remove the field1 for all the document that have field1 == 123.

POST example/doc/_update_by_query
{
  "query":{"term": {"field1": 123}},
  "script": {
    "source": "ctx._source.remove('field1');",
    "lang": "painless"
  }
}

It will return a json response with updated == 2

Then you can check your data by running this:

GET example/doc/_search

if everything is fine you will have your 3 documents and the doc 1 and 2 will have only field2, document id 3 will keep his 2 fields.

More about update_by_query:
https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update-by-query.html
One similar question on stackoverflow:

1 Like

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