Link Search Menu Expand Document Documentation Menu

重命名字段处理器

2.8 版本引入

rename_field 搜索响应处理器拦截搜索响应并重命名指定的字段。当您的索引和应用程序对同一字段使用不同的名称时,这非常有用。例如,如果您在索引中重命名字段,rename_field 处理器可以在将响应发送到应用程序之前,将新名称更改回旧名称。

请求正文字段

下表列出了所有可用的请求字段。

字段 数据类型 描述
field 字符串 要重命名的字段。必填。
target_field 字符串 新字段名称。必填。
tag 字符串 处理器的标识符。
description 字符串 处理器的描述。
ignore_failure 布尔型 如果为 true,OpenSearch 将忽略此处理器的任何失败 并继续运行搜索管道中的其余处理器。可选。默认值为 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"
          ]
        }
      }
    ]
  }
}

剩余 350 字符

有问题?

想贡献吗?