我試過使用update api。我插入了一個包含列表的文檔。Elasticsearch不允許用於腳本的字段名稱的括號
INSERT:
curl -XPOST "http://localhost:9200/t/t/1" -d'
{
"hobbies(first)" : ["a", "b"]
}'
更新查詢:
curl -XPOST localhost:9200/t/t/1/_update?pretty -d '{ "script" : {
"inline": "ctx._source.hobbies(first).add(params.new_hobby)",
"params" : {
"new_hobby" : "c"
}
}
}'
錯誤:
{
"error" : {
"root_cause" : [
{
"type" : "remote_transport_exception",
"reason" : "[aaBiwwv][172.17.0.2:9300][indices:data/write/update[s]]"
}
],
"type" : "illegal_argument_exception",
"reason" : "failed to execute script",
"caused_by" : {
"type" : "script_exception",
"reason" : "compile error",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Variable [first] is not defined."
},
"script_stack" : [
"ctx._source.hobbies(first).add(params.new_hob ...",
" ^---- HERE"
],
"script" : "ctx._source.hobbies(first).add(params.new_hobby)",
"lang" : "painless"
}
},
"status" : 400
}
當我試圖更新列表中,我已經得到了上面的錯誤。我意識到,當我從領域的名稱中刪除部分括號('(第一)'),它的工作。如何準備帶有括號的字段名稱的更新查詢?
在此先感謝。
請用下劃線替換圓括號。具有文件特性絕對不是一個好習慣。 「業餘愛好(第一)」 - > 「hobbies_first」 好鏈接命名約定: https://www.elastic.co/guide/en/beats/libbeat/current/event-conventions.html – nozari
我'動態生成我的字段名稱,並且有像company(company_code)這樣的字段,這意味着公司字段是使用company_code關係準備的,所以我試圖讓我的字段更容易理解。你知道在腳本中使用括號嗎? –
我建議你創建一個函數,將括號替換爲下劃線,而不是嘗試做這項工作。 – nozari