通过Logstash更新Elasticsearch的现有文档,并插入当前记录
我正在尝试将记录插入Elasticsearch,并更新一个现有文档的字段,该文档将从当前的记录中获得_id。在线搜索后,我发现我们可以将_update_by_query API与logstash中的HTTP插件一起使用。这是以下配置。
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "my_index_*"
document_id => "%{id_field}"
}
http {
url => "http://localhost:9200/my_index_*/_update_by_query"
http_method => "post"
content_type => "application/json"
format => "message"
message => '{"query":{"match":{"_id":"%{previous_record_id}"}},"script":{"source":"ctx._source.field_to_be_updated=xyz","lang":"painless"}}'
}
}
Elasticsearch没有密码保护,因此我没有添加授权标题。 但是,当我启动LogStash时,当前记录将被插入,但是我总是在HTTP插件中出现以下错误。
2022-05-05T11:31:51,916][ERROR][logstash.outputs.http ][logstash_txe] [HTTP Output Failure] Encountered non-2xx HTTP code 400 {:response_code=>400, :url=>"http://localhost:9200/my_index_*/_update_by_query", :event=>#<LogStash::Event:0x192606f8>}
I am trying to insert a record into elasticsearch and also update a field of an existing document whose _id I'll be getting from the current record. After searching online, I found that we can use the _update_by_query api with the http plugin in logstash. This is the below configuration.
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "my_index_*"
document_id => "%{id_field}"
}
http {
url => "http://localhost:9200/my_index_*/_update_by_query"
http_method => "post"
content_type => "application/json"
format => "message"
message => '{"query":{"match":{"_id":"%{previous_record_id}"}},"script":{"source":"ctx._source.field_to_be_updated=xyz","lang":"painless"}}'
}
}
The Elasticsearch has no password protection and so I haven't added an authorization header.
But when I start logstash, the current record gets inserted but I always the below error for the http plugin.
2022-05-05T11:31:51,916][ERROR][logstash.outputs.http ][logstash_txe] [HTTP Output Failure] Encountered non-2xx HTTP code 400 {:response_code=>400, :url=>"http://localhost:9200/my_index_*/_update_by_query", :event=>#<LogStash::Event:0x192606f8>}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是您应该这样做的方式,您可以简单地使用两种用例的
Elasticsearch
输出。第一个用于索引新记录的索引和以下一个用于部分更新另一个记录的记录,其ID为
PROSES_RECORD_ID
。脚本中可以在params.event
中访问事件数据:It's not how you're supposed to do it, you can simply use the
elasticsearch
output for both use cases.The first one for indexing a new record and the following one for partial updating another record whose id is
previous_record_id
. The event data can be accessed inparams.event
within the script: