提供意見
使用自然語言理解技術進行篩選
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意
:
這項功能是預先發布版產品,使用時須遵守《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」 頁面,然後在導覽選單中點選「Data Stores」 。
前往「資料儲存庫」頁面
點按資料儲存庫的名稱。
在資料儲存庫的「資料」 頁面中,取得資料儲存庫 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-08-04 (世界標準時間)。
想進一步說明嗎?
[[["容易理解","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-08-04 (世界標準時間)。"],[[["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."]]],[]]