# Elasticsearch painless

**URL:** <https://discuss.elastic.co/t/elasticsearch-painless/108649>\
**Category:** Elasticsearch\
**Created:** [November 22, 2017, 6:34am UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649 "2017-11-22T06:34:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shijiebei2009](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shijiebei2009/32/22000_2.png) [@shijiebei2009](https://discuss.elastic.co/u/shijiebei2009)\
**Post date:** [November 22, 2017, 6:34am UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/1 "2017-11-22T06:34:26Z")

</div>

when using script in elasticsearch, why must we use params.\_source to access \_source in GET query, but use ctx.\_source to access \_source in POST query, es v5.5.0

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 27, 2017, 5:03pm UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/2 "2017-11-27T17:03:13Z")

</div>

There should not be any difference between GET and POST for query. The bodies are parsed by the same code. Can you give exact reproduction steps?

---

<div class="post-metadata">

**Author:** ![shijiebei2009](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shijiebei2009/32/22000_2.png) [@shijiebei2009](https://discuss.elastic.co/u/shijiebei2009)\
**Post date:** [November 28, 2017, 1:38pm UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/3 "2017-11-28T13:38:46Z")

</div>

params

```auto
GET actionLog/_search?from=0&size=1
{
  "script_fields": {
    "newCBID": {
      "script": {
        "inline": "params._source.CBID-params.curCBID",
        "params": {
          "curCBID": 100000000
        }
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

```

ctx

```auto
POST /myCompany/employee/1/_update
{
  "script": {
    "inline": "ctx._source.age+=params.new_tag",
    "params": {
      "new_tag": 1
    }
  }
}
POST /myCompany/employee/1/_update
{
  "script": {
    "inline": "ctx._source.interests.add(params.new_tag)",
    "params": {
      "new_tag": "football"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 28, 2017, 9:05pm UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/4 "2017-11-28T21:05:34Z")

</div>

These are two different apis. The GET is a search, but the POST is an update. In an update script, ctx is available (the "context" of the document being updated).

---

<div class="post-metadata">

**Author:** ![shijiebei2009](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shijiebei2009/32/22000_2.png) [@shijiebei2009](https://discuss.elastic.co/u/shijiebei2009)\
**Post date:** [November 29, 2017, 2:53am UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/5 "2017-11-29T02:53:32Z")

</div>

> [@rjernst](#):
>
> "context" of the document being update

you mean, when get use params, when post use ctx, but both them I just want to get \_source, why use different reference?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 29, 2017, 4:49pm UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/6 "2017-11-29T16:49:36Z")

</div>

It has nothing to do with POST vs GET. You are using two different APIs. In the search api, the original document is available as `params._source`. In an update script, \_source is available through the special `ctx` variable. This is because update scripts are modifying the document, so `ctx._source` is modifiable.

---

<div class="post-metadata">

**Author:** ![shijiebei2009](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shijiebei2009/32/22000_2.png) [@shijiebei2009](https://discuss.elastic.co/u/shijiebei2009)\
**Post date:** [December 5, 2017, 8:12am UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/7 "2017-12-05T08:12:14Z")

</div>

> [@shijiebei2009](#):
>
> "inline": "params.\_source.CBID-params.curCBID",  
> "params": {  
> "curCBID": 100000000  
> }

one more thing, when I modify fields by params.\_source, It just shows that the fields value in search result has changed, and the original fields value doesn't change? am I right?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [December 5, 2017, 6:19pm UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/8 "2017-12-05T18:19:10Z")

</div>

You shouldn't be able to modify `params._source` at all. If you can, that is a bug in our validation. Only `ctx._source` can be modified in an update script.

---

<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:** [January 2, 2018, 6:20pm UTC](https://discuss.elastic.co/t/elasticsearch-painless/108649/9 "2018-01-02T18:20:03Z")

</div>

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