发送反馈
对结构化数据存储区中的结果进行排序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意
:此功能为预览版,受 GCP 服务专用条款 的“正式发布前产品条款”约束。我们对正式发布前产品和功能的支持可能比较有限,并且对正式发布前产品和功能的更改可能与其他正式发布前版本不兼容。如需了解详情,请参阅发布阶段说明 。此外,使用此功能,即表示您同意生成式 AI 预览版条款及条件 (“预览版条款”)。对于此功能,您可以按照云端数据处理附录 中的说明处理个人数据,但受到协议中的适用限制和义务(如“预览版条款”中所定义)的约束。
本页介绍了如何对对结构化数据存储区和包含元数据的非结构化数据存储区执行的搜索查询的结果进行排序。
用于对结果进行排序的支持的数据类型
您可以按以下字段类型对搜索结果进行排序:
string
number
datetime
geolocation
准备工作
确保您有一个应用,其数据存储区包含结构化数据或包含元数据的非结构化数据。
对搜索结果进行排序
如需对结构化数据存储区或包含元数据的非结构化数据存储区的搜索结果进行排序,请按以下步骤操作:
REST
如需使用此 API 对包含结构化数据或包含元数据的非结构化数据的应用的搜索结果进行排序,请使用 engines.servingConfigs.search
方法:
找到您的应用 ID。如果您已经有应用 ID,请跳至下一步。
在 Google Cloud 控制台中,前往 AI 应用 页面。
前往“应用”
在应用 页面上,找到应用的名称,并从 ID 列获取应用的 ID。
发出搜索查询并添加 orderBy
字段。
关键字 :在 Vertex AI Search 中,应用 一词在 API 上下文中可与引擎 一词互换使用。
curl -X POST -H "Authorization: Bearer $( gcloud auth print-access-token) " \
-H "Content-Type: application/json" \
"https://discoveryengine.googleapis.com/v1alpha/projects/PROJECT_ID /locations/global/collections/default_collection/engines/APP_ID /servingConfigs/default_search:search" \
-d '{
"servingConfig": "projects/PROJECT_ID /locations/global/collections/default_collection/engines/APP_ID /servingConfigs/default_search",
"query": "QUERY ",
"orderBy": "ORDER_BY "
}'
替换以下内容:
PROJECT_ID
:您的 Google Cloud 项目的 ID。
APP_ID
:Vertex AI Search 应用的 ID。
QUERY
:要搜索的查询文本。
ORDER_BY
:结果的排列顺序。默认排序顺序为升序。例如,指定 date
会返回从最旧到最新的结果。如需获取降序排序,请将 desc
附加到 date
值,例如 date desc
。如需查看更多示例,请参阅 orderBy
示例 。
命令和结果示例
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://discoveryengine.googleapis.com/v1alpha/projects/my-project-123/locations/global/collections/default_collection/engines/my-app/servingConfigs/default_search:search" \
-d '{
"query": "hotel",
"orderBy": "rating desc"
}'
{
"results": [
{
"id": "10d480b19c256bb1",
"document": {
"name": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/10d480b19c256bb1",
"id": "10d480b19c256bb1",
"structData": {
"available_date": "2023-11-05",
"amenities": [
"Lake Views",
"Private Beach",
"Spa",
"Water Activities"
],
"room_types": [
"Lakefront Suite",
"Deluxe Room",
"Cottage"
],
"location": {
"address": "988 Serenity Circle, Tranquil Town, NV 89501, USA"
},
"rating": 4.6,
"id": 11,
"price_per_night": 220.5,
"title": "Serenity Springs Hotel"
}
}
},
{
"id": "9ffae8af37cc8b63",
"document": {
"name": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/9ffae8af37cc8b63",
"id": "9ffae8af37cc8b63",
"structData": {
"title": "Riverfront Plaza Hotel",
"rating": 4.2,
"location": {
"address": "101 Main St, Anytown, CA 94501, USA"
},
"price_per_night": 145.8,
"amenities": [
"Fitness Center",
"Conference Rooms",
"Restaurant",
"Valet Parking"
],
"id": 3,
"room_types": [
"Executive Suite",
"King Room",
"Double Queen"
],
"available_date": "2023-11-15"
}
}
},
{
"id": "3be9e854d8f3a47f",
"document": {
"name": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/3be9e854d8f3a47f",
"id": "3be9e854d8f3a47f",
"structData": {
"amenities": [
"Oceanfront Views",
"Pool",
"Spa",
"Beachside Dining"
],
"available_date": "2023-09-18",
"location": {
"address": "449 Oceanfront Drive, Seaside Resort, CA 92007, USA"
},
"id": 13,
"title": "Ocean Breeze Hotel",
"room_types": [
"Ocean View Suite",
"Deluxe Room",
"Family Suite"
],
"rating": 4.1,
"price_per_night": 180
}
}
}
],
"totalSize": 3,
"attributionToken": "wAHwvwoMCNDYz7UGEMCklrsCEiQ2NmIzYmU5My0wMDAwLTIxMDUtYmUyNy01ODI0MjljMzdlZTQiB0dFTkVSSUMqgAHd1akt3e2ILebtiC2CspoigLKaIpjeqC-q-LMtjr6dFeqCsS2W3qgvwvCeFaz4sy2jgJcinta3LeiCsS3b7Ygt5O2ILa3Eii3Usp0Vpp-VLZzWty359rMt-_azLaCJsy3dj5oixcvzF6vEii2iibMttreMLd_VqS3bj5oipJ-VLQ",
"guidedSearchResult": {},
"summary": {}
}
在此示例中,包含“hotel”一词的文档按评分排序,从评分为 4.6 的 Serenity Springs Hotel 到评分为 4.1 的 Ocean Breeze Hotel。
orderBy
个示例
如需按名为 title
的字符串字段进行排序,请执行以下操作:
如需按名为 rating
的数字字段进行排序,请执行以下操作:
如需按名为 available_date
的日期时间字段对数据进行排序,请执行以下操作:
如需按名为 location
的字段中的地理位置对数据进行排序,请执行以下操作:
"orderBy": "GEO_DISTANCE(location, \"Mountain View, CA\")"
会按距离 Mountain View 的距离从近到远对文档进行排序。
"orderBy": "GEO_DISTANCE(location, 37.38, -122.08) desc"
会按距离 37.38°N、122.08°W 的位置从最远到最近的顺序对文档进行排序。
发送反馈
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可 获得了许可,并且代码示例已根据 Apache 2.0 许可 获得了许可。有关详情,请参阅 Google 开发者网站政策 。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-05-05。
需要向我们提供更多信息?
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-05-05。"],[[["This feature allows users to order search results from both structured and unstructured data stores with metadata."],["Supported field types for ordering include string, number, datetime, and geolocation."],["The `engines.servingConfigs.search` method is recommended for ordering search results when using the API, and can be used with an engine/app or a data store."],["The `orderBy` field in the search query allows you to specify the field and the desired sort order (ascending or descending), for example sorting by title, rating, or a date."],["Geolocation-based ordering is supported using `GEO_DISTANCE` to order results by proximity to a specified location, which can be an address or latitude and longitude."]]],[]]