如果您有媒體搜尋應用程式,可以使用中繼資料篩選搜尋查詢。本頁面說明如何使用中繼資料欄位,將搜尋範圍限制在特定文件。
事前準備
請確認您已建立媒體應用程式和資料儲存庫,並擷取資料。詳情請參閱「建立媒體資料儲存庫」和「建立媒體應用程式」。
文件範例
請查看這些媒體文件範例。閱讀本頁面時,您可以隨時參考這些定義。
{"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.
篩選媒體搜尋
如要使用中繼資料篩選媒體搜尋結果,請按照下列步驟操作:
找出應用程式 ID。如果您已有應用程式 ID,請跳到下一個步驟。
前往 Google Cloud 控制台的「AI Applications」頁面。
在「Apps」(應用程式) 頁面中,找出應用程式名稱,然後從「ID」欄取得應用程式 ID。
決定要篩選的文件欄位。舉例來說,對於「事前準備」中的文件,您可以使用
categories欄位做為篩選條件。篩選運算式只能使用可建立索引的欄位。如要判斷欄位是否可建立索引,請按照下列步驟操作:
前往 Google Cloud 控制台的「AI Applications」頁面,然後點選導覽選單中的「Data Stores」(資料儲存庫)。
點按資料儲存庫的名稱。
在「Name」(名稱) 欄中,點選資料儲存庫。
點選「結構定義」分頁標籤,查看資料儲存庫的結構定義。如果欄位的「可建立索引」為:
選取「」,該欄位即可用於篩選搜尋結果,請略過步驟 3。
未選取 ,請按照步驟 3 啟用欄位索引功能。
如果 無法使用,則無法為欄位建立索引。
如要讓欄位 (例如
categories欄位) 可篩選,請按照下列步驟操作:前往 Google Cloud 控制台的「AI Applications」頁面,然後點選導覽選單中的「Apps」。
按一下媒體搜尋應用程式。
點按導覽選單中的「資料」。
按一下 [Schema] (結構定義) 分頁標籤。這個分頁會顯示目前的欄位設定。
按一下 [編輯]。
如果尚未選取,請勾選「categories」列中的「Indexable」核取方塊,然後按一下「Save」。
請等待六小時,讓結構定義編輯內容生效。六小時後,即可繼續執行下列步驟。
取得搜尋結果。
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),雙引號必須使用反斜線字元逸出。
範例:這項篩選條件會將搜尋範圍限制在演員為布萊德彼特或凱特溫絲蕾的電影。
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 預先定義的結構定義」一節,然後再查看本節中的範例。如要在自訂結構定義中使用評分、人員和機構篩選器,必須正確設定屬性對應。如要瞭解屬性對應,請參閱「自訂結構定義」。
| 篩選器 | 要對應的屬性 |
|---|---|
| 評分 | media_aggregated_ratingmedia_aggregated_rating_scoremedia_aggregated_rating_source |
| 使用者圖示 | media_personmedia_person_namemedia_person_role |
| 組織 | media_organizationmedia_organization_namemedia_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 的值。