我對 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"
}
}
}
}
}
}
}
}
我想使用更新 API 來更新嵌套欄位,例如income.potential.pending。我有一個類似于下面使用更新 API 的腳本。
{
"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'
uj5u.com熱心網友回復:
如果earnings您嘗試更新的檔案為 null,您將收到錯誤訊息:
...
"caused_by": {
"type": "null_pointer_exception",
"reason": "cannot access method/field [potential] from a null def reference"
}
所以本質上你需要在欄位上設定一個默認值,如果它是空的。最簡單的方法是在索引時進行,或者運行一次性查詢以在任何地方設定默認值。但如果它不是一個選項,你可以擴展你的腳本來檢查和分配一個默認值:
{
"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://www.uj5u.com/gongcheng/357699.html
