【问题标题】:logstash output to elasticsearch with document_id; what to do when I don't have a document_id?logstash 使用 document_id 输出到 elasticsearch;当我没有 document_id 时该怎么办?
【发布时间】:2015-07-25 10:56:42
【问题描述】:

我有一些logstash 输入,我使用document_id 删除重复项。但是,大多数输入没有document_id。以下内容通过实际的document_id 进行探测,但如果它不存在,它会被接受为字面意义上的%{document_id},这意味着大多数文档被视为彼此的副本。这是我的输出块的样子:

output {
        elasticsearch_http {
            host => "127.0.0.1"
            document_id => "%{document_id}"
        }
}

我认为我可以在输出中使用条件。它失败了,错误在代码下方给出。

output {
        elasticsearch_http {
            host => "127.0.0.1"
            if document_id {
                document_id => "%{document_id}"
            } 
        }
}

Error: Expected one of #, => at line 101, column 8 (byte 3103) after output {
        elasticsearch_http {
    host => "127.0.0.1"
    if 

我尝试了一些“if”语句,但它们都失败了,这就是为什么我认为问题在于该块中有任何类型的条件。以下是我尝试过的替代方案:

if document_id <> "" {
if [document_id] <> "" {
if [document_id] {
if "hello" <> "" {

【问题讨论】:

    标签: elasticsearch logstash logstash-configuration


    【解决方案1】:

    你已经接近条件的想法,但你不能把它放在插件块中。改为这样做:

    output {
      if [document_id] {
        elasticsearch_http {
          host => "127.0.0.1"
          document_id => "%{document_id}"
        } 
      } else {
        elasticsearch_http {
          host => "127.0.0.1"
        } 
      }
    }
    

    (但其他答案之一中使用 uuid 过滤器的建议也很好。)

    【讨论】:

    • 完美。正如我对 Val 所说,我更喜欢这个,以防 uuid 意外被省略。
    【解决方案2】:

    解决此问题的一种方法是确保document_id 始终可用。您可以通过在过滤器部分添加UUID filter 来实现此目的,如果它不存在,它将创建document_id 字段。

    filter {
        if "" in [document_id] {
            uuid {
                target => "document_id"
            }
        }
    }
    

    根据 Magnus Bäck 的建议进行了编辑。谢谢!

    【讨论】:

    • 检查字段是否存在的典型方法是if [document_id] { ... }
    • Val,这是一个很好的答案,可能是最“正确”的方法。我将使用 Magnus 的解决方案,因为如果我碰巧错过了过滤输入上的 uuid,它会更安全。我希望我能接受这两个,因为它们都是很好的答案。
    • 没问题!最好的方法是尝试这两种方法,然后自己看看;)
    • @Magnus-Bäck 如果您检查的字段是布尔值,请注意这一点
    【解决方案3】:

    参考:docinfo_fields

    对于在 elasticsearch 中添加的任何文档,如果在插入期间未指定 _id,则会自动生成。我们可以稍后通过使用docinfo_fields 功能使用相同的_id 来更新/删除/搜索查询。

    例子:

    filter {
        json {
            source => "message"
        }
        
        elasticsearch {
            hosts => "http://localhost:9200/"
            user => elastic
            password => elastic
            query => "..."
            docinfo_fields => {
              "_id" => "docid"
              "_index" => "document_index"
            }
        }
        if ("_elasticsearch_lookup_failure" not in [tags]) {
            #... doc update logic ...
        }
    }
    output {
        elasticsearch {
            hosts => "http://localhost:9200/"
            user => elastic
            password => elastic
            index => "%{document_index}"
            action => "update"
            doc_as_upsert => true
            document_id => "%{docid}"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      相关资源
      最近更新 更多