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

**URL:** <https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570>\
**Category:** Elasticsearch\
**Created:** [August 11, 2020, 2:31pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570 "2020-08-11T14:31:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmadimt07](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadimt07/32/10031_2.png) [@ahmadimt07](https://discuss.elastic.co/u/ahmadimt07)\
**Post date:** [August 11, 2020, 2:31pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/1 "2020-08-11T14:31:15Z")

</div>

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 :

```auto
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:

```auto
      "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`:

```auto
{
  "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?

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 11, 2020, 3:46pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/2 "2020-08-11T15:46:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmadimt07](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadimt07/32/10031_2.png) [@ahmadimt07](https://discuss.elastic.co/u/ahmadimt07)\
**Post date:** [August 11, 2020, 3:58pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/3 "2020-08-11T15:58:29Z")

</div>

I am using 6.7.0:

```auto
{
  "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"
}

```

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 11, 2020, 4:04pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/4 "2020-08-11T16:04:37Z")

</div>

I think it has to do with this refactoring: [https://github.com/elastic/elasticsearch/pull/32096](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()}}""",
```

---

<div class="post-metadata">

**Author:** ![ahmadimt07](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadimt07/32/10031_2.png) [@ahmadimt07](https://discuss.elastic.co/u/ahmadimt07)\
**Post date:** [August 11, 2020, 4:44pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/5 "2020-08-11T16:44:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmadimt07](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadimt07/32/10031_2.png) [@ahmadimt07](https://discuss.elastic.co/u/ahmadimt07)\
**Post date:** [August 11, 2020, 4:53pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/6 "2020-08-11T16:53:31Z")

</div>

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

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

```

Thanks for the help 🙂

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 11, 2020, 5:11pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/7 "2020-08-11T17:11:47Z")

</div>

Yeah, sorry, I wrote `key` thinking `ctx`... glad ou figured it out, though 😉

---

<div class="post-metadata">

**Author:** ![ahmadimt07](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadimt07/32/10031_2.png) [@ahmadimt07](https://discuss.elastic.co/u/ahmadimt07)\
**Post date:** [August 11, 2020, 5:13pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/8 "2020-08-11T17:13:14Z")

</div>

😄

---

<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:** [September 8, 2020, 5:13pm UTC](https://discuss.elastic.co/t/update-by-query-adds-ctx-in-source-of-all-the-matching-document/244570/9 "2020-09-08T17:13:31Z")

</div>

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