Link Search Menu Expand Document Documentation Menu

使用搜索管道

您可以通过以下方式使用搜索管道

为请求指定现有搜索管道

创建搜索管道后,您可以通过以下方式将管道与查询一起使用。有关将搜索管道与 filter_query 处理器一起使用的完整示例,请参阅filter_query 处理器示例

在查询参数中指定管道

您可以在 search_pipeline 查询参数中指定管道名称,如下所示

GET /my_index/_search?search_pipeline=my_pipeline

在请求正文中指定管道

您可以在搜索请求正文中提供搜索管道 ID,如下所示

GET /my-index/_search
{
    "query": {
        "match_all": {}
    },
    "from": 0,
    "size": 10,
    "search_pipeline": "my_pipeline"
}

对于多重搜索,您可以在搜索请求正文中提供搜索管道 ID,如下所示

GET /_msearch
{ "index": "test"}
{ "query": { "match_all": {} }, "from": 0, "size": 10, "search_pipeline": "my_pipeline"}
{ "index": "test-1", "search_type": "dfs_query_then_fetch"}
{ "query": { "match_all": {} }, "search_pipeline": "my_pipeline1" }

为请求使用临时搜索管道

作为创建搜索管道的替代方案,您可以定义一个临时搜索管道,仅用于当前查询

POST /my-index/_search
{
  "query" : {
    "match" : {
      "text_field" : "some search text"
    }
  },
  "search_pipeline" : {
    "request_processors": [
      {
        "filter_query" : {
          "tag" : "tag1",
          "description" : "This processor is going to restrict to publicly visible documents",
          "query" : {
            "term": {
              "visibility": "public"
            }
          }
        }
      }
    ],
    "response_processors": [
      {
        "rename_field": {
          "field": "message",
          "target_field": "notification"
        }
      }
    ]
  }
}

使用此语法,管道不会持久化,仅用于指定它的查询。

默认搜索管道

为了方便起见,您可以为索引设置默认搜索管道。一旦您的索引有了默认管道,您就不需要在每个搜索请求中指定 search_pipeline 查询参数。

为索引设置默认搜索管道

要为索引设置默认搜索管道,请在索引设置中指定 index.search.default_pipeline

PUT /my_index/_settings 
{
  "index.search.default_pipeline" : "my_pipeline"
}

my_index 设置默认管道后,您可以尝试对所有文档进行相同的搜索

GET /my_index/_search

响应中只包含公共文档,表明管道已默认应用

响应
{
  "took" : 19,
  "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" : {
          "message" : "This is a public message",
          "visibility" : "public"
        }
      }
    ]
  }
}

您可以跨多个共享相同默认管道的索引进行搜索。例如,alias1 有两个索引,my_index1my_index2,它们都附加了默认管道 my_pipeline

GET /alias1/_search

响应中只包含文档的公共版本,证实默认管道已成功应用

响应
{
    "took": 59,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "my_index1",
                "_id": "1",
                "_score": 0.0,
                "_source": {
                    "message": "This is a public message",
                    "visibility": "public"
                }
            }
        ]
    }
}

为请求禁用默认管道

如果您想运行搜索请求而不应用默认管道,可以将 search_pipeline 查询参数设置为 _none

GET /my_index/_search?search_pipeline=_none

移除默认管道

要从索引中移除默认管道,请将其设置为 null_none

PUT /my_index/_settings 
{
  "index.search.default_pipeline" : null
}

PUT /my_index/_settings 
{
  "index.search.default_pipeline" : "_none"
}