新聞中心
這篇文章主要介紹“怎么使用enrich processor”,在日常操作中,相信很多人在怎么使用enrich processor問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么使用enrich processor”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
華龍ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!
enrich processor 簡(jiǎn)介
ingest pipeline 可以在傳入的文檔被索引之前,對(duì)文檔進(jìn)行預(yù)處理,通過(guò) processor 中定義的一系列規(guī)則來(lái)修改文檔的內(nèi)容(例如大小寫(xiě)轉(zhuǎn)換等)。

在 Elasticsearch 7.5 版本引入了 enrich processor,可以將現(xiàn)有索引(source index)中的數(shù)據(jù)添加到傳入的文檔(incoming document)中。

比如,你可以在如下的場(chǎng)景中用到:
根據(jù)已知的 IP 地址識(shí)別 Web 服務(wù)或供應(yīng)商。
根據(jù)產(chǎn)品 ID 將產(chǎn)品信息添加到零售訂單中。
根據(jù)電子郵件地址補(bǔ)充聯(lián)系信息。
根據(jù)用戶(hù)坐標(biāo)添加郵政編碼。
使用 enrich processor
使用 enrich processor 有如下幾個(gè)步驟:
1.添加 enrich data:添加 document (enrich data)到一個(gè)或者多個(gè)的 source index 中,這些 document 中應(yīng)包含之后要添加到 incoming documents 中的數(shù)據(jù)。
2.創(chuàng)建 enrich policy:enrich policy 中應(yīng)至少包含如下參數(shù):
指定source index的。
指定 incoming documents 和 source index 用于匹配的屬性。
指定要添加到 incoming documents 中的屬性。
3.執(zhí)行 enrich policy:執(zhí)行完后會(huì)自動(dòng)創(chuàng)建相應(yīng)的 enrich index, enrich index 和普通索引不同,進(jìn)行了優(yōu)化。
4.在 ingest pipeline 使用 enrich processor:enrich processor 使用 enrich index 來(lái)查詢(xún)。
背景說(shuō)明
source index 的內(nèi)容如下:
| loc | num | company |
|---|---|---|
| 廣東省 | A1001 | 騰訊 |
| 上海市 | B1001 | Bilibili |
| 浙江省 | C1001 | 阿里巴巴 |
incoming document 傳入的文檔如下,通過(guò) num 字段查到對(duì)應(yīng) source index 中的 loc 的值,添加到 incoming document 中新增 enrich_loc 屬性中。
| num | company |
|---|---|
| A1001 | 騰訊 |
| B1001 | Bilibili |
| C1001 | 阿里巴巴 |
第一步:添加 enrich data
通過(guò) _bulk API 批量添加文檔到 location 索引,這些文檔和普通的文檔一樣。
POST _bulk
{"index": {"_index":"location"}}
{"loc":"廣東省","company":"騰訊","num":"A1001"}
{"index": {"_index":"location"}}
{"loc":"上海市","company":"Bilibili","num":"B1001"}
{"index": {"_index":"location"}}
{"loc":"浙江省","company":"阿里巴巴","num":"C1001"}第二步:創(chuàng)建 enrich policy
enrich policy 一旦創(chuàng)建,就不能更新或者修改。
PUT /_enrich/policy/my-policy
{
"match": {
"indices": "location", #source index 索引名,就是前面創(chuàng)建的 enrich data 對(duì)應(yīng)的索引
"match_field": "num", #source index 中的屬性名,用于incoming documents 和 source index 匹配的屬性,屬性名一樣都是 num
"enrich_fields": ["loc"], #添加到 incoming documents 中的屬性
# 可選,過(guò)濾 source index 的文檔,只有 loc.keyword 是上海市的 enrich data 才能將屬性添加到 incoming documents 中
"query": {
"match": {
"loc.keyword": "上海市"
}
}
}
}第三步:執(zhí)行 enrich policy
當(dāng)創(chuàng)建了 enrich policy 后,你可以通過(guò) execute enrich policy API 去執(zhí)行 enrich policy。當(dāng)執(zhí)行 enrich policy 后,會(huì)自動(dòng)創(chuàng)建 enrich index。
直接將 incoming document 與 source index 中的文檔匹配可能會(huì)很慢且占用大量資源。 為了加快處理速度,enrich processor 使用了 enrich index。 enrich index 包含來(lái)自 source index 的 enrich data,enrich index 具有一些特殊屬性可幫助簡(jiǎn)化它們:
它們是系統(tǒng)索引,這意味著它們由 Elasticsearch 在內(nèi)部進(jìn)行管理,僅適用于 enrich processor。
它們始終以 .enrich- * 開(kāi)頭。
它們是只讀的,這意味著你不能直接更改它們。
它們被強(qiáng)制合并以便快速檢索。
當(dāng) source index 中新增或者修改了數(shù)據(jù),只需要重新執(zhí)行 enrich policy 就可以更改 enrich index,從而更新 enrich processor。
通過(guò)以下命令執(zhí)行 enrich policy:
PUT /_enrich/policy/my-policy/_execute
查看自動(dòng)創(chuàng)建的 enrich index:
GET _cat/indices/.enrich* # 返回結(jié)果 green open .enrich-my-policy-1616136526661 Vxal9lLBSlKS5lmzMpFfwQ 1 3 1 0 13.4kb 3.3kb
我感覺(jué) enrich policy 這里有個(gè)小 bug,當(dāng)刪除 enrich policy 時(shí),例如刪除的 enrich policy 為 my-policy-1,會(huì)同時(shí)刪除 my-policy-1 的 enrich index 和 enrich policy ,但是如果原先還有個(gè) my-policy-2(兩個(gè) enrich policy 在-之前是一樣的),會(huì)把 my-policy-2 的 enrich index 也誤刪了(enrich policy 不刪)。
第四步:在 ingest pipeline 使用 enrich processor
PUT _ingest/pipeline/loc-pipeline
{
"processors": [
{
"enrich": {
"policy_name": "my-policy", #引用前面創(chuàng)建的 enrich policy
"field": "num", # incoming document 中的屬性名,用于和 source index 中的屬性匹配值
#在incoming document 中新增的屬性,
#包含在 enrich policy 中定義的 match_field 和 enrich_fields 的值
"target_field": "enrich_loc"
}
}
]
}驗(yàn)證
使用 simulate 用來(lái)調(diào)試 ingest pipeline的效果,由于 source index 中匹配到的 loc.keyword 不是上海市,不會(huì)對(duì)這個(gè)文檔進(jìn)行處理:
POST _ingest/pipeline/loc-pipeline/_simulate
{
"docs": [
{
"_source": {
"num": "A1001",
"company": "騰訊"
}
}
]
}
# 返回結(jié)果
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"company" : "騰訊",
"num" : "A1001"
},
"_ingest" : {
"timestamp" : "2021-03-19T06:56:45.754486259Z"
}
}
}
]
}這個(gè)文檔的 loc.keyword 是上海市,因此會(huì)添加上 enrich data 中指定的屬性:
POST _ingest/pipeline/loc-pipeline/_simulate
{
"docs": [
{
"_source": {
"num": "B1001",
"company": "Bilibili"
}
}
]
}
# 返回結(jié)果
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"company" : "Bilibili",
"enrich_loc" : {
"loc" : "上海市",
"num" : "B1001"
},
"num" : "B1001"
},
"_ingest" : {
"timestamp" : "2021-03-19T06:56:29.393585306Z"
}
}
}
]
}在 simulate 調(diào)試成功之后,我們?cè)诓迦胛臋n的時(shí)候指定 ingest pipeline:
# 方式一:?jiǎn)螚l插入
POST origin-location/_doc?pipeline=loc-pipeline
{
"num": "A1001",
"company": "騰訊"
}
POST origin-location/_doc?pipeline=loc-pipeline
{
"num": "B1001",
"company": "Bilibili"
}
# 方式二:批量插入
POST _bulk?pipeline=loc-pipeline
{"index":{"_index":"origin-location"}}
{"num":"A1001","company":"騰訊"}
{"index":{"_index":"origin-location"}}
{"num":"B1001","company":"Bilibili"}查看插入的結(jié)果:
GET origin-location/_search
#返回結(jié)果
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "origin-location",
"_type" : "_doc",
"_id" : "zXxLSXgBUc4opBV-QiOv",
"_score" : 1.0,
"_source" : {
"num" : "A1001",
"company" : "騰訊"
}
},
{
"_index" : "origin-location",
"_type" : "_doc",
"_id" : "znxLSXgBUc4opBV-SCPk",
"_score" : 1.0,
"_source" : {
"num" : "B1001",
"company" : "Bilibili",
"enrich_loc" : {
"loc" : "上海市",
"num" : "B1001"
}
}
}
]
}
}也可以指定索引默認(rèn)使用的 ingest pipeline ,這樣就不用每次在插入文檔的時(shí)候指定 ingest pipeline了:
# 指定索引默認(rèn)使用的 ingest pipeline
PUT origin-location2
{
"settings": {
"default_pipeline": "loc-pipeline"
}
}
# 插入數(shù)據(jù)
POST _bulk
{"index":{"_index":"origin-location2"}}
{"num":"A1001","company":"騰訊"}
{"index":{"_index":"origin-location2"}}
{"num":"B1001","company":"Bilibili"}
# 查看結(jié)果
GET origin-location2/_search
# 輸出結(jié)果
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "origin-location2",
"_type" : "_doc",
"_id" : "CXxPSXgBUc4opBV-oyTJ",
"_score" : 1.0,
"_source" : {
"num" : "A1001",
"company" : "騰訊"
}
},
{
"_index" : "origin-location2",
"_type" : "_doc",
"_id" : "CnxPSXgBUc4opBV-oyTJ",
"_score" : 1.0,
"_source" : {
"num" : "B1001",
"company" : "Bilibili",
"enrich_loc" : {
"loc" : "上海市",
"num" : "B1001"
}
}
}
]
}
}另外還可以使用 index template,通過(guò)正則表達(dá)式的方式匹配多個(gè)索引,來(lái)指定索引使用的 ingest pipeline:
# 使用 index template
PUT _template/my-template
{
"index_patterns": ["origin-*"],
"settings": {
"default_pipeline": "loc-pipeline"
}
}
# 插入數(shù)據(jù)
POST _bulk
{"index":{"_index":"origin-location3"}}
{"num":"A1001","company":"騰訊"}
{"index":{"_index":"origin-location3"}}
{"num":"B1001","company":"Bilibili"}
# 查看結(jié)果
GET origin-location3/_search
# 輸出結(jié)果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "origin-location3",
"_type" : "_doc",
"_id" : "XnxVSXgBUc4opBV-1yRp",
"_score" : 1.0,
"_source" : {
"num" : "A1001",
"company" : "騰訊"
}
},
{
"_index" : "origin-location3",
"_type" : "_doc",
"_id" : "X3xVSXgBUc4opBV-1yRp",
"_score" : 1.0,
"_source" : {
"num" : "B1001",
"company" : "Bilibili",
"enrich_loc" : {
"loc" : "上海市",
"num" : "B1001"
}
}
}
]
}
}到此,關(guān)于“怎么使用enrich processor”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
網(wǎng)站題目:怎么使用enrichprocessor
分享URL:http://www.dlmjj.cn/article/gejigd.html


咨詢(xún)
建站咨詢(xún)
