本文介紹如何通過HTTP API向Collection中插入或更新Doc。
説明
- 若調用本接口時Doc Id已存在,則等同於更新Doc;Doc Id不存在,則等同於插入Doc。
- 若調用本接口時不指定Doc Id,則等同於插入Doc,DashVector會自動生成Doc Id,並在返回結果中攜帶id信息。
前提條件
- 已創建Cluster:創建Cluster。
- 已獲得API-KEY:API-KEY管理。
Method與URL
HTTP
POST https://{Endpoint}/v1/collections/{CollectionName}/docs/upsert
使用示例
説明
- 需要使用您的api-key替換示例中的YOUR_API_KEY、您的Cluster Endpoint替換示例中的YOUR_CLUSTER_ENDPOINT,代碼才能正常運行。
- 本示例需要參考新建Collection-使用示例提前創建好名稱為
quickstart的Collection
插入或更新Doc
Shell
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"docs": [{"id": "1", "vector": [0.1, 0.2, 0.3, 0.4]}]
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs/upsert
# example output:
# {"request_id":"1b8b4ada-1148-49ce-82e3-f7f521bb6f3b","code":0,"message":"Success","output":[{"doc_op":"insert","id":"1","code":0,"message":""}]}
插入或更新不帶有Id的Doc
Shell
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"docs": [{"vector": [0.1, 0.2, 0.3, 0.4]}]
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs/upsert
# example output:
# {"request_id":"9c3f2441-c47b-43a0-bb84-127ff6a88711","code":0,"message":"Success","output":[{"doc_op":"insert","id":"6176745980928","code":0,"message":""}]}
插入或更新帶有Fields的Doc
Shell
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"docs": [
{
"id": "2",
"vector": [0.2, 0.3, 0.4, 0.5],
"fields":
{
"age": 70,
"name": "zhangshan",
"anykey1": "str-value",
"anykey2": 1,
"anykey3": true,
"anykey4": 3.1415926
}
}
]
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs/upsert
# example output:
# {"request_id":"e1467d91-f4fd-4944-9ec0-1634de426db1","code":0,"message":"Success","output":[{"doc_op":"insert","id":"2","code":0,"message":""}]}
批量插入或更新Doc
Shell
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"docs": [
{"id": "3", "vector": [0.3, 0.4, 0.5, 0.6]},
{"id": "4", "vector": [0.4, 0.5, 0.6, 0.7], "fields": {"age": 20, "name": "zhangsan"}},
{"id": "5", "vector": [0.5, 0.6, 0.7, 0.8], "fields": {"anykey": "anyvalue"}}
]
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs/upsert
# example output:
# {"request_id":"113d221b-ec79-4fde-b546-58db5154e49d","code":0,"message":"Success","output":[{"doc_op":"insert","id":"3","code":0,"message":""},{"doc_op":"insert","id":"4","code":0,"message":""},{"doc_op":"insert","id":"5","code":0,"message":""}]}
插入或更新帶有Sparse Vector的Doc
Shell
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"docs": [
{"id": "6", "vector": [0.1, 0.2, 0.3, 0.4], "sparse_vector":{"1":0.4, "10000":0.6, "222222":0.8}}
]
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs/upsert
# example output:
# {"request_id":"c401e51d-430e-4c49-9d07-c944ec94de7b","code":0,"message":"Success","output":[{"doc_op":"insert","id":"6","code":0,"message":""}]}
入參描述
|
參數 |
Location |
類型 |
必填 |
説明 |
|
{Endpoint} |
path |
str |
是 |
Cluster的Endpoint,可在控制枱Cluster詳情中查看 |
|
{CollectionName} |
path |
str |
是 |
Collection名稱 |
|
dashvector-auth-token |
header |
str |
是 |
api-key |
|
docs |
body |
array |
是 |
待更新或插入的Doc列表 |
|
partition |
body |
str |
否 |
Partition名稱 |
出參描述
|
字段 |
類型 |
描述 |
示例 |
|
code |
int |
返回值,參考返回狀態碼説明 |
0 |
|
message |
str |
返回消息 |
success |
|
request_id |
str |
請求唯一id |
19215409-ea66-4db9-8764-26ce2eb5bb99 |
|
output |
array |
返回插入或更新Doc的結果,DocOpResult列表 |
|
|
usage |
map |
對Serverless實例(按量付費)集合的Doc插入或更新請求,成功後返回實際消耗的寫請求單元數 |
|