Elasticsearch 8.X 小技巧:使用存儲(chǔ)腳本優(yōu)化數(shù)據(jù)索引與轉(zhuǎn)換過程
1、引言
在 Elasticsearch 中,可以使用 Painless 腳本來(lái)實(shí)現(xiàn)一些非標(biāo)準(zhǔn)的處理結(jié)果。這些腳本可以直接嵌入到數(shù)據(jù)處理管道中,但為了使腳本與管道相互獨(dú)立,還可以將腳本單獨(dú)存儲(chǔ)在 Elasticsearch 中,并在數(shù)據(jù)攝取管道(Ingest pipeline)中按需調(diào)用它們。
這種存儲(chǔ)腳本的方式,咱們之前也有過介紹,Elasticsearch 中有個(gè)專有術(shù)語(yǔ)名詞與之對(duì)應(yīng),叫:stored script 存儲(chǔ)腳本。通過 stored script 方式,可以在不同的地方重復(fù)使用同一段腳本,而無(wú)需復(fù)制代碼。
在Elasticsearch中使用 stored script 存儲(chǔ)腳本是一種高效且靈活的方法,特別適用于那些需要在多個(gè)數(shù)據(jù)處理場(chǎng)景中重復(fù)使用相同邏輯的場(chǎng)合。通過這種方式,可以構(gòu)建更加模塊化、易于管理的數(shù)據(jù)處理管道。
2、Base64 解碼的存儲(chǔ)腳本實(shí)現(xiàn)
如下腳本的目的是將源數(shù)據(jù)中的字段從Base64格式轉(zhuǎn)換為解碼后的文本。
2.1 創(chuàng)建 Base64 解碼腳本
PUT /_scripts/decodebase64
{
"script": {
"description": "Decode base64",
"lang": "painless",
"source": "def src=ctx[params['field']]; if (src == null) { return; } def target=params['target_field']; ctx[target]=src.decodeBase64();"
}
}
腳本解讀如下:
- PUT /_scripts/decodebase64: 這部分指示Elasticsearch創(chuàng)建或更新一個(gè)名為decodebase64的腳本。
- "script": 腳本的主體部分。
- "description": 腳本的描述,說(shuō)明了腳本的作用,即解碼Base64。
- "lang": 腳本的編寫語(yǔ)言,這里使用的是Elasticsearch的Painless腳本語(yǔ)言。
- "source": 腳本的具體內(nèi)容。這個(gè)腳本接受一個(gè)字段名作為輸入(params['field']),檢查是否為空,如果不為空,則將其Base64解碼并存儲(chǔ)在指定的目標(biāo)字段(params['target_field'])。
這個(gè)腳本可以在Elasticsearch的攝取管道中使用,用于在數(shù)據(jù)索引之前動(dòng)態(tài)地對(duì)字段進(jìn)行Base64解碼。
2.2 獲取存儲(chǔ)腳本
如下腳本僅驗(yàn)證,實(shí)戰(zhàn)中可忽略。
GET /_scripts/decodebase64
召回結(jié)果如下:
{
"_id": "decodebase64",
"found": true,
"script": {
"lang": "painless",
"source": "def src=ctx[params['field']]; if (src == null) { return; } def target=params['target_field']; ctx[target]=src.decodeBase64();"
}
}
注意:之前咱們很少這么用??醇?xì)節(jié),上面的召回結(jié)果有 "_id": "decodebase64", 你關(guān)注一下,一會(huì)就能用到!
2.3 創(chuàng)建使用Base64 解碼存儲(chǔ)腳本的管道
PUT /_ingest/pipeline/decodebase64
{
"description": "Decode hash values",
"processors": [
{
"script": {
"id": "decodebase64",
"params": {
"field": "name_base64",
"target_field": "name"
}
}
}
]
}
上述代碼創(chuàng)建了一個(gè)名為 decodebase64 的 Elasticsearch 攝取管道,其功能是使用存儲(chǔ)的腳本 decodebase64 將字段 name_base64 中的 Base64 編碼值解碼,并將解碼后的文本存儲(chǔ)到 name 字段中。
和咱們之前講的不同的地方、靈活的地方在于:field 和 target_field 變成了變量了,可以靈活按照項(xiàng)目需求替換之。
2.4 批量寫入數(shù)據(jù)的時(shí)候同時(shí)指定 pipeline
POST /fruits/_bulk?pipeline=decodebase64
{"index":{"_id":"1"}}
{"name_base64":"QXBwbGU="}
{"index":{"_id":"2"}}
{"name_base64":"QW5hbmFz"}
{"index":{"_id":"3"}}
{"name_base64":"Q2hlcnJ5"}
如上 bulk 批量寫入的時(shí)候指定 pipeline 的方式,咱們之前也少有講解。
GET fruits/_search
結(jié)果如下圖所示:
圖片
我們清晰的看到,咱們寫入的 name_base64 字段借助我們創(chuàng)建的管道、基于存儲(chǔ)腳本解碼為 name字段值。
不著急下結(jié)論,咱們?cè)倏匆唤M例子。
3、16進(jìn)制解碼的存儲(chǔ)腳本實(shí)現(xiàn)
步驟參見第2部分,咱們只講重點(diǎn)。
3.1 創(chuàng)建16進(jìn)制解碼存儲(chǔ)腳本
如下存儲(chǔ)腳本的目的:在Elasticsearch中創(chuàng)建并存儲(chǔ)一個(gè)名為decodehex的腳本,該腳本用于將HEX(十六進(jìn)制)編碼的字符串轉(zhuǎn)換為普通文本。
PUT /_scripts/decodehex
{
"script": {
"description": "Decode HEX",
"lang": "painless",
"source": "def src=ctx[params['field']]; if (src == null) { return; } def target=params['target_field']; StringBuilder sb = new StringBuilder(); for (int i = 0; i < src.length(); i += 2) { String byteStr = src.substring(i, i + 2); char byteChar = (char) Integer.parseInt(byteStr, 16); sb.append(byteChar) } ctx[target] = sb.toString();"
}
}
腳本解讀如下:
- PUT /_scripts/decodehex: 這部分指示Elasticsearch創(chuàng)建或更新一個(gè)名為decodehex的腳本。
- script: 腳本的主體部分。
- description: 腳本的描述,說(shuō)明了腳本的作用,即解碼HEX字符串。
- lang: 腳本的編寫語(yǔ)言,這里使用的是Elasticsearch的Painless腳本語(yǔ)言。
- source: 腳本的具體內(nèi)容。這個(gè)腳本接受一個(gè)字段名作為輸入(params['field']),檢查是否為空,如果不為空,則將其HEX編碼的內(nèi)容轉(zhuǎn)換為普通文本并存儲(chǔ)在指定的目標(biāo)字段(params['target_field'])。
如上腳本可以在Elasticsearch的攝取管道中使用,用于在數(shù)據(jù)索引之前動(dòng)態(tài)地對(duì)字段進(jìn)行 HEX 解碼。
3.2 獲取16進(jìn)制解碼存儲(chǔ)腳本
如下腳本僅驗(yàn)證,實(shí)戰(zhàn)中可忽略。
GET /_scripts/decodehex
召回結(jié)果如下:
圖片
3.3 創(chuàng)建使用16進(jìn)制解碼腳本的管道
PUT /_ingest/pipeline/decodehex
{
"description": "Decode hash values",
"processors": [
{
"script": {
"id": "decodehex",
"params": {
"field": "color_hex",
"target_field": "color"
}
}
}
]
}
該管道的功能是使用存儲(chǔ)的腳本 decodehex 來(lái)處理數(shù)據(jù):它會(huì)取 color_hex 字段中的HEX(十六進(jìn)制)編碼字符串,將其解碼成普通文本,并將解碼后的結(jié)果存儲(chǔ)到 color 字段中。這個(gè)過程主要用于在將數(shù)據(jù)索引到 Elasticsearch 之前自動(dòng)進(jìn)行數(shù)據(jù)轉(zhuǎn)換和預(yù)處理。
同樣,靈活的地方在于:field、target_field 是變量。
3.4 批量寫入數(shù)據(jù)的時(shí)候同時(shí)指定 pipeline
POST /fruits_ext/_bulk?pipeline=decodehex
{"index":{"_id":"1"}}
{"color_hex":"477265656e"}
{"index":{"_id":"2"}}
{"color_hex":"59656c6c6f77"}
{"index":{"_id":"3"}}
{"color_hex":"526564"}
如上 bulk 批量寫入的時(shí)候指定 pipeline 的方式,咱們之前也少有講解。
GET fruits_ext/_search
結(jié)果如下圖所示:
圖片
當(dāng)然,第2部分、第3部分的存儲(chǔ)腳本使用可以靈活的整合為一部分,如下所示。
PUT /_ingest/pipeline/decodehashes
{
"description": "Decode hash values",
"processors": [
{
"script": {
"id": "decodebase64",
"params": {
"field": "name_base64",
"target_field": "name"
}
}
},
{
"script": {
"id": "decodehex",
"params": {
"field": "color_hex",
"target_field": "color"
}
}
}
]
}
批量構(gòu)建數(shù)據(jù)結(jié)果:
POST /fruits_all/_bulk?pipeline=decodehashes
{"index":{"_id":"1"}}
{"name_base64":"QXBwbGU=","color_hex":"477265656e"}
{"index":{"_id":"2"}}
{"name_base64":"QW5hbmFz","color_hex":"59656c6c6f77"}
{"index":{"_id":"3"}}
{"name_base64":"Q2hlcnJ5","color_hex":"526564"}
執(zhí)行檢索效果:
圖片
4、小結(jié)
我們一起探索了如何在Elasticsearch中創(chuàng)建并存儲(chǔ)腳本,以及如何檢索這些腳本,以確認(rèn)它們的 id 和內(nèi)容。我們還學(xué)習(xí)了如何在數(shù)據(jù)處理的攝取管道中調(diào)用這些存儲(chǔ)的腳本。
通過這種方法,你可以有效地節(jié)省存儲(chǔ)空間,并減少因重復(fù)編寫相同腳本而可能出現(xiàn)的錯(cuò)誤。簡(jiǎn)而言之,你只需編寫和存儲(chǔ)一次腳本,就可以在多個(gè)地方反復(fù)使用,這無(wú)疑提高了工作效率,同時(shí)也使得數(shù)據(jù)處理過程更加流暢和可靠。