亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 python 進行 Elastic Search 批量更新,如何使用新數據追加數組字段

使用 python 進行 Elastic Search 批量更新,如何使用新數據追加數組字段

慕的地6264312 2023-10-26 15:14:59
如何使用 python 中的bulkupdate 更新彈性搜索中的字段。我嘗試了很多方法,但都出錯了。在某些情況下,我收到文檔丟失錯誤,如何同時更新和更新插入。而且附加到字段也不起作用。elasticsearch==7.9.1 是我在 python 中使用的包for i in range(0, length, steps):    end_index = length-1 if i+steps>length else i+steps    temp_list = test_data[i: end_index]    bulk_file = ''    actions = [{        "_index": "test-data",        "_opt_type":"update",        "_type": "test-test-data",        "_id": test_row ['testId'],        "doc":{"script": {                          "source": "ctx._source.DataIds.add(params.DataIds)",                          "lang": "painless",                          "params": {                              "DataIds":test_row ['DataIds']                          }                      }}        }        for test_row in temp_list    ]    helpers.bulk(es, actions)我得到的錯誤是這樣的    {'update': {'_index': 'test-data', '_type': 'products', '_id': '333', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'failed to execute script', 'caused_by': {'type': 'script_exception', 'reason': 'runtime error', 'script_stack': ['ctx._source.dataIds.add(params.dataIds)', '    ^---- HERE'], 'script': 'if (ctx._source.dataIds == null) { ctx._source.dataIds = []; } ctx._source.dataIds.add(params.dataIds)', 'lang': 'painless', 'position': {'offset': 105, 'start': 71, 'end': 118}, 'caused_by': {'type': 'illegal_argument_exception', 'reason': 'dynamic method [java.lang.String, add/1] not found'}}}, 'data': {'upsert': {}, 'scripted_upsert': True, 'script': {'source': 'if (ctx._source.dataIds == null) { ctx._source.dataIds = []; } ctx._source.dataIds.add(params.dataIds)', 'lang': 'painless', 'params': {'cdataIds': 'set123'}}}}}])
查看完整描述

1 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

upsert通過腳本的正確方法是不使用該部分,doc而僅使用該script部分。upsert如果您想在同一命令中更新插入和更新,您還需要該部分。事情是這樣的:

actions = [{

? ? "_op_type":"update",

? ? "_index": "test-data",

? ? "_type": "test-test-data",

? ? "_id": test_row ['testId'],

? ? "upsert": {

? ? ? ?"DataIds": test_row ['DataIds']

? ? },

? ? "script": {

? ? ? ? "source": "ctx._source.DataIds.add(params.DataIds)",

? ? ? ? "lang": "painless",

? ? ? ? "params": {

? ? ? ? ? ?"DataIds":test_row ['DataIds']

? ? ? ? }

? ? }

} for test_row in temp_list

]

另一種方法是使用scripted_upsert

actions = [{

? ? "_op_type":"update",

? ? "_index": "test-data",

? ? "_type": "test-test-data",

? ? "_id": test_row ['testId'],

? ? "upsert": {},

? ? "scripted_upsert": true,

? ? "script": {

? ? ? ? "source": "if (ctx._source.DataIds == null) { ctx._source.DataIds = []; } ctx._source.DataIds.add(params.DataIds)",

? ? ? ? "lang": "painless",

? ? ? ? "params": {

? ? ? ? ? ?"DataIds":test_row ['DataIds']

? ? ? ? }

? ? }

} for test_row in temp_list

]


查看完整回答
反對 回復 2023-10-26
  • 1 回答
  • 0 關注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號