日志存储。使用 ruby 脚本的嵌套对象映射

Logstash. Nested object mapping with ruby script

我对 ruby 过滤器插件的嵌套对象映射有疑问。 我的对象应该有字段 cmds,它是这样的对象数组:

 "cmds": [
            {
              "number": 91,
              "errors": [],
              "errors_count": 0
            },
            {
              "number": 92,
              "errors": ["ERROR_1"],
              "errors_count": 1
            }]

通过 elasticsearch 我需要找到 number = 91[=37 的对象=] > 0。所以上面的对象不应该是正确的结果。但我的查询(下方)与它匹配。

GET /logs/default/_search
{
    "query": {
    "bool": {
      "must": [
        {
          "match": {
            "cmds.number": 91
          }
        },
        {
         "range": {
           "cmds.errors_count": {
             "gt": 0

           }
         }
        }]}}

我知道是因为 JSON document is flattened into a simple key-value format 并且我应该将 cmds 字段映射为嵌套类型而不是类型对象。 问题是我不知道如何在我的 logstash ruby 脚本中使用 event.set

我有以下代码:

for t in commandTexts do

    commandv = Command.new(t)

    if i==0
        event.set("[cmds]", ["[number]" => commandv.hexnumber, 
                            "[command_text]" => commandv.command_text, 
                            "[errors]" => commandv.errors, 
                            "[has_error]" => commandv.has_error, 
                            "[errors_count]" => commandv.errors_count])      
    else
        event.set("[cmds]", event.get("cmds") + ["[number]" => commandv.hexnumber,
                                                 "[command_text]" => commandv.command_text, 
                                                 "[errors]" => commandv.errors,
                                                 "[has_error]" => commandv.has_error, 
                                                 "[errors_count]" => commandv.errors_count])

    end
    i+=1
   end
end

我是 ruby 的新手,我的代码并不完美,但 "cmds" 字段在弹性搜索中看起来不错。唯一的问题是没有嵌套。请帮忙。

好的,我做到了。我还是 ELK 的新手,有时我很困惑(logstash/kibana/scripts in ruby)我应该做什么。

我的代码没问题。使用 kibana,我删除了我的索引,并创建了一个具有正确映射的新索引

代码:

PUT  /logs?pretty
{
  "mappings": {"default": {
"properties": {
  "cmds" : {
   "type" : "nested",
    "properties": {
              "command_text": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "errors": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "errors_count": {
                "type": "long"
              },
              "has_error": {
                "type": "boolean"
              },
              "number": {
                "type": "long"
              }
            }

  }
}
  }}

}

早些时候我试图通过将 "type" 设置为 "nested"

来创建新索引
  PUT  /logs?pretty
    {
      "mappings": {"default": {
    "properties": {
      "cmds" : {
       "type" : "nested"
      }


    }
    }}} 

但它没有正常工作("cmds" 字段未添加到 elasticsearch)所以我通过完整映射(所有属性)完成了它。