Painless scripting Elastic Search : variable is no

2019-08-01 04:02发布

问题:

I am trying to learn painless scripting in Elastic Search by following the official documentation. ( https://www.elastic.co/guide/en/elasticsearch/painless/6.0/painless-examples.html )

A sample of the document I am working with :

{
          "uid" : "CT6716617",
          "old_username" : "xyz",
          "new_username" : "abc"

    }

the following script fields query using params._source to access document values works :

{
      "script_fields": {
        "sales_price": {
          "script": {
            "lang":   "painless",
            "source": "(params._source.old_username != params._source.new_username) ? \"change\" : \"nochange\"",
            "params": {
              "change": "change"
            }
          }
        }
      }
    }

The same query but using the doc map to access values fails :

{
      "script_fields": {
        "sales_price": {
          "script": {
            "lang":   "painless",
            "source": "(doc['old_username'] != doc['new_username']) ? \"change\" : \"nochange\"",
            "params": {
              "change": "change"
            }
          }
        }
      }
    } 

The error message I get is :

"caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Variable [old_username] is not defined."
          }

Based on the documentation both of the approaches should work, especially the 2nd one. I am not sure what I am missing here.?