如何通过Logstash输出向嵌套字段中添加数据

raogr8fs  于 2022-12-09  发布在  Logstash
关注(0)|答案(1)|浏览(223)

我有一个用户索引,其中有一个嵌套的汽车字段。我如何将mysql结果添加到该用户的嵌套字段中呢?
经过一些挖掘到互联网上,我发现这个代码

POST /users/1/_update
{
  "script": {
    "source": "ctx._source.cats.add(params.cat)",
    "params": {
      "cat": {
        "colors": 4,
        "name": "Leon",
        "breed": "Persian"
      }
    }
  }
}

如何通过logstash实现它?这是我的logstash配置

input {
  jdbc {
    .....
    statement => "select from cars table where...."
    schedule => "*/5 * * * * *"
  }
}
filter {
  mutate {
    copy => { "user_id" => "[@metadata][_id]"}
  }
}
output {
  stdout { codec => rubydebug { metadata => true } }
  opensearch {
    ...
    index => "users"
    action => "update"
    document_id => "%{[@metadata][_id]}"
    script => "ctx._source.cars.add(>>>>>pass the mysql result here<<<<<<<<)"
  }
}

还是我用的方法正确?

f2uvfpb9

f2uvfpb91#

对我来说,你没有用好的方法。
我的建议是重命名字段colorsnamebreed

input {
  jdbc {
    .....
    statement => "select from cars table where...."
    schedule => "*/5 * * * * *"
  }
}
filter {
  mutate {
    copy => { "user_id" => "[@metadata][_id]"}
  }
  mutate {
    rename => {
      "colors" => "[car][colors]"
      "name" => "[car][name]"
      "breed" => "[car][breed]"
    }
  }
}
output {
  stdout { codec => rubydebug { metadata => true } }
  opensearch {
    ...
    index => "users"
    action => "update"
    document_id => "%{[@metadata][_id]}"
  }
}

相关问题