Elasticsearch painless

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

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?

params

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

ctx

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

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

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

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.

1 Like

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?

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.

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