Painless scripting Elastic Search : variable is not defined error when trying to access values from doc

寵の児 提交于 2019-12-24 07:58:55

问题


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

来源:https://stackoverflow.com/questions/47566393/painless-scripting-elastic-search-variable-is-not-defined-error-when-trying-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!