如何通過Python SDK 向 Collection 中插入 Doc
本文介紹如何通過Python SDK向Collection中插入Doc。
說明
-
插入Doc時若指定id已存在,已存在的Doc不會被覆蓋,本次插入Doc操作無效。
-
插入Doc時若不指定id,則在插入過程中會自動生成id,并在返回結果中攜帶id信息。
前提條件
- 已創建Cluster
- 已獲得API-KEY
- 已安裝最新版SDK
接口定義
Python
Collection.insert(
docs: Union[Doc, List[Doc], Tuple, List[Tuple]],
partition: Optional[str] = None,
async_req: False
) -> DashVectorResponse
使用示例
說明
-
需要使用您的api-key替換示例中的YOUR_API_KEY、您的Cluster Endpoint替換示例中的YOUR_CLUSTER_ENDPOINT,代碼才能正常運行。
-
本示例需要參考新建Collection-使用示例的Collection。
Python
import dashvector
from dashvector import Doc
import numpy as np
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')
插入Doc
Python
# 通過Doc對象insert
ret = collection.insert(
Doc(
id='1',
vector=[0.1, 0.2, 0.3, 0.4]
)
)
# 判斷insert是否成功
assert ret
# 簡化形式:通過Tuple insert
ret = collection.insert(
('2', [0.1, 0.1, 0.1, 0.1]) # (id, vector)
)
插入不帶有Id的Doc
Python
# 插入Doc不攜帶id
ret = collection.insert(
Doc(vector=[0.1, 0.2, 0.3, 0.4])
)
# 簡化形式:通過Tuple insert
ret = collection.insert(
([0.1, 0.1, 0.1, 0.1],)
)
插入帶有Fields的Doc
Python
# insert單條數據,并設置Fields Value
ret = collection.insert(
Doc(
id='3',
vector=np.random.rand(4),
fields={
# 設置創建Collection時預定義的Fields Value
# name:str, weight:float, age:int, id:dashvector.long
'name': 'zhangsan', 'weight':70.0, 'age':30, 'id':1234567890,
# 設置Schema-Free的Field & Value
'anykey1': 'str-value', 'anykey2': 1,
'anykey3': True, 'anykey4': 3.1415926
}
)
)
# insert單條數據,并設置Fields Value
ret = collection.insert(
('4', np.random.rand(4), {'foo': 'bar'}) # (id, vector, fields)
)
批量插入Doc
Python
# 通過Doc對象,批量insert 10條數據
ret = collection.insert(
[
Doc(id=str(i+5), vector=np.random.rand(4)) for i in range(10)
]
)
# 簡化形式:通過Tuple,批量insert 3條數據
ret = collection.insert(
[
('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)]
)
# 判斷批量insert是否成功
assert ret
異步插入Doc
Python
# 異步批量insert 10條數據
ret_funture = collection.insert(
[
Doc(id=str(i+18), vector=np.random.rand(4), fields={'name': 'foo' + str(i)}) for i in range(10)
],
async_req=True
)
# 等待并獲取異步insert結果
ret = ret_funture.get()
插入帶有Sparse Vector的Doc
Python
ret = collection.insert(
Doc(
id='28',
vector=[0.1, 0.2, 0.3, 0.4],
sparse_vector={1:0.4, 10000:0.6, 222222:0.8}
)
)
插入多向量的Doc
Python
collection = client.get(name='multi_vector_demo')
docs = []
docs.append(Doc(id='1', vectors={"title": [0.3, 0.4, 0.5, 0.6], "content": [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]}))
docs.append(Doc(id='2', vectors={"title": [0.3, 0.4, 0.5, 0.6]}, fields={'author':'zhangsan'}))
docs.append(Doc(id='3', vectors={"content": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]}, fields={'anykey':'anyvalue'}))
docs.append(Doc(id='4', vectors={"title": [0.3, 0.4, 0.5, 0.6], "content": [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]}, sparse_vectors={"abstruct": {0: 0.1, 1: 0.32, 2: 0.482}}))
docs.append(Doc(id='5', sparse_vectors={"abstruct": {0: 0.3, 1: 0.232, 2: 0.4482, 3: 0.6672}}, fields={'author':'lisi'}))
ret = collection.insert(docs)
print(ret)
說明
多向量Collection中,稠密向量+稀疏向量字段總共不能超過4條
浙公網安備 33010602011771號