如果您有媒体搜索应用,可以使用元数据过滤搜索查询。本页介绍了如何使用元数据字段将搜索范围限定为特定的一组文档。
准备工作
确保您已创建媒体应用和数据存储区并提取了数据。如需了解详情,请参阅创建媒体数据存储区和创建媒体应用。
示例文档
请查看以下媒体文档示例。您可以在阅读本页内容时参考这些概念。
{"id":"172851","schemaId":"default_schema","jsonData":"{\"title\":\"Avatar: Creating the World of Pandora (2010)\",\"categories\":[\"Documentary\"],\"uri\":\"http://mytestdomain.movie/content/172851\",\"available_time\":\"2023-01-01T00:00:00Z\",\"media_type\":\"movie\"}"}
{"id":"243308","schemaId":"default_schema","jsonData":"{\"title\":\"Capturing Avatar (2010)\",\"categories\":[\"Documentary\"],\"uri\":\"http://mytestdomain.movie/content/243308\",\"available_time\":\"2023-01-01T00:00:00Z\",\"media_type\":\"movie\"}"}
{"id":"280218","schemaId":"default_schema","jsonData":"{\"title\":\"Avatar: The Way of Water (2022)\",\"categories\":[\"Action\",\"Adventure\",\"Sci-Fi\"],\"uri\":\"http://mytestdomain.movie/content/280218\",\"available_time\":\"2023-01-01T00:00:00Z\",\"media_type\":\"movie\"}"}
{"id":"72998","schemaId":"default_schema","jsonData":"{\"title\":\"Avatar (2009)\",\"categories\":[\"Action\",\"Adventure\",\"Sci-Fi\",\"IMAX\"],\"uri\":\"http://mytestdomain.movie/content/72998\",\"available_time\":\"2023-01-01T00:00:00Z\",\"media_type\":\"movie\"}"}
过滤器表达式语法
请务必了解您将用于定义搜索过滤条件的过滤表达式语法。过滤条件表达式语法可按以下扩展巴科斯范式总结:
# A single expression or multiple expressions that are joined by "AND" or "OR". filter = expression, { " AND " | "OR", expression }; # Expressions can be prefixed with "-" or "NOT" to express a negation. expression = [ "-" | "NOT " ], # A parenthetical expression. | "(", expression, ")" # A simple expression applying to a text field. # Function "ANY" returns true if the field contains any of the literals. ( text_field, ":", "ANY", "(", literal, { ",", literal }, ")" # A simple expression applying to a numerical field. Function "IN" returns true # if a field value is within the range. By default, lower_bound is inclusive and # upper_bound is exclusive. | numerical_field, ":", "IN", "(", lower_bound, ",", upper_bound, ")" # A simple expression that applies to a numerical field and compares with a double value. | numerical_field, comparison, double ); # Datetime field | datetime_field, comparison, literal_iso_8601_datetime_format); # A lower_bound is either a double or "*", which represents negative infinity. # Explicitly specify inclusive bound with the character 'i' or exclusive bound # with the character 'e'. lower_bound = ( double, [ "e" | "i" ] ) | "*"; # An upper_bound is either a double or "*", which represents infinity. # Explicitly specify inclusive bound with the character 'i' or exclusive bound # with the character 'e'. upper_bound = ( double, [ "e" | "i" ] ) | "*"; # Supported comparison operators. comparison = "<=" | "<" | ">=" | ">" | "="; # A literal is any double quoted string. You must escape backslash (\) and # quote (") characters. literal = double quoted string; text_field = text field - for example, category; numerical_field = numerical field - for example, score; datetime_field = field of datetime data type - for example available_time; literal_iso_8601_datetime_format = either a double quoted string representing ISO 8601 datetime or a numerical field representing microseconds from unix epoch.
过滤媒体搜索结果
如需使用元数据过滤媒体搜索结果,请按以下步骤操作:
查找数据存储区 ID。如果您已拥有数据存储区 ID,请跳至下一步。
在 Google Cloud 控制台中,前往 AI Applications 页面,然后在导航菜单中点击数据存储区。
点击您的数据存储区的名称。
在数据存储区的数据页面上,获取数据存储区 ID。
确定要过滤的文档字段。 例如,对于准备工作中的文档,您可以使用
categories
字段作为过滤条件。您只能在过滤条件表达式中使用可编入索引的字段。如需确定某个字段是否可编入索引,请执行以下操作:
在 Google Cloud 控制台中,前往 AI Applications 页面,然后在导航菜单中点击数据存储区。
点击您的数据存储区的名称。
在名称列中,点击相应数据存储区。
点击架构标签页,查看数据存储区的架构。 如果字段的 Indexable 为:
选择
,然后该字段即可用于过滤搜索结果;跳过第 3 步。未选择
,然后按照第 3 步启用该字段以进行索引编制。如果
不可用,则无法为相应字段编制索引。
如需使某个字段(例如
categories
字段)可过滤,请执行以下操作:在 Google Cloud 控制台中,前往 AI Applications 页面,然后在导航菜单中点击应用。
点击您的媒体搜索应用。
在导航菜单中,点击数据。
点击架构标签页。此标签页显示当前字段设置。
点击修改。
如果尚未选中,请选中类别行中的可编入索引复选框,然后点击保存。
等待 6 小时,以便架构修改传播。六小时后,您可以继续执行下一步。
获取搜索结果。
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/servingConfigs/default_search:search" \ -d '{ "query": "QUERY", "filter": "FILTER" }'
替换以下内容:
PROJECT_ID
:您的项目的 ID。DATA_STORE_ID
:数据存储区的 ID。QUERY
:要搜索的查询文本。FILTER
:一个文本字段,用于使用过滤表达式过滤搜索结果。
例如,假设您想搜索准备工作部分中的电影,并且只希望搜索结果包含以下电影:(1) 包含“阿凡达”一词,(2) 属于“纪录片”类别。为此,您需要在调用中添加以下语句:
"query": "avatar", "filter": "categories: ANY(\"Documentary\")"
如需了解详情,请参阅
search
方法。点击查看示例回答。
如果您执行的搜索与上述过程中的搜索类似,则会收到类似于以下内容的响应。请注意,响应仅包含《阿凡达》纪录片。
{ "results": [ { "id": "243308", "document": { "name": "projects/431678329718/locations/global/collections/default_collection/dataStores/rdds3_1698205785399/branches/0/documents/243308", "id": "243308", "structData": { "categories": [ "Documentary" ], "title": "Capturing Avatar (2010)", "uri": "http://mytestdomain.movie/content/243308", "media_type": "movie" } } }, { "id": "172851", "document": { "name": "projects/431678329718/locations/global/collections/default_collection/dataStores/rdds3_1698205785399/branches/0/documents/172851", "id": "172851", "structData": { "categories": [ "Documentary" ], "uri": "http://mytestdomain.movie/content/172851", "media_type": "movie", "title": "Avatar: Creating the World of Pandora (2010)" } } } ], "totalSize": 2, "attributionToken": "XfBcCgwIvIzJqwYQ2_qNxwMSJDY1NzEzNmY1LTAwMDAtMmFhMy05YWU3LTE0MjIzYmIwOGVkMiIFTUVESUEqII6-nRXFy_MXnIaOIsLwnhXUsp0VpovvF6OAlyKiho4i", "guidedSearchResult": {}, "summary": {} }
过滤可用文档
如果您希望搜索结果仅返回可用的文档,则需要在查询中添加相应过滤条件。有效证件是指 available_time
为过去日期,且未指定 expire_time
或 expire_time
设置为未来日期的证件。
过滤条件,用于仅返回当前可用的文档:
available_time <= \"DATE_TIME\" AND expire_time > \"DATE_TIME\"
将 DATE_TIME
替换为今天的日期,例如 2025-04-21
或 2025-04-21T00:00:00Z
。
评分、人物和组织的过滤条件
媒体分级、人物和组织的过滤条件语法是独一无二的,不遵循上述模式。您可以参考以下示例和可复制的过滤条件代码段,为评分、人物和组织创建过滤条件。
过滤条件会有所不同,具体取决于您使用的是 Google 预定义架构还是您自己的自定义架构。
评分、个人和组织的过滤条件(Google 预定义架构)
以下是评分、人物和组织过滤器的语法和示例:
按评分过滤:按指定来源的评分进行过滤。
rating(RATING_SOURCE, aggregate_ratings.rating_score) OPERATOR RATING_SCORE
替换以下内容:
RATING_SOURCE
:相应评级的来源。对于预定义架构,这是aggregate_ratings.rating_source
字段中的值。OPERATOR
:比较运算符之一,即<=
、<
、>=
、>
或=
RATING_SCORE
:[1,5] 范围内的评分值。对于预定义的架构,这是aggregate_ratings.rating_score
字段中的值。
示例:此过滤条件会将搜索范围限制为 IMDB 评分高于 2½ 星的电影。括号内的值解析为 IMDB 评分的值:
"filter": "rating(imdb, aggregate_ratings.rating_score) > 2.5"
过滤人员:按指定角色的名称过滤人员。
person(PERSONS_ROLE, persons.name): ANY NAME_STRING
替换以下内容:
PERSONS_ROLE
:对于预定义的架构,这是persons.role
字段中的值(director
、actor
、player
、team
、league
、editor
、author
、character
、contributor
、creator
、editor
、funder
、producer
、provider
、publisher
、sponsor
、translator
、music-by
、channel
或custom-role
)。NAME_STRING
:具有指定角色的一位或多位人员的名称。对于 curl 命令(例如第 4 步中的命令),双引号需要使用反斜杠字符进行转义。
示例:此过滤条件会将搜索范围限定为由布拉德·皮特或凯特·温斯莱特出演的电影。
filter: "person(actor, persons.name): ANY(\"Brad Pitt\", \"Kate Winslet\")"
过滤组织:按指定角色的组织名称进行过滤。
org(ORG_ROLE, organization.name): ANY NAME_STRING
替换以下内容:
ORG_ROLE
:对于预定义架构,这是organizations.role
字段中的值(director
、actor
、player
、team
、league
、editor
、author
、character
、contributor
、creator
、editor
、funder
、producer
、provider
、publisher
、sponsor
、translator
、music-by
、channel
或custom-role
)。NAME_STRING
:具有指定角色的一个或多个组织的名称。对于 curl 命令(例如第 4 步中的命令),需要使用反斜杠字符转义英文双引号。
此示例将搜索范围限制为制作组织为 Walt Disney Studios 的电影:
filter: "org(producer, organizations.name): ANY(\"Walt Disney Studios\")"
评分、个人和组织的过滤条件(自定义架构)
如果您使用自定义架构,请查看 Google 预定义架构部分,然后查看本部分中的示例。如需在自定义架构中使用评分、人员和组织过滤器,必须正确设置属性映射。如需了解属性映射,请参阅自定义架构。
过滤 | 要映射的媒体资源 |
---|---|
rating | media_aggregated_rating media_aggregated_rating_score media_aggregated_rating_source |
个人 | media_person media_person_name media_person_role |
org | media_organization media_organization_name media_organization_role |
自定义架构的评分过滤条件示例
此过滤条件会搜索烂番茄评分为 5 星的电影:
"filter": "rating(rotten_tomatoes, custom_rating.star_score) = 5"
rotten_tomatoes
是映射到 media_aggregated_rating_source
的字段中的值。custom_rating.star_score
是映射到 media_aggregated_rating.media_aggregated_rating_score
键属性的字段。
自定义架构的组织过滤条件示例
此过滤条件用于搜索音乐由伦敦交响乐团或好莱坞工作室交响乐团演奏的电影。
"filter: org(music-by, company.id): ANY (\"London Symphony Orchestra\", \"Hollywood Studio Symphony\" )
company.id
是映射到 media_organization_name
属性的字段的名称。music-by
是公司记录字段中映射到 media_organization_role
的值。