Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ElasticSearch,将值追加到字段,但首先检查字段是否存在,如果不存在,则创建字段,然后追加_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

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'}]
                      }
                  }
              })
理想情况下,我想要的过程是

  • 检查字段“notes”是否存在
  • 如果存在,则用现有值追加新值
  • 如果不存在,则创建字段,然后附加我的值
  • 日志存储:

    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]}"