Python ElasticSearch,将值追加到字段,但首先检查字段是否存在,如果不存在,则创建字段,然后追加
我正在尝试根据文档的需要动态地向文档中添加一个新的数组类型字段,如果该字段已经存在(即,其他人已经向数组中添加了一个项),那么就添加我的项。如果它不存在,我需要它来创建字段,然后附加我的项 目前,我只能在第一次创建字段时追加,但如果存在字段值,我编写字段的方式会覆盖现有字段值Python ElasticSearch,将值追加到字段,但首先检查字段是否存在,如果不存在,则创建字段,然后追加,python,elasticsearch,Python,elasticsearch,我正在尝试根据文档的需要动态地向文档中添加一个新的数组类型字段,如果该字段已经存在(即,其他人已经向数组中添加了一个项),那么就添加我的项。如果它不存在,我需要它来创建字段,然后附加我的项 目前,我只能在第一次创建字段时追加,但如果存在字段值,我编写字段的方式会覆盖现有字段值 # Create the field, not ideal as it wipes the field if it existed already es.update( index
# 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'}]
}
}
})
理想情况下,我想要的过程是
if [notes] {
notes.add("NewItem");
} else {
notes = new ArrayList();
notes.add("NewItem");
}
弹性搜索:
"script": "if (ctx._source.containsKey(\"notes\")) {ctx._source.notes += value;} else {ctx._source.notes = [value]}"