找回密码
 立即注册
首页 业界区 业界 如何通过Python SDK向Collection中插入或更新Doc ...

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

东门芳洲 昨天 16:25
本文介绍如何通过Python SDK向Collection中插入或更新Doc。
说明

  • 若调用本接口时Doc Id已存在,则等同于更新Doc;
    Doc Id不存在,则等同于插入Doc。
  • 若调用本接口时不指定Doc Id,则等同于插入Doc,DashVector会自动生成Doc Id,并在返回结果中携带id信息。
前提条件


  • 已创建Cluster
  • 已获得API-KEY
  • 已安装最新版SDK
接口定义

Python示例:
  1. Collection.upsert(
  2.     docs: Union[Doc, List[Doc], Tuple, List[Tuple]],
  3.     partition: Optional[str] = None,
  4.     async_req: False
  5. ) -> DashVectorResponse
复制代码
使用示例

说明

  • 需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。
  • 本示例需要参考新建Collection-使用示例提前创建好名称为quickstart的Collection。
Python示例:
  1. import dashvector
  2. from dashvector import Doc
  3. import numpy as np
  4. client = dashvector.Client(
  5.     api_key='YOUR_API_KEY',
  6.     endpoint='YOUR_CLUSTER_ENDPOINT'
  7. )
  8. collection = client.get(name='quickstart')
复制代码
插入或更新Doc

Python示例:
  1. # 通过Doc对象upsert
  2. ret = collection.upsert(
  3.     Doc(
  4.         id='1',
  5.         vector=[0.1, 0.2, 0.3, 0.4]
  6.     )
  7. )
  8. # 判断upsert是否成功
  9. assert ret
  10. # 简化形式:通过Tuple upsert
  11. ret = collection.upsert(
  12.     ('2', [0.1, 0.1, 0.1, 0.1])               # (id, vector)
  13. )
复制代码
插入或更新不带有Id的Doc

Python
  1. # 通过Doc对象upsert
  2. ret = collection.upsert(
  3.     Doc(vector=[0.1, 0.2, 0.3, 0.4])
  4. )
  5. # 简化形式:通过Tuple upsert
  6. ret = collection.upsert(
  7.     ([0.1, 0.1, 0.1, 0.1],)         
  8. )
复制代码
插入或更新带有Fields的Doc

Python示例:
  1. # upsert单条数据,并设置Fields Value
  2. ret = collection.upsert(
  3.     Doc(
  4.         id='3',
  5.         vector=np.random.rand(4),
  6.         fields={
  7.             # 设置创建Collection时预定义的Fileds Value
  8.             # name:str, weight:float, age:int, id:long
  9.             'name': 'zhangsan', 'weight':70.0, 'age':30, 'id':1234567890,
  10.             # 设置Schema-Free的Field & Value
  11.             'anykey1': 'str-value', 'anykey2': 1,
  12.             'anykey3': True, 'anykey4': 3.1415926
  13.         }
  14.     )
  15. )
  16. # upsert单条数据,并设置Fields Value
  17. ret = collection.upsert(
  18.     ('4', np.random.rand(4), {'foo': 'bar'})  # (id, vector, fields)
  19. )
复制代码
批量插入或更新Doc

Python示例:
  1. # 通过Doc对象,批量upsert 10条数据
  2. ret = collection.upsert(
  3.     [
  4.         Doc(id=str(i+5), vector=np.random.rand(4)) for i in range(10)
  5.     ]
  6. )
  7. # 简化形式:通过Tuple,批量upsert 3条数据
  8. ret = collection.upsert(
  9.     [
  10.         ('15', [0.2,0.7,0.8,1.3], {'age': 20}),
  11.         ('16', [0.3,0.6,0.9,1.2], {'age': 30}),
  12.         ('17', [0.4,0.5,1.0,1.1], {'age': 40})
  13.     ]                                         # List[(id, vector, fields)]
  14. )
  15. # 判断批量upsert是否成功
  16. assert ret
复制代码
异步插入或更新Doc

Python示例:
  1. # 异步批量upsert 10条数据
  2. ret_funture = collection.upsert(
  3.     [
  4.         Doc(id=str(i+18), vector=np.random.rand(4), fields={'name': 'foo' + str(i)}) for i in range(10)
  5.     ],
  6.     async_req=True
  7. )
  8. # 等待并获取异步upsert结果
  9. ret = ret_funture.get()
复制代码
插入或更新带有Sparse Vector的Doc

Python示例:
  1. ret = collection.upsert(
  2.     Doc(
  3.         id='28',
  4.         vector=[0.1, 0.2, 0.3, 0.4],
  5.         sparse_vector={1:0.4, 10000:0.6, 222222:0.8}
  6.     )
  7. )
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册