Query API 的目的是使用过滤器从集合中检索数据对象。这类似于查询数据库表并使用 SQL WHERE 子句。您还可以使用聚合来获取与过滤条件匹配的数据对象的数量。
过滤条件表达式语言
除了 KNN/ANN 搜索功能之外,Vector Search 2.0 还使用自定义查询语言提供多用途的查询功能。下表介绍了查询语言。
| 过滤 | 说明 | 受支持的类型 | 示例 |
|---|---|---|---|
| $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"]}} |
查询集合
以下示例演示了如何使用过滤条件查询集合 movies 中的数据对象。
# Query Data Objects
curl -X POST \
'https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/movies/dataObjects:query' \
-H 'Bearer $(gcloud auth print-access-token)' \
-H 'Content-Type: application/json' \
-d '{ \
"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": "*" \
} \
}'
以下示例演示了如何统计集合 movies 中的所有数据对象。
curl -X POST \ 'https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/movies/dataObjects:query' \
-H 'Bearer $(gcloud auth print-access-token)' \
-H 'Content-Type: application/json' \
-d '{ \
"aggregate": "count" \
}'
后续步骤
- 了解如何搜索数据对象。