首页 > 解决方案 > 如何在 logstash 中使用脚本 upsert 来更新文档

问题描述

我正在使用下面的输出块来更新文档并为具有匹配 ID 的现有文档增加计数器(部分更新)。

目前,将第一个文档条目发布到 elasticsearch 中,“脚本”对后续更新调用没有影响。它没有增加计数器值

下面是使用 upsert 和脚本的 logstash 的输出块:

  `output {
    stdout { }
    elasticsearch {
    hosts => "localhost"
    index => "test_dest"
    script => "ctx._source.views+=1"
    script_lang => "painless"
    script_type => "inline"
    # scripted_upsert => true 
    doc_as_upsert => true
    document_id => "%{[userId]}"
  }
  stdout {
    codec => "json"
  }
}`

标签: elasticsearchgroovylogstashupsertelasticsearch-painless

解决方案


尝试将其设置action“更新”“真”scripted_upsert,如下所示:

output {
    elasticsearch {
       action => "update"
       hosts => "localhost"
       index => "test_dest"
       script => "ctx._source.views+=1"
       script_lang => "painless"
       script_type => "inline"
       scripted_upsert => true 
       document_id => "%{[userId]}"
     }
}

推荐阅读