过滤媒体搜索结果

如果您有媒体搜索应用,则可以使用元数据来过滤搜索查询。本页面介绍了如何使用元数据字段将搜索范围限制在特定的一组文档中。

准备工作

确保您已创建媒体应用和数据存储区,并已注入数据。如需了解详情,请参阅创建媒体数据存储区和创建媒体应用。

示例文档

查看这些示例媒体文档。在阅读本页内容时,您可以随时参考这些示例。

{"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 exactly matches any of the literals.
    | text_field, ":", "ANY", "(", literal, { ",", literal }, ")"
    # A simple expression applying to any indexable field.
    # Preview: Function "EXISTS" returns true if the field is not NULL, empty, or containing the default value.
    | text_field, ":", "EXISTS"
    | numerical_field, ":", "EXISTS"
    # 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.

如需使用元数据过滤媒体搜索结果,请按以下步骤操作:

  1. 找到应用 ID。如果您已拥有应用 ID,请跳到下一步。

    1. 在 Google Cloud 控制台中,前往 AI Applications 页面。

      前往应用

    2. 在应用页面上,找到应用的名称,并从 ID 列获取应用的 ID。

  2. 确定要过滤的一个或多个文档字段。例如,对于准备工作中的文档,您可以使用 categories 字段作为过滤条件。

    您只能在过滤表达式中使用可编入索引的字段。如需确定某个字段是否可编入索引,请执行以下操作:

    1. 在 Google Cloud 控制台中,前往 AI Applications 页面,然后在导航菜单中点击数据存储区。

      前往“数据存储区”页面

    2. 点击您的数据存储区的名称。

    3. 在名称列中,点击相应数据存储区。

    4. 点击架构标签页,以查看数据存储区的架构。 如果字段的可编入索引为:

      • 选择了 ,则该字段已准备好进行过滤以供搜索;跳过第 3 步。

      • 未选择 ,请按照第 3 步操作,为该字段启用索引。

      • 不可用 ,则无法为该字段编制索引。

  3. 如需使某个字段(例如 categories 字段)可过滤,请执行以下操作:

    1. 在 Google Cloud 控制台中,前往 AI Applications 页面,然后在导航菜单中点击应用。

      前往“应用”页面

    2. 点击您的媒体搜索应用。

    3. 在导航菜单中,点击数据。

    4. 点击架构标签页。此标签页显示当前的字段设置。

    5. 点击修改。

    6. 如果尚未选中,请选中类别行中的可编入索引复选框,然后点击保存。

    7. 等待 6 小时,以便架构修改生效。6 小时后,您可以继续执行以下步骤。

  4. 获取搜索结果。

    curl -X POST -H "Authorization: Bearer $(gcloud auth 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 '{
    "query": "QUERY",
    "filter": "FILTER"
    }'
    

    替换以下内容:

    • PROJECT_ID:您的项目的 ID。
    • APP_ID:应用的 ID。
    • QUERY:要搜索的查询文本。
    • FILTER:一个文本字段,用于使用过滤表达式过滤搜索。

    例如,假设您要搜索准备工作部分中的影片,并且只想搜索满足以下条件的影片的搜索结果:(1) 包含“avatar”一词,以及 (2) 属于“Documentary”类别。您可以通过在调用中包含以下语句来实现:

    "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 或将其设置为未来日期的文档。

过滤以仅返回当前可用的文档:

  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.5 星的电影。括号内的值会解析为 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 步中的命令),需要使用反斜杠字符对英文双引号进行转义。

    示例:此过滤条件会将搜索范围限制为演员之一是 Brad Pitt 或 Kate Winslet 的电影。

    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 的值。