无痛脚本弹性搜索:从文档试图访问值时,变量没有定义的错误(Painless scripting El

2019-09-26 07:57发布

我想通过以下官方文档,以了解在弹性搜索无痛脚本。 ( https://www.elastic.co/guide/en/elasticsearch/painless/6.0/painless-examples.html )

我一起工作的文件的样本:

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

    }

下面的脚本使用params._source访问文档值字段工作查询:

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

同样的查询,但使用DOC地图来访问值失败:

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

该错误消息我得到的是:

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

基于文档两种方法都应该工作,尤其是第二次一个上。 我不知道我在这里失踪?

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