我在 logstash 中接收到一些有效負載,我在每月滾動索引中推送 Elastic,并使用腳本允許我根據這些有效負載的狀態順序覆寫欄位。
例子 :
{
"id" : "abc",
"status" : "OPEN",
"field1" : "foo",
"opening_ts" : 1234567
}
{
"id" : "abc",
"status" : "CLOSED",
"field1" : "bar",
"closing_ts": 7654321
}
我想要這樣,即使我在 CLOSE 之后收到有效載荷 OPEN 為 id“abc”,我的彈性檔案是:
{
"_id" : "abc",
"status": "CLOSED",
"field1" : "bar",
"closing_ts": 7654321,
"opening_ts" : 1234567
}
我為了保證,我在我的彈性輸出插件中添加了一個腳本
script => "
if (ctx._source['status'] == 'CLOSED') {
for (key in params.event.keySet()) {
if (ctx._source[key] == null) {
ctx._source[key] = params.event[key]
}
}
} else {
for (key in params.event.keySet()) {
ctx._source[key] = params.event[key]
}
}
"
Buuuuut,添加這個腳本還在索引上隱含的“PUT”之間增加了一個額外的步驟,如果目標索引不存在,則腳本將失敗并且永遠不會創建整個檔案。(也不是索引)
你知道我該如何處理這個腳本中的錯誤嗎?
uj5u.com熱心網友回復:
您需要使用腳本 upsert:
output {
elasticsearch {
index => "your-index"
document_id => "%{id}"
action => "update"
scripted_upsert => true
script => "... your script..."
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485194.html
標籤:弹性搜索
