【发布时间】:2019-05-27 14:23:23
【问题描述】:
我正在尝试根据需要向文档动态添加一个新的数组类型字段,如果该字段已经存在(即,其他人已经向数组中添加了一个项目)然后附加我的项目。如果它不存在,我需要它来创建字段然后附加我的项目。
目前我只能在第一次创建字段时追加,但我编写它的方式会覆盖现有的字段值(如果存在)。
# Create the field, not ideal as it wipes the field if it existed already
es.update(
index='index_name',
id='doc_id_987324bhashjgbasf',
body={"doc": {
'notes': []}})
# Append my value
es.update(index='index_name', id='doc_id_987324bhashjgbasf',
body={
"script": {
"source": "ctx._source.notes.addAll(params.new_note)",
"lang": "painless",
"params": {
"new_note": [{'note': 'Hello I am a note', 'user':'Ari'}]
}
}
})
理想情况下我想要的过程是
- 检查字段“注释”是否存在
- 如果存在,则将新值附加到现有值中
- 如果不存在,则创建字段,然后附加我的值
【问题讨论】:
标签: python elasticsearch