Query API 的用途是使用过滤条件从集合中检索数据对象。这类似于查询数据库表和使用 SQL WHERE 子句。您还可以使用聚合来获取与过滤条件匹配的数据对象的数量。
过滤表达式语言
除了 KNN/ANN 搜索功能之外,Agent Retrieval 还使用自定义查询语言提供多功能查询功能。下表介绍了查询语言。
| 过滤 | 说明 | 受支持的类型 | 示例 |
|---|---|---|---|
| $eq | 匹配字段值等于 指定值的数据对象。 | 数字、字符串、布尔值 | {"genre": {"$eq": "documentary"}} |
| $ne | 匹配字段值不等于 指定值的数据对象。 | 数字、字符串、布尔值 | {"genre": {"$ne": "drama"}} |
| $gt | 匹配字段值大于 指定值的数据对象。 | 数字 | {"year": {"$gt": 2019}} |
| $gte | 匹配字段值大于或等于 指定值的数据对象。 | 数字 | {"year": {"$gte": 2020}} |
| $lt | 匹配字段值小于 指定值的数据对象。 | 数字 | {"year": {"$lt": 2020}} |
| $lte | 匹配字段值小于或等于 指定值的数据对象。 | 数字 | {"year": {"$lte": 2020}} |
| $in | 匹配字段值位于 指定数组中的数据对象。 | 字符串 | {"genre": {"$in": ["comedy", "documentary"]}} |
| $nin | 匹配字段值不在 指定数组中的数据对象。 | 字符串 | {"genre": {"$nin": ["comedy", "documentary"]}} |
| $and | 使用逻辑 AND 联接查询子句。 | - | {"$and": [{"genre": {"$eq": "drama"}}, {"year": {"$gte": 2020}}]} |
| $or | 使用逻辑 OR 联接查询子句。 | - | {"$or": [{"genre": {"$eq": "drama"}}, {"year": {"$gte": 2020}}]} |
| $all | 选择字段的数组值包含所有指定值的文档。 | - | {"colors": {"$all": ["red", "blue"]}} |
查询集合
以下示例演示了如何使用过滤条件查询 ID 为 COLLECTION_ID 的集合中的数据对象。
REST
在使用任何请求数据之前, 请先进行以下替换:
- COLLECTION_ID:集合的 ID。
- LOCATION:您使用 Agent Platform 的区域。
- PROJECT_ID:您的 Google Cloud 项目 ID。
HTTP 方法和网址:
POST https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects:query
请求 JSON 正文:
{
"page_size": 10,
"page_token": "",
"filter": {
"$or": [
{
"director": {
"$eq": "Akira Kurosawa"
}
},
{
"$and": [
{
"director": {
"$eq": "David Fincher"
}
},
{
"genre": {
"$ne": "Thriller"
}
}
]
}
]
},
"output_fields": {
"data_fields": "*",
"vector_fields": "*",
"metadata_fields": "*"
}
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"dataObjects": [
{
"name": "projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/1",
"createTime": "2026-02-04T14:35:29Z",
"updateTime": "2026-02-04T14:37:29Z",
"data": {
"title": "Seven Samurai",
"director": "Akira Kurosawa",
"genre": "Action",
"year": 1954
},
"vectors": {
"genre_embedding": {
"dense": {
"values": [
0.3863801,
0.73934346,
0.16189057,
0.5271367
]
}
},
"sparse_embedding": {
"sparse": {
"values": [
1,
6,
3,
2,
8,
5,
2
],
"indices": [
4065,
13326,
17377,
25918,
28105,
32683,
42998
]
}
},
"plot_embedding": {
"dense": {
"values": [
1,
1,
1
]
}
},
"soundtrack_embedding": {
"dense": {
"values": [
0.5920452,
0.08301644,
0.12647335,
0.619643,
0.49258286
]
}
}
}
},
{
"name": "projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/2",
"createTime": "2026-02-04T15:35:29Z",
"updateTime": "2026-02-04T15:37:29Z",
"data": {
"title": "The Social Network",
"director": "David Fincher",
"genre": "Drama",
"year": 2010
},
"vectors": {
"genre_embedding": {
"dense": {
"values": [
0.1,
0.2,
0.3,
0.4
]
}
},
"sparse_embedding": {
"sparse": {
"values": [
1
],
"indices": [
1000
]
}
},
"plot_embedding": {
"dense": {
"values": [
0.1,
0.1,
0.1
]
}
},
"soundtrack_embedding": {
"dense": {
"values": [
0.1,
0.2,
0.3,
0.4,
0.5
]
}
}
}
}
]
}
gcloud
在使用下面的命令数据之前, 请先进行以下替换:
- COLLECTION_ID:集合的 ID。
- LOCATION:您使用 Agent Platform 的区域。
- PROJECT_ID:您的 Google Cloud 项目 ID。
执行以下命令:
Linux、macOS 或 Cloud Shell
gcloud beta vector-search collections data-objects query \ --json-filter='{"$or": [{"director": {"$eq": "Akira Kurosawa"}},{"$and": [{"director": {"$eq": "David Fincher"}},{"genre": {"$ne": "Thriller"}}]}]}' \ --output-data-fields='*' \ --output-vector-fields='*' \ --output-metadata-fields='*' \ --collection=COLLECTION_ID \ --location=LOCATION \ --project=PROJECT_ID
Windows (PowerShell)
gcloud beta vector-search collections data-objects query ` --json-filter='{"$or": [{"director": {"$eq": "Akira Kurosawa"}},{"$and": [{"director": {"$eq": "David Fincher"}},{"genre": {"$ne": "Thriller"}}]}]}' ` --output-data-fields='*' ` --output-vector-fields='*' ` --output-metadata-fields='*' ` --collection=COLLECTION_ID ` --location=LOCATION ` --project=PROJECT_ID
Windows (cmd.exe)
gcloud beta vector-search collections data-objects query ^ --json-filter='{"$or": [{"director": {"$eq": "Akira Kurosawa"}},{"$and": [{"director": {"$eq": "David Fincher"}},{"genre": {"$ne": "Thriller"}}]}]}' ^ --output-data-fields='*' ^ --output-vector-fields='*' ^ --output-metadata-fields='*' ^ --collection=COLLECTION_ID ^ --location=LOCATION ^ --project=PROJECT_ID
您应该会收到类似如下所示的响应:
---
createTime: '2026-02-04T14:35:29Z'
data:
director: Akira Kurosawa
genre: Action
title: Seven Samurai
year: 1954
name: projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/1
updateTime: '2026-02-04T14:37:29Z'
vectors:
genre_embedding:
dense:
values:
- 0.38638
- 0.739343
- 0.161891
- 0.527137
plot_embedding:
dense:
values:
- 1.0
- 1.0
- 1.0
soundtrack_embedding:
dense:
values:
- 0.592045
- 0.0830164
- 0.126473
- 0.619643
- 0.492583
sparse_embedding:
sparse:
indices:
- 4065
- 13326
- 17377
- 25918
- 28105
- 32683
- 42998
values:
- 1.0
- 6.0
- 3.0
- 2.0
- 8.0
- 5.0
- 2.0
---
createTime: '2026-02-04T15:35:29Z'
data:
director: David Fincher
genre: Drama
title: The Social Network
year: 2010
name: projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/2
updateTime: '2026-02-04T15:37:29Z'
vectors:
genre_embedding:
dense:
values:
- 0.1
- 0.2
- 0.3
- 0.4
plot_embedding:
dense:
values:
- 0.1
- 0.1
- 0.1
soundtrack_embedding:
dense:
values:
- 0.1
- 0.2
- 0.3
- 0.4
- 0.5
sparse_embedding:
sparse:
indices:
- 1000
values:
- 1.0
Python
from google.cloud import vectorsearch_v1beta
# Create the client
data_object_search_service_client = vectorsearch_v1beta.DataObjectSearchServiceClient()
# Initialize request
request = vectorsearch_v1beta.QueryDataObjectsRequest(
parent="projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID",
filter={
"$or": [
{"director": {"$eq": "Akira Kurosawa"}},
{
"$and": [
{"director": {"$eq": "David Fincher"}},
{"genre": {"$ne": "Thriller"}},
]
},
]
},
)
# Make the request
page_result = data_object_search_service_client.query_data_objects(request=request)
# Handle the response
for response in page_result:
print(response)
如需执行聚合,您可以使用 aggregate 端点,并在请求正文中指定聚合类型。
以下示例演示了如何统计 ID 为 COLLECTION_ID 的集合中的所有数据对象。
REST
在使用任何请求数据之前, 请先进行以下替换:
- COLLECTION_ID:集合的 ID。
- LOCATION:您使用 Agent Platform 的区域。
- PROJECT_ID:您的 Google Cloud 项目 ID。
HTTP 方法和网址:
POST https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects:aggregate
请求 JSON 正文:
{
"aggregate": "count"
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"aggregateResults": [
{
"count": 1000
}
]
}
gcloud
在使用下面的命令数据之前, 请先进行以下替换:
- COLLECTION_ID:集合的 ID。
- LOCATION:您使用 Agent Platform 的区域。
- PROJECT_ID:您的 Google Cloud 项目 ID。
执行以下命令:
Linux、macOS 或 Cloud Shell
gcloud beta vector-search collections data-objects aggregate \ --aggregation-method=count \ --collection=COLLECTION_ID \ --location=LOCATION \ --project=PROJECT_ID
Windows (PowerShell)
gcloud beta vector-search collections data-objects aggregate ` --aggregation-method=count ` --collection=COLLECTION_ID ` --location=LOCATION ` --project=PROJECT_ID
Windows (cmd.exe)
gcloud beta vector-search collections data-objects aggregate ^ --aggregation-method=count ^ --collection=COLLECTION_ID ^ --location=LOCATION ^ --project=PROJECT_ID
您应该会收到类似如下所示的响应:
aggregateResults: - count: 1000
Python
from google.cloud import vectorsearch_v1beta
# Create the client
data_object_search_service_client = vectorsearch_v1beta.DataObjectSearchServiceClient()
# Initialize request
request = vectorsearch_v1beta.AggregateDataObjectsRequest(
parent="projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID",
aggregate="COUNT",
)
# Make the request
response = data_object_search_service_client.aggregate_data_objects(request=request)
# Handle the response
print(response)
接下来怎么做?
- 了解如何搜索数据对象。