本文介绍如何通过Python SDK,根据ID或ID列表获取Collection中已存在的Doc。
说明
如果指定id不存在,则该id对应的Doc为空。
前提条件
- 已创建Cluster
- 已获得API-KEY
- 已安装最新版SDK
接口定义
python
collection.fetch(
ids: Union[str, List[str]],
partition: Optional[str] = None,
async_req: bool = False
) -> DashVectorResponse
使用示例
说明
-
需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。
-
本示例需要参考新建Collection-使用示例提前创建好名称为
quickstart的Collection,并参考插入Doc提前插入部分数据。
Python
python
import dashvector
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')
doc_id = '1'
docs = collection.fetch(doc_id)
# 判断fetch接口是否成功
if docs:
print('fetch success')
# 判断fetch的doc是否存在,如果指定的ID不存在,则返回的output为空
if doc_id in docs:
doc = docs[doc_id]
print(doc.id)
print(doc.vector)
print(doc.fields)
# 遍历返回结果
for id in docs:
print(docs[id])
# 批量fetch
docs = collection.fetch(['1','2'])