所以我的文档中有两个字段
{
emails: ["", "", ""]
name: "",
}
我希望在文档被索引后有一个新字段称为uid,该字段将仅包含所有电子邮件的连接字符串以及每个文档的名称。
我可以在索引
_search
端点上使用此GET请求来获取类似脚本的字段{
"script_fields": {
"combined": {
"script": {
"lang": "painless",
"source": "def result=''; for (String email: doc['emails.keyword']) { result = result + email;} return doc['name'].value + result;"
}
}
}
}
我想知道我的摄取管道PUT请求主体应该是什么样子,如果我想用我的文档索引相同的脚本字段?
最佳答案
假设我有以下示例索引和示例文档。
样本来源索引
为了理解,我创建了以下映射。
PUT my_source_index
{
"mappings": {
"properties": {
"email":{
"type":"text"
},
"name":{
"type": "text"
}
}
}
}
样本文件:
POST my_source_index/_doc/1
{
"email": ["john@gmail.com","doe@outlook.com"],
"name": "johndoe"
}
只需按照以下步骤
步骤1:建立提取管道
PUT _ingest/pipeline/my-pipeline-concat
{
"description" : "describe pipeline",
"processors" : [
{
"join": {
"field": "email",
"target_field": "temp_uuid",
"separator": "-"
}
},
{
"set": {
"field": "uuid",
"value": "{{name}}-{{temp_uuid}}"
}
},
{
"remove":{
"field": "temp_uuid"
}
}
]
}
注意,我使用了Ingest API,其中我使用了三个processors,而creating the above pipeline则将按顺序执行:
temp_uuid
。 name
与temp_uuid
结合在一起。 temp_uuid
请注意,我使用
-
作为所有值之间的分隔符。您可以随意使用任何想要的东西。步骤2:建立目的地索引:
PUT my_dest_index
{
"mappings": {
"properties": {
"email":{
"type":"text"
},
"name":{
"type": "text"
},
"uuid":{ <--- Do not forget to add this
"type": "text"
}
}
}
}
步骤3:套用Reindex API:
POST _reindex
{
"source": {
"index": "my_source_index"
},
"dest": {
"index": "my_dest_index",
"pipeline": "my-pipeline-concat" <--- Make sure you add pipeline here
}
}
注意我在使用Reindex API时如何提到管道
步骤4:验证目标索引:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_dest_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "johndoe",
"uuid" : "johndoe-john@gmail.com-doe@outlook.com", <--- Note this
"email" : [
"john@gmail.com",
"doe@outlook.com"
]
}
}
]
}
}
希望这可以帮助!
关于elasticsearch - 如何使用摘要管道插入脚本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60629839/