提供意見
使用自然語言理解技術進行篩選
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意:
這項功能是預先發布版產品,使用時須遵守《GCP 服務專屬條款 》的「正式發布前產品條款」。正式發布前的產品和功能或許只提供部分支援,而且這類產品和功能的變更,可能與其他正式發布前版本不相容。詳情請參閱推出階段說明 。此外,使用這項功能即表示您同意《生成式 AI 預先發布版條款及細則 》(下稱「預先發布版條款」)。就這項功能而言,您可以按照《Cloud 資料處理附加條款 》所述方式處理個人資料,同時也必須遵守「協議」中的適用限制和義務 (相關定義請見「預先發布版條款」)。
本頁說明如何運用自然語言理解技術,自動為搜尋查詢建立篩選器,進而改善傳回結果的品質。
您可以搭配連結至結構化資料儲存庫的搜尋應用程式使用這項功能。
關於自然語言查詢理解
如果您有含有結構化資料的自訂搜尋應用程式,使用者的自然語言查詢可以重新格式化為篩選查詢。這比在查詢字串中搜尋字詞,更能提供品質優良的搜尋結果。
使用自然語言查詢理解功能比自行編寫篩選運算式更簡單,也更具彈性。如要瞭解如何編寫篩選運算式,請參閱「篩選結構化或非結構化資料的自訂搜尋 」。
這項功能最適合透過範例說明:
範例:從查詢中擷取欄位
我們將以搜尋飯店為例,說明這項自然語言查詢理解功能。
以飯店網站的結構化資料儲存庫為例,以下是其中一個查詢:「請幫我找間適合親子同行的飯店,至少四星級,每晚價格低於 300 美元,可攜帶狗狗入住,並提供免費 Wi-Fi。」
如果沒有自然語言查詢理解功能,搜尋應用程式會尋找包含查詢中字詞的文件。
透過自然語言查詢理解功能和適當的結構化資料,您可以使用篩選器取代查詢中的部分自然語言,讓搜尋更有效。如果結構化資料包含 star_rating
(數字)、price
(數字) 和 amenities
(字串) 的欄位,則可以將查詢重寫為下列篩選器加上剩餘查詢:
從自然語言查詢中擷取的篩選器:
{
"star_rating": ≥4,
"price": ≤300,
"amenities": "Wifi", "Pets Allowed"
}
剩餘查詢,在擷取篩選條件後重新定義:
family-friendly
範例:使用地理位置篩選器
這個範例與前一個範例類似,差別在於它包含地理位置 篩選器,這是一種特殊的擷取篩選器。Vertex AI Search 可辨識查詢中的地點,並為這些地點建立鄰近篩選器。
以向全州商家網站提出的以下查詢為例:「請幫我找出位於舊金山的至少 4 星級時尚飯店。」
透過自然語言查詢理解和地理位置篩選器,搜尋內容會重新編寫為篩選器和剩餘查詢:
從自然語言查詢中擷取的篩選條件:至少 4 星評等,且位於舊金山半徑 10 公里內:
{
"star_rating": ≥4,
"location": GEO_DISTANCE(\"San Francisco, CA\", 10000)
}
在本例中,GEO_DISTANCE
是地址,但在其他查詢中,即使原始查詢包含地址,它可能會以經緯度寫入。
剩餘查詢,在擷取篩選條件後重新定義:
chic and stylish in San Francisco
雖然已建立地理位置篩選器,但地點名稱仍會保留在剩餘的查詢中。這與其他篩選器 (例如 star_rating
) 不同。
限制
自然語言查詢理解功能有以下限制:
自然語言查詢理解功能無法套用至混合搜尋應用程式 。如果您嘗試在混合搜尋應用程式中使用自然語言查詢理解功能,系統會顯示錯誤訊息。
自然語言查詢理解功能僅適用於使用結構化資料儲存庫的自訂搜尋應用程式。
使用自然語言查詢理解功能會增加延遲時間,因此如果延遲時間是問題,您可以選擇不使用這項功能。
對於地理位置,您必須明確描述位置。您無法使用「附近」或「住家」等地點。
地理位置的半徑為 10 公里,無法設定。
事前準備
開始使用自然語言查詢理解功能前,您必須先為與要使用的應用程式連結的結構化資料儲存庫啟用這項功能。
如要啟用自然語言查詢理解功能,請按照下列步驟操作:
REST
找出資料儲存庫 ID。如果您已取得資料儲存庫 ID,請略過至下一個步驟。
前往 Google Cloud 控制台的「AI Applications」 頁面,然後在導覽選單中按一下「資料儲存庫」 。
前往「資料儲存庫」頁面
點按資料儲存庫的名稱。
在資料儲存庫的「資料」 頁面中,取得資料儲存庫 ID。
執行下列 curl 指令:
curl -X PATCH \
-H "Authorization: Bearer $( gcloud auth print-access-token) " \
-H "Content-Type: application/json" \
-H "X-Goog-User-Project: PROJECT_ID " \
"https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID /locations/global/collections/default_collection/dataStores/DATA_STORE_ID ?update_mask=natural_language_query_understanding_config.mode" \
-d '{
"naturalLanguageQueryUnderstandingConfig": {
"mode": "ENABLED"
}
}'
更改下列內容:
PROJECT_ID
: Google Cloud 專案的 ID。
DATA_STORE_ID
:Vertex AI Search 資料儲存庫的 ID。
注意: 如果您想為資料儲存庫停用自然語言查詢理解功能,請執行上述指令,並將 ENABLED
替換為 DISABLED
。
指令和回應範例
curl -X PATCH
-H "Authorization: Bearer $(gcloud auth print-access-token)"
-H "Content-Type: application/json"
-H "X-Goog-User-Project: my-project-123"
"https://discoveryengine.googleapis.com/v1beta/projects/my-project-123/locations/global/collections/default_collection/dataStores/my-data-store?update_mask=natural_language_query_understanding_config.mode"
-d '{
"naturalLanguageQueryUnderstandingConfig": {
"mode": "ENABLED"
}
}'
{
"name": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store",
"displayName": "test_data_store",
"industryVertical": "GENERIC",
"createTime": "2024-07-10T18:50:01.673414Z",
"solutionTypes": [
"SOLUTION_TYPE_SEARCH"
],
"defaultSchemaId": "default_schema",
"documentProcessingConfig": {
"name": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/documentProcessingConfig",
"defaultParsingConfig": {
"digitalParsingConfig": {}
}
},
"servingConfigDataStore": {},
"naturalLanguageQueryUnderstandingConfig": {
"mode": "ENABLED"
}
}
針對每個資料儲存庫重複執行步驟 1 和 2。
等待約 24 小時。
如果您在資料儲存庫準備就緒前嘗試使用自然語言查詢理解功能,您會收到的回應,就會與 filterExtractionCondition
設為 DISABLED
時一樣。
搜尋:將自然語言查詢轉換為篩選器
如要以自然語言查詢,並取得針對自然語言查詢最佳化的結果,請按照下列步驟操作:
REST
執行下列 curl 指令,呼叫 search
方法:
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/engines/APP_ID /servingConfigs/default_search:search" \
-d '{
"query": "QUERY ",
"naturalLanguageQueryUnderstandingSpec": {
"filterExtractionCondition": "ENABLED"
}
}'
更改下列內容:
PROJECT_ID
: Google Cloud 專案的 ID。
APP_ID
:您要查詢的 Vertex AI Search 應用程式 ID。應用程式必須連結至含有結構化資料的資料儲存庫。應用程式不得是混合搜尋應用程式。
QUERY
:查詢是以自然語言書寫。
指令和結果範例
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)"
-H "Content-Type: application/json"
"https://discoveryengine.googleapis.com/v1beta/projects/123456
/locations/global/collections/default_collection/engines/my-app/servingConfigs/default_search:search"
-d '{
"query": "Find me a family-friendly hotel with at least four stars that costs less than 300 a night, lets me bring my dog, and has free Wi-Fi.",
"naturalLanguageQueryUnderstandingSpec": {
"filterExtractionCondition": "ENABLED"}
}'
{
"results": [
{
"id": "b2617d862",
"document": {
"name": "projects/123456/locations/us/collections/default_collection/dataStores/my-data-store/branches/0/documents/b2617d862",
...
}
},
{
"id": "a51841841",
"document": {
"name": "projects/123456/locations/us/collections/default_collection/dataStores/my-data-store/branches/0/documents/a51841841",
...
}
}
],
"naturalLanguageQueryUnderstandingInfo": {
"extractedFilters": "(amenities: ANY(\"Pets Allowed\") AND amenities: ANY(\"Wifi\") AND star_rating: >= 4 AND price: < 300)",
"rewrittenQuery": "family-friendly",
"extractedFilterStructured": {
"expression": {
"andExpr": {
"expressions": [
{
"numberConstraint": {
"fieldName": "star_rating",
"value": 4,
"comparison": "GREATER_THAN_EQUALS",
}
},
{
"numberConstraint": {
"fieldName": "price",
"value": 300,
"comparison": "LESS_THAN",
}
},
{
"stringConstraint": {
"fieldName": "amenities",
"any": ["Pets Allowed"]
}
},
{
"stringConstraint": {
"fieldName": "amenities",
"any": ["Wifi"]
}
},
]
}
搜尋,將查詢中的地點轉換為地理位置篩選條件
如要以自然語言查詢,並取得針對自然語言查詢最佳化的結果 (包括與地點的距離),請按照下列步驟操作:
REST
執行下列 curl 指令,呼叫 search
方法:
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/engines/APP_ID /servingConfigs/default_search:search" \
-d '{
"query": "QUERY ",
"naturalLanguageQueryUnderstandingSpec": {
"filterExtractionCondition": "ENABLED",
"geoSearchQueryDetectionFieldNames": ["GEO_FIELD_NAME_1 ", "GEO_FIELD_NAME_N "]"
}
}'
更改下列內容:
PROJECT_ID
: Google Cloud 專案的 ID。
APP_ID
:您要查詢的 Vertex AI Search 應用程式 ID。應用程式必須連結至含有結構化資料的資料儲存庫。應用程式不得是混合搜尋應用程式。
QUERY
:查詢是以自然語言書寫。
GEO_FIELD_NAME_1, GEO_FIELD_NAME_N
:類型為 geolocation
的值清單。如果值類型不是 geolocation
,系統會略過這個欄位。
指令和結果範例
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)"
-H "Content-Type: application/json"
"https://discoveryengine.googleapis.com/v1beta/projects/123456
/locations/global/collections/default_collection/engines/my-app/servingConfigs/default_search:search"
-d '{
"query": "Find me a chic and stylish hotel with at least 4 stars that is in San Francisco.",
"naturalLanguageQueryUnderstandingSpec": {
"filterExtractionCondition": "ENABLED",
"geoSearchQueryDetectionFieldNames": ["location"]}
}'
{
"results": [
{
"id": "b2617d862",
"document": {
"name": "projects/123456/locations/us/collections/default_collection/dataStores/my-data-store/branches/0/documents/b2617d862",
...
}
},
{
"id": "a51841841",
"document": {
"name": "projects/123456/locations/us/collections/default_collection/dataStores/my-data-store/branches/0/documents/a51841841",
...
}
}
],
"naturalLanguageQueryUnderstandingInfo": {
"extractedFilters": "(star_rating: >= 4 AND GEO_DISTANCE("location", "San Francisco", 10000))",
"rewrittenQuery": "chic and stylish",
"extractedFilterStructured": {
"expression": {
"andExpr": {
"expressions": [
{
"numberConstraint": {
"fieldName": "star_rating",
"value": 4,
"comparison": "GREATER_THAN_EQUALS",
}
},
{
"geolocationConstraint": {
"fieldName": "location",
"address": "San Francisco",
"radius_in_meters": 10000,
}
},
]
}
提供意見
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權 ,程式碼範例則為阿帕契 2.0 授權 。詳情請參閱《Google Developers 網站政策 》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-19 (世界標準時間)。
想進一步說明嗎?
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-06-19 (世界標準時間)。"],[[["This feature, in preview, uses natural-language understanding to convert user queries into filters, enhancing search results for apps connected to structured data."],["By extracting fields, such as star ratings, prices, and amenities, natural-language queries can be transformed into structured filters to refine searches beyond keyword matching."],["Geolocation is supported as a special case of filter extraction, where natural-language locations in queries are converted into proximity filters with a default radius of 10km."],["Natural-language query understanding is limited to generic search apps using structured data, cannot be applied to blended search apps, and is subject to increased latency, and requires explicitly described locations."],["To enable natural-language query understanding for a data store, you must use a REST API command to set it to `ENABLED`, then wait approximately 24 hours for the changes to become effective."]]],[]]