starzodiac
Nov 10, 2020

Elasticsearch ingest pipeline set processor and if condition

最近在研究 Elasticsearch ingest pipeline 的寫法,發現大部分的範例都是直接用 script processor 或是簡單的 set processor 做 1-on-1 的轉換。

但都沒有提到如果想要用 set processor + if 的用法阿~~

以下是我試成功的寫法:

[  
{
"set":{
"if": "ctx.status == 'stopped'",
"field":"status",
"value":"0"
}
},
{
"set":{
"if": "ctx.status == 'running'",
"field":"status",
"value":"1"
}
}
]

測試字串:

[{"_source": {"status":"stopped","cpu":4}}]

測試結果:

{
"docs": [
{
"doc": {
"_index": "_index",
"_type": "_doc",
"_id": "_id",
"_source": {
"cpu": 4,
"status": "0"
},
"_ingest": {
"timestamp": "2020-11-10T07:22:25.382542Z"
}
}
}
]
}

轉換成功!!