python - ElasticSearch,将值附加到字段,但检查字段是否首先存在,如果不存在,则附加字段

标签 python elasticsearch

我试图根据需要将新的数组类型字段动态地添加到文档中,如果该字段已经存在(即,其他人已经在数组中添加了一个项目),则追加我的项目。如果不存在,则需要它来创建字段,然后附加我的项目。

目前,只有在我第一次创建该字段时才能追加,但是编写该字段的方式会覆盖现有的字段值(如果存在)。

# 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”是否存在
  • 如果存在,则将新值附加到现有值
  • 如果不存在,请创建字段,然后附加我的值
  • 最佳答案

    logstash:

    if [notes] {
        notes.add("NewItem");
    } else {
       notes = new ArrayList();
       notes.add("NewItem");
    }
    

    elasticsearch:
    "script": "if (ctx._source.containsKey(\"notes\")) {ctx._source.notes += value;} else {ctx._source.notes = [value]}"
    

    关于python - ElasticSearch,将值附加到字段,但检查字段是否首先存在,如果不存在,则附加字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318886/

    相关文章:

    python - 使用正则表达式解析文本以提取有效段落

    python - Pandas 插值不使用某些方法插值

    python - 使用 BeautifulSoup 遍历 html 树中的元素,并生成一个保持每个元素相对位置的输出?在 Python 中

    elasticsearch - 如何连接两个elasticsearch插入?

    ruby-on-rails - Tire-使用条件/自定义_score进行查询

    python - 使用 Python 将数据发布到 Firebase

    python - 尽管我调用了 pyplot.show(),但 matplotlib 没有显示我的情节

    elasticsearch - Elasticsearch由多个字段分别聚合

    elasticsearch - 如何将Kibana用于多用户

    elasticsearch - 在Java Lucene上模拟 Elasticsearch 索引和查询