我正在尝试通过官方文档学习Elastic Search中的无痛脚本。 (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 map访问值失败:
{
"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."
}
基于文档,两种方法都应该有效,尤其是第二种方法。我不确定我在这里缺少什么。