Remove Fields - all or if contained specific string

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

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

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"
    }
  }
}

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