我对ES不太熟悉,所以我可能犯了一些错误。
我有一个具有以下映射的索引users
。
{
"mappings":{
"dynamic":false,
"properties":{
"id":{
"type":"keyword"
},
"userId":{
"type":"text"
},
"total":{
"type":"float"
},
"count":{
"type":"float"
},
"earnings":{
"properties":{
"potential":{
"properties":{
"pending":{
"type":"float"
},
"finished":{
"type":"float"
}
}
},
"completed":{
"properties":{
"pending":{
"type":"float"
},
"paid":{
"type":"float"
}
}
}
}
}
}
}
}
我想使用update更新嵌套字段,比如earnings.potential.pending。我有一个类似于下面使用Update的脚本。
{
"script" : {
"source": "ctx._source.earnings.potential.pending += params.total",
"lang": "painless",
"params" : {
"total" : 4
}
}
}
然而,我在illegal_argument_exception
外,点符号是假定的问题如下所示。
"ctx._source.earnings.potential.pending += params.total",
^---- HERE'
发布于 2021-11-12 14:02:26
如果要更新的文档中的earnings
为null,则会得到一个错误:
...
"caused_by": {
"type": "null_pointer_exception",
"reason": "cannot access method/field [potential] from a null def reference"
}
因此,本质上,如果字段为null,则需要在字段上设置默认值。最简单的方法是在索引时执行该操作,或者运行一次查询来在任何地方设置默认值。但是,如果它不是选项,则可以扩展脚本以检查和分配默认设置:
{
"script": {
"lang": "painless",
"source": "if(ctx._source.earnings == null) { ctx._source.earnings = [:] }\n if(ctx._source.earnings.potential == null) { ctx._source.earnings.potential = [:] }\n if(ctx._source.earnings.potential.pending == null) { ctx._source.earnings.potential.pending = 0 } \n ctx._source.earnings.potential.pending += params.total",
"params": {
"total": 5
}
}
}
https://stackoverflow.com/questions/69943778
复制相似问题