# ElasticSearch painless script to remove all the keys except for a list of keys

**URL:** https://discuss.elastic.co/t/elasticsearch-painless-script-to-remove-all-the-keys-except-for-a-list-of-keys/116215
**Category:** Elasticsearch
**Created:** [January 19, 2018, 10:50am UTC](https://discuss.elastic.co/t/elasticsearch-painless-script-to-remove-all-the-keys-except-for-a-list-of-keys/116215 "2018-01-19T10:50:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![umbreak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umbreak/32/26753_2.png) [@umbreak](https://discuss.elastic.co/u/umbreak)
#### Post date: [January 19, 2018, 10:50am UTC](https://discuss.elastic.co/t/elasticsearch-painless-script-to-remove-all-the-keys-except-for-a-list-of-keys/116215/1 "2018-01-19T10:50:16Z")

</div>

I want to execute an atomic update operation on a Elasticsearch (6.1) document where I want to remove all the document except for some keys (on the top level, not nested).

I know that for removing a specific key (something in the example) I can do as follows:

> curl -XPOST 'localhost:9200/index/type/id/\_update' -d '{  
> "script" : "ctx.\_source.remove(params.field)",  
> "params": {  
> "field": "something"  
> }  
> }'

But what If I want to remove every field except for a field called **a** and a field called **b**?

---

<div class="post-metadata">

### Author: ![Arvind\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arvind_rao/32/26758_2.png) [@Arvind\_Rao](https://discuss.elastic.co/u/Arvind_Rao)
#### Post date: [January 19, 2018, 11:44am UTC](https://discuss.elastic.co/t/elasticsearch-painless-script-to-remove-all-the-keys-except-for-a-list-of-keys/116215/2 "2018-01-19T11:44:35Z")

</div>

Try this:

```auto
POST index/type/id/_update
{
   "script": {
    "inline": "Set keys = new HashSet(ctx._source.keySet()); for(fieldName in keys){ if (!fieldName.equals('a') && !fieldName.equals('b')) {ctx._source.remove(fieldName)} }",
    "lang": "painless"
  }
}

```

---

<div class="post-metadata">

### Author: ![umbreak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umbreak/32/26753_2.png) [@umbreak](https://discuss.elastic.co/u/umbreak)
#### Post date: [January 20, 2018, 11:00am UTC](https://discuss.elastic.co/t/elasticsearch-painless-script-to-remove-all-the-keys-except-for-a-list-of-keys/116215/3 "2018-01-20T11:00:27Z")

</div>

Thanks. That's an option. Since in my case the amount of keys there are in the document is much greater than the ones I want to keep, I did it this way:

```auto
POST index/type/id/_update
    "script" : {
        "source": "Object var0 = ctx._source.get(\"a\"); Object var1 = ctx._source.get(\"b\"); ctx._source.clear(); if(var0 != null) ctx._source.put(\"a\", var0); if(var1 != null) ctx._source.put(\"b\", var1);"
    }
```

---

<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: [February 17, 2018, 11:00am UTC](https://discuss.elastic.co/t/elasticsearch-painless-script-to-remove-all-the-keys-except-for-a-list-of-keys/116215/4 "2018-02-17T11:00:29Z")

</div>

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