写点什么

如何通过 Python SDK 向 Collection 中插入或更新 Doc

作者:DashVector
  • 2025-11-04
    陕西
  • 本文字数:1503 字

    阅读完需:约 5 分钟

如何通过Python SDK向Collection中插入或更新Doc

本文介绍如何通过 Python SDK 向 Collection 中插入或更新 Doc。


说明


  1. 若调用本接口时 Doc Id 已存在,则等同于更新 Doc;Doc Id 不存在,则等同于插入 Doc。

  2. 若调用本接口时不指定 Doc Id,则等同于插入 Doc,DashVector 会自动生成 Doc Id,并在返回结果中携带 id 信息。

前提条件

  • 已创建 Cluster

  • 已获得 API-KEY

  • 已安装最新版 SDK

接口定义

Python 示例:


Collection.upsert(    docs: Union[Doc, List[Doc], Tuple, List[Tuple]],    partition: Optional[str] = None,    async_req: False) -> DashVectorResponse
复制代码

使用示例

说明


  1. 需要使用您的 api-key 替换示例中的 YOUR_API_KEY、您的 Cluster Endpoint 替换示例中的 YOUR_CLUSTER_ENDPOINT,代码才能正常运行。

  2. 本示例需要参考新建Collection-使用示例提前创建好名称为quickstart的 Collection。


Python 示例:


import dashvectorfrom dashvector import Docimport numpy as np
client = dashvector.Client( api_key='YOUR_API_KEY', endpoint='YOUR_CLUSTER_ENDPOINT')collection = client.get(name='quickstart')
复制代码

插入或更新 Doc

Python 示例:


# 通过Doc对象upsertret = collection.upsert(    Doc(        id='1',        vector=[0.1, 0.2, 0.3, 0.4]    ))# 判断upsert是否成功assert ret
# 简化形式:通过Tuple upsertret = collection.upsert( ('2', [0.1, 0.1, 0.1, 0.1]) # (id, vector))
复制代码

插入或更新不带有 Id 的 Doc

Python


# 通过Doc对象upsertret = collection.upsert(    Doc(vector=[0.1, 0.2, 0.3, 0.4]))# 简化形式:通过Tuple upsertret = collection.upsert(    ([0.1, 0.1, 0.1, 0.1],)          )
复制代码

插入或更新带有 Fields 的 Doc

Python 示例:


# upsert单条数据,并设置Fields Valueret = collection.upsert(    Doc(        id='3',        vector=np.random.rand(4),        fields={            # 设置创建Collection时预定义的Fileds Value            # name:str, weight:float, age:int, id:long            'name': 'zhangsan', 'weight':70.0, 'age':30, 'id':1234567890,            # 设置Schema-Free的Field & Value            'anykey1': 'str-value', 'anykey2': 1,            'anykey3': True, 'anykey4': 3.1415926        }    ))
# upsert单条数据,并设置Fields Valueret = collection.upsert( ('4', np.random.rand(4), {'foo': 'bar'}) # (id, vector, fields))
复制代码

批量插入或更新 Doc

Python 示例:


# 通过Doc对象,批量upsert 10条数据ret = collection.upsert(    [        Doc(id=str(i+5), vector=np.random.rand(4)) for i in range(10)    ])
# 简化形式:通过Tuple,批量upsert 3条数据ret = collection.upsert( [ ('15', [0.2,0.7,0.8,1.3], {'age': 20}), ('16', [0.3,0.6,0.9,1.2], {'age': 30}), ('17', [0.4,0.5,1.0,1.1], {'age': 40}) ] # List[(id, vector, fields)])
# 判断批量upsert是否成功assert ret
复制代码

异步插入或更新 Doc

Python 示例:


# 异步批量upsert 10条数据ret_funture = collection.upsert(    [        Doc(id=str(i+18), vector=np.random.rand(4), fields={'name': 'foo' + str(i)}) for i in range(10)    ],    async_req=True)# 等待并获取异步upsert结果ret = ret_funture.get()
复制代码

插入或更新带有 Sparse Vector 的 Doc

Python 示例:


ret = collection.upsert(    Doc(        id='28',        vector=[0.1, 0.2, 0.3, 0.4],        sparse_vector={1:0.4, 10000:0.6, 222222:0.8}    ))
复制代码


用户头像

DashVector

关注

还未添加个人签名 2024-05-14 加入

向量检索服务DashVector基于通义实验室自研的高效向量引擎Proxima内核,提供具备水平拓展能力的云原生、全托管的向量检索服务。

评论

发布
暂无评论
如何通过Python SDK向Collection中插入或更新Doc_人工智能_DashVector_InfoQ写作社区