过滤器查询处理器

filter_query 搜索请求处理器会拦截搜索请求,并向请求应用一个额外的查询,从而过滤结果。当您不希望重写应用程序中的现有查询,但需要对结果进行额外过滤时,这非常有用。

请求正文字段

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

字段 数据类型 描述
query 对象 查询领域特定语言(DSL)中的查询。有关 UDB-SX 查询类型的列表,请参阅查询DSL。必需。
tag 字符串 处理器的标识符。可选。
description 字符串 处理器的描述。可选。
ignore_failure 布尔值 如果为 true,UDB-SX 会忽略此处理器的任何失败,并继续运行搜索管道中的其余处理器。可选。默认为 false

示例

以下示例演示了使用带有 filter_query 处理器的搜索管道。

设置

创建一个名为 my_index 的索引,并索引两个文档,一个公开,一个私有:

POST /my_index/_doc/1
{
  "message": "This is a public message", 
  "visibility":"public"
}
POST /my_index/_doc/2
{
  "message": "This is a private message", 
  "visibility": "private"
}

创建搜索管道

以下请求创建一个名为 my_pipeline 的搜索管道,其中包含一个 filter_query 请求处理器,该处理器使用词项查询仅返回公开消息:

PUT /_search/pipeline/my_pipeline 
{
  "request_processors": [
    {
      "filter_query" : {
        "tag" : "tag1",
        "description" : "此处理器将限制为公开可见的文档",
        "query" : {
          "term": {
            "visibility": "public"
          }
        }
      }
    }
  ]
}

使用搜索管道

在没有搜索管道的情况下搜索 my_index 中的文档:

GET /my_index/_search

响应包含两个文档:

响应
{
  "took" : 47,
  "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"
        }
      },
      {
        "_index" : "my_index",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "message" : "This is a private message",
          "visibility" : "private"
        }
      }
    ]
  }
}

要使用管道进行搜索,请在 search_pipeline 查询参数中指定管道名称:

GET /my_index/_search?search_pipeline=my_pipeline

响应仅包含具有 public 可见性的文档:

响应
{
  "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"
        }
      }
    ]
  }
}