【问题标题】:ElasticSearch, appending value to field, but check if field exists first, if not create field then appendElasticSearch,将值附加到字段,但首先检查字段是否存在,如果不创建字段则追加
【发布时间】: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'}]
                      }
                  }
              })

理想情况下我想要的过程是

  1. 检查字段“注释”是否存在
  2. 如果存在,则将新值附加到现有值中
  3. 如果不存在,则创建字段,然后附加我的值

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    logstash:

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

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 2019-10-28
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多