重命名字段处理器
rename_field 搜索响应处理器能够拦截搜索响应,并对指定的字段进行重命名。当您的索引和应用程序对同一字段使用了不同的命名时,这个功能非常有用。例如,如果您在索引中重命名了一个字段,rename_field 处理器可以在将响应发送给您的应用程序之前,将新名称改回旧名称。
请求体字段
下表列出了所有可用的请求字段。
| 字段 | 数据类型 | 描述 |
|---|---|---|
field |
字符串 | 需要重命名的字段。必需。 |
target_field |
字符串 | 新的字段名称。必需。 |
tag |
字符串 | 处理器的标识符。 |
description |
字符串 | 处理器的描述信息。 |
ignore_failure |
布尔值 | 如果设置为 true,UDB-SX 将忽略此处理器的任何失败,并继续运行搜索管道中的其余处理器。可选。默认值为 false。 |
示例
以下示例演示了如何使用包含 rename_field 处理器的搜索管道。
准备工作
创建一个名为 my_index 的索引,并为包含 message 字段的文档建立索引:
POST /my_index/_doc/1
{
"message": "This is a public message",
"visibility":"public"
}
创建搜索管道
以下请求创建了一个搜索管道,其中包含一个 rename_field 响应处理器,该处理器会将字段 message 重命名为 notification:
PUT /_search/pipeline/my_pipeline
{
"response_processors": [
{
"rename_field": {
"field": "message",
"target_field": "notification"
}
}
]
}
使用搜索管道
在不使用搜索管道的情况下,搜索 my_index 中的文档:
GET /my_index/_search
响应中包含字段 message:
响应
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_index",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"message" : "This is a public message",
"visibility" : "public"
}
}
]
}
}
若要使用管道进行搜索,请在 search_pipeline 查询参数中指定管道名称:
GET /my_index/_search?search_pipeline=my_pipeline
此时,message 字段已被重命名为 notification:
响应
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "my_index",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"visibility" : "public",
"notification" : "This is a public message"
}
}
]
}
}
您也可以使用 fields 选项来搜索文档中的特定字段:
POST /my_index/_search?pretty&search_pipeline=my_pipeline
{
"fields":["visibility", "message"]
}
在响应中,字段 message 已被重命名为 notification:
响应
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "my_index",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"visibility" : "public",
"notification" : "This is a public message"
},
"fields" : {
"visibility" : [
"public"
],
"notification" : [
"This is a public message"
]
}
}
]
}
}