Update by query adds "ctx" in source of all the matching document

We are using update_by_query to add/modify some of the fields in our documents. The update is working fine. However, it is adding an additional field "ctx" in each of the document:
Here is the query I am using :

POST test/_update_by_query
    {
      "size": 1000,
      "query": {
        "match_all": {}
      },
      "script": {
        "lang": "painless",
        "source":"""for (entry in params.entrySet()){ctx._source[entry.getKey()] = entry.getValue()}""",
        "params": {
          "gender": "female",
          "age": 12,
          "first_name": "John"
        }
      }
 } 

The document before running the query:

      "took" : 0,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Doe"
            }
          }
        ]
      }
    }

After running update_by_query:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "gender" : "female",
          "ctx" : {
            "_routing" : null,
            "_parent" : null,
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_version" : -1
          },
          "last_name" : "Doe",
          "first_name" : "John",
          "age" : 12
        }
      }
    ]
  }
}

I am not able to understand this behavior. Is this the way it works or am I doing something wrong in the script?

Which version of ES are you running?
On ES 7.8 I cannot reproduce this.

I am using 6.7.0:

{
  "name" : "test",
  "cluster_name" : "test_cluster",
  "cluster_uuid" : "MDK5OQFrTDW2pwXs0e67xw",
  "version" : {
    "number" : "6.7.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "8453f77",
    "build_date" : "2019-03-21T15:32:29.844721Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

I think it has to do with this refactoring: https://github.com/elastic/elasticsearch/pull/32096

You should probably just change your script to

    "source":"""for (entry in params.entrySet()){if (entry.getKey() != "key") {ctx._source[entry.getKey()] = entry.getValue()}}""",

Thanks for the help.
The query is still adding ctx into the documents.

Hi Val,
Changing key to ctx helped. the final script is

"source":"""for (entry in params.entrySet()){if (entry.getKey() != "ctx") {ctx._source[entry.getKey()] = entry.getValue()}}""",

Thanks for the help :slight_smile:

Yeah, sorry, I wrote key thinking ctx... glad ou figured it out, though :wink:

:smile:

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