本页介绍了如何为具有网站数据的搜索应用过滤搜索查询。
准备工作
请确保您已创建应用和数据存储区,并且已将网站数据提取到数据存储区中。如需了解详情,请参阅创建搜索应用。
过滤表达式简介
使用过滤表达式来构建网站搜索过滤条件。过滤条件的构建方式因您是否已启用高级网站编入索引而异。请根据您使用的是基本网站搜索还是高级网站索引编制,参阅以下部分之一:
基本网站搜索的过滤表达式
本部分介绍了基本网站搜索(高级网站索引编制功能已关闭)中的过滤表达式行为。
基本网站搜索的语法
以下扩展的 Backus-Naur 范式总结了在您使用基本网站搜索功能时构建网站搜索过滤器的过滤条件表达式语法。过滤条件中冒号后面的双引号必须严格遵守。
# A single expression or multiple expressions that are joined by "AND". filter = expression, { "AND", expression }; expression = # A simple expression applying to a text url string. | filter_key, ":", \"text_value\" filter_key = (cr | highRange | lowRange | fileType | lr | rights | siteSearch); text_value = string value to filter on;
基本网站搜索可用的字段
如果您使用的是基本网站搜索,则可以使用以下字段来过滤网站搜索结果:
cr
(字符串)将搜索结果限制为来自特定国家/地区的文档。 如需查看支持的值的列表,请参阅国家/地区集合值。highRange
(字符串)指定搜索范围的上限。如果文档包含数字,则该数字必须小于或等于highRange
的值,才能将相应文档纳入到响应中。同时指定highRange
和lowRange
,以创建位于这些参数范围内的搜索查询。lowRange
(字符串)指定搜索范围的下限。如果文档包含数字,则该数字必须大于或等于lowRange
的值,文档才能包含在响应中。同时指定lowRange
和highRange
,以创建位于这些参数范围内的搜索查询。fileType
(字符串)将搜索结果限制为具有指定扩展名的文档。如需查看支持的文件类型列表,请参阅可供 Google 编入索引的文件类型。lr
(字符串)将搜索结果限制为以指定语言撰写的文档。如需查看支持的语言列表,请参阅查询参数 (lr)。rights
(字符串)根据许可过滤搜索结果。如需了解支持的值,请参阅查询参数(版权)。siteSearch
(字符串)指定网页的网址模式,您的查询应搜索这些网页。
基本网站搜索示例
以下是一些基本网站搜索的过滤条件示例:
{"filter": "cr:\"countryUS\" AND siteSearch:\"https://example.com/example_domain\""}
过滤出符合以下条件的文档:(1) 来自美国,(2) 位于网域
https://example.com/example_domain
中。{"filter": "fileType:\".pdf\" AND lr:\"lang_en\""}
过滤出以下文档:(1) PDF 文件;(2) 采用英语。
{"filter": "rights:\"cc_publicdomain\""}
用于过滤属于公共领域的文档。
使用高级网站索引编制功能的过滤表达式
本部分介绍了在启用高级网站索引编制功能的情况下,过滤表达式的行为。
高级网站索引编制功能的可用字段
启用高级网站索引编制功能后,您可以使用以下字段过滤网站搜索结果:
siteSearch
(字符串):您的查询应搜索的网页的网址格式。meta
标记名称和 PageMap 属性名称:网页中的结构化数据,可添加到数据存储区的架构中,使相应字段可供搜索、检索和编入索引。如需了解详情,请参阅使用结构化数据进行高级网站索引编制。
高级网站索引编制的语法
以下扩展的 Backus-Naur 范式总结了在您使用高级网站编入索引功能时,用于构建网站搜索过滤条件的过滤表达式语法。过滤条件中冒号后面的双引号必须严格遵守。
过滤 siteSearch
时,EBNF 语法为:
# A single expression or multiple expressions that are joined by "OR". filter = expression, { "OR", expression }; # Expressions can be prefixed with "-" or "NOT" to express a negation. expression = [ "-" | "NOT " ], # A simple expression applying to a text url string. | filter_key, ":", \"url_string\" filter_key = siteSearch; url_string = double quoted string representing a URL;
过滤 meta
标记名称和 PageMap 属性名称时,EBNF 语法如下:
# A single expression or multiple expressions that are joined by "OR". filter = expression, { "OR", expression }; # Expressions can be prefixed with "-" or "NOT" to express a negation. expression = [ "-" | "NOT " ], # Function "ANY" returns true if the field exactly matches any of the literals. | text_field, ":", "ANY", "(", literal, { ",", literal }, ")" literal = double quoted string; # text_field corresponds to the meta tag or PageMap Attribute name, for example, category text_field = text field;
高级网站索引编制示例
以下是一些使用 siteSearch
的高级网站索引编制过滤条件示例:
{"filter": "siteSearch:\"https://example.com/example_domain\""}
用于过滤网域
https://example.com/example_domain
中的文档。例如https://example.com/example_domain/index.html
。{"filter": "siteSearch:\"https://example.com/subdomains/*\""}
过滤出位于与
https://example.com/subdomains/*
匹配的任何网域中的文档。例如https://example.com/subdomains/example_subdomain_page
。{"filter": "siteSearch:\"https://altostrat.com/subdomain/pages/*\" OR siteSearch:\"http://cymbalgroup.com/pages/*\""}
过滤出位于与第一个或第二个网址模式匹配的任何网域中的文档。例如
https://altostrat.com/subdomain/pages/title_page
、https://cymbalgroup.com/subdomain/pages/title_page
或https://altostrat.com/subdomain/pages/INFO
。
以下是 meta
或 PageMap 属性名称过滤的一些示例:
{"filter": "product: ANY(\"networking\",\"compute\")"}
过滤包含
meta
标记或名为product
的 PageMap 属性且其值为networking
或compute
的文档。{"filter": "NOT product: ANY(\"storage\")"}
过滤不包含
meta
标记或 PageMap 属性名称product
(值为storage
)的文档。
如需详细了解用于编入索引的 meta
标记名称和 PageMap 属性名称,请参阅 meta
标记的使用场景示例和 PageMap 的使用场景示例。
过滤网站搜索结果
如需过滤网站搜索结果,请按以下步骤操作:
查找应用 ID。如果您已拥有应用 ID,请跳到下一步。
在 Google Cloud 控制台中,前往 AI Applications 页面。
在应用页面上,找到应用的名称,并从 ID 列获取应用的 ID。
如需过滤网站搜索结果,请使用
engines.servingConfigs.search
方法搭配filter
字段。curl -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "Content-Type: application/json" \ "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:search" \ -d '{ "servingConfig": "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search", "query": "QUERY", "filter": "FILTER" }'
替换以下内容:
PROJECT_ID
:您的项目的 ID。APP_ID
:应用的 ID。QUERY
:要搜索的查询文本。FILTER
:一个文本字段,用于使用过滤表达式过滤搜索结果。默认值为空字符串。如需了解如何为基本网站搜索构建过滤条件,请参阅使用基本网站搜索的过滤条件表达式。
如需了解如何为高级网站编入索引构建过滤条件,请参阅使用高级网站编入索引的过滤表达式。