如何通过 Python SDK 在 Collection 中分组检索 Doc
作者:DashVector
- 2025-10-27 陕西
本文字数:1895 字
阅读完需:约 6 分钟

本文介绍如何通过 Python SDK 在 Collection 中按分组进行相似性检索。
前提条件
已创建 Cluster
已获得 API-KEY
已安装最新版 SDK
接口定义
Python 示例:
Collection.query_group_by( self, vector: Optional[Union[List[Union[int, float]], np.ndarray]] = None, *, group_by_field: str, group_count: int = 10, group_topk: int = 10, id: Optional[str] = None, filter: Optional[str] = None, include_vector: bool = False, partition: Optional[str] = None, output_fields: Optional[List[str]] = None, sparse_vector: Optional[Dict[int, float]] = None, async_req: bool = False, ) -> DashVectorResponse:
复制代码
使用示例
说明
需要使用您的 api-key 替换示例中的 YOUR_API_KEY、您的 Cluster Endpoint 替换示例中的 YOUR_CLUSTER_ENDPOINT,代码才能正常运行。Python 示例:
import dashvectorimport numpy as np
client = dashvector.Client( api_key='YOUR_API_KEY', endpoint='YOUR_CLUSTER_ENDPOINT')ret = client.create( name='group_by_demo', dimension=4, fields_schema={'document_id': str, 'chunk_id': int})assert ret
collection = client.get(name='group_by_demo')
ret = collection.insert([ ('1', np.random.rand(4), {'document_id': 'paper-01', 'chunk_id': 1, 'content': 'xxxA'}), ('2', np.random.rand(4), {'document_id': 'paper-01', 'chunk_id': 2, 'content': 'xxxB'}), ('3', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 1, 'content': 'xxxC'}), ('4', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 2, 'content': 'xxxD'}), ('5', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 3, 'content': 'xxxE'}), ('6', np.random.rand(4), {'document_id': 'paper-03', 'chunk_id': 1, 'content': 'xxxF'}),])assert ret
复制代码
根据向量进行分组相似性检索
Python 示例:
ret = collection.query_group_by( vector=[0.1, 0.2, 0.3, 0.4], group_by_field='document_id', # 按document_id字段的值分组 group_count=2, # 返回2个分组 group_topk=2, # 每个分组最多返回2个doc)# 判断是否成功if ret: print('query_group_by success') print(len(ret)) print('------------------------') for group in ret: print('group key:', group.group_id) for doc in group.docs: prefix = ' -' print(prefix, doc)
复制代码
参考输出如下
query_group_by success4------------------------group key: paper-01 - {"id": "2", "fields": {"document_id": "paper-01", "chunk_id": 2, "content": "xxxB"}, "score": 0.6807} - {"id": "1", "fields": {"document_id": "paper-01", "chunk_id": 1, "content": "xxxA"}, "score": 0.4289}group key: paper-02 - {"id": "3", "fields": {"document_id": "paper-02", "chunk_id": 1, "content": "xxxC"}, "score": 0.6553} - {"id": "5", "fields": {"document_id": "paper-02", "chunk_id": 3, "content": "xxxE"}, "score": 0.4401}
复制代码
根据主键(对应的向量)进行分组相似性检索
Python 示例:
ret = collection.query_group_by( id='1', group_by_field='name',)# 判断query接口是否成功if ret: print('query_group_by success') print(len(ret)) for group in ret: print('group:', group.group_id) for doc in group.docs: print(doc) print(doc.id) print(doc.vector) print(doc.fields)
复制代码
带过滤条件的分组相似性检索
Python 示例:
# 根据向量或者主键进行分组相似性检索 + 条件过滤ret = collection.query_group_by( vector=[0.1, 0.2, 0.3, 0.4], # 向量检索,也可设置主键检索 group_by_field='name', filter='age > 18', # 条件过滤,仅对age > 18的Doc进行相似性检索 output_fields=['name', 'age'], # 仅返回name、age这2个Field include_vector=True)
复制代码
带有 Sparse Vector 的分组向量检索
Python 示例:
# 根据向量进行分组相似性检索 + 稀疏向量ret = collection.query_group_by( vector=[0.1, 0.2, 0.3, 0.4], # 向量检索 sparse_vector={1: 0.3, 20: 0.7}, group_by_field='name',)
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 2
DashVector
关注
还未添加个人签名 2024-05-14 加入
向量检索服务DashVector基于通义实验室自研的高效向量引擎Proxima内核,提供具备水平拓展能力的云原生、全托管的向量检索服务。







评论