# Remove Fields - all or if contained specific string

**URL:** https://discuss.elastic.co/t/remove-fields-all-or-if-contained-specific-string/187285
**Category:** Elasticsearch
**Created:** [June 25, 2019, 9:26am UTC](https://discuss.elastic.co/t/remove-fields-all-or-if-contained-specific-string/187285 "2019-06-25T09:26:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Datakids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/datakids/32/39622_2.png) [@Datakids](https://discuss.elastic.co/u/Datakids)
#### Post date: [June 25, 2019, 9:26am UTC](https://discuss.elastic.co/t/remove-fields-all-or-if-contained-specific-string/187285/1 "2019-06-25T09:26:16Z")

</div>

Hi all,

I have a question regarding removing an existing field if contained a specific string.

What I did:

I update documents by this Post:

```
POST /myindex/_update_by_query
{
  "script": {
    "source": "ctx._source.tags= 'Test'",
    "lang": "painless"
  },
    "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "must" : [
                 { "term" : {"Custo" : "80010"}}, 
                 { "term" : {"AreaNr" : "962497"}} 
              ]
           }
         }
      }
   }
}

```

And if whatsoever happened I need to remove this entries afterwards by trying this:

```
POST /myindex/_update_by_query?conflicts=proceed
{
  "script" : "ctx._source.tags.remove = 'Test'",
    "query" : {
        "exists": { "field": "tags" }
    }
}

```

Or this:

```
POST /myindex/_update_by_query?conflicts=proceed
{
    "script" : {
        "source": "if (ctx._source.tags.contains('Test')) { ctx._source.tags.remove(ctx._source.tags.indexOf('Test')) }",
        "lang": "painless"
        }
}

```

But neither of the remove Posts works as expected.

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.tags.remove = 'Test'",
          " ^---- HERE"
        ],
        "script": "ctx._source.tags.remove = 'Test'",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.tags.remove = 'Test'",
      " ^---- HERE"
    ],
    "script": "ctx._source.tags.remove = 'Test'",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "dynamic setter [java.lang.String, remove] not found"
    }
  },
  "status": 400
}

```

> And what about:
> 
> `"script":{"inline":"ctx._source.tags.removeAll`
> 
> How does this works ?

Hope you can help

Thanks and regards

---

<div class="post-metadata">

### Author: ![Datakids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/datakids/32/39622_2.png) [@Datakids](https://discuss.elastic.co/u/Datakids)
#### Post date: [June 25, 2019, 10:35am UTC](https://discuss.elastic.co/t/remove-fields-all-or-if-contained-specific-string/187285/2 "2019-06-25T10:35:54Z")

</div>

...then I found and try this:

```
POST /myindex/_update_by_query
{
  "script": { "source": "ctx._source.tags.removeAll(Collections.singleton(params.tag))", "lang": "painless", "params" : { "tag" : "Test" } }, 
    "query" : { "term" : { "field" : "tags" } 
  }
}

```

and it looks like to work, but:

```
{
  "took" : 0,
  "timed_out" : false,
  "total" : 0,
  "updated" : 0,
  "deleted" : 0,
  "batches" : 0,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : []
}

```

hmm, why nothing happens...

still need your advice...  
thanks

---

<div class="post-metadata">

### Author: ![Datakids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/datakids/32/39622_2.png) [@Datakids](https://discuss.elastic.co/u/Datakids)
#### Post date: [June 28, 2019, 8:07am UTC](https://discuss.elastic.co/t/remove-fields-all-or-if-contained-specific-string/187285/3 "2019-06-28T08:07:06Z")

</div>

got it at first step, now need to try to work with combinations of must and should fields..:

```
POST /myindex/_doc/_delete_by_query?conflicts=proceed
{
  "query": {
    "match": {
      "tags.keyword": "Test"
    }
  }
}
```

---

<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: [July 26, 2019, 8:07am UTC](https://discuss.elastic.co/t/remove-fields-all-or-if-contained-specific-string/187285/4 "2019-07-26T08:07:09Z")

</div>

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