如何通过HTTP API插入Doc

本文介绍如何通过HTTP API向Collection中插入Doc。


说明

  1. 插入Doc时若指定id已存在,已存在的Doc不会被覆盖,本次插入Doc操作无效。

  2. 插入Doc时若不指定id,则在插入过程中会自动生成id,并在返回结果中携带id信息。

前提条件

Method与URL

HTTP

复制代码
POST https://{Endpoint}/v1/collections/{CollectionName}/docs

使用示例

说明

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

  2. 本示例需要参考新建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

# example output:
# {"code": 0, "message": "Success", "requests_id": "6fda9f39-ee83-45cb-bfc5-ff353d650568", "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

# example output:
# {"request_id":"1602e7fb-227d-4a39-bfc5-0066763f20ab","code":0,"message":"Success","output":[{"doc_op":"insert","id":"2196112409600","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

# example output:
# {"request_id":"6de0609c-476f-4c2c-a732-4f8745f24536","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

# example output:
# {"request_id":"b0bf32b0-92dc-4fa5-bf33-e6b27f567c60","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

# example output:
# {"request_id":"9ffed1ac-bdbe-4341-a1f7-0e25afce4b47","code":0,"message":"Success","output":[{"doc_op":"insert","id":"6","code":0,"message":""}]}

插入多向量集合

复制代码
curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{ 
    "docs": [ 
      {"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]}},
      {"id": "2", "vectors": {"title": [0.1, 0.2, 0.3, 0.4]},"fields": {"author": "zhangsan"}},
      {"id": "3", "vectors": {"content": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]}, "fields": {"anykey": "anyvalue"}}
    ]
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/multi_vector_demo/docs

# example output:
# {"request_id":"f98999e1-ab30-4bea-940c-a25b4b5bbb84","code":0,"message":"Success","output":[{"doc_op":"insert","id":"1","code":0,"message":""},{"doc_op":"insert","id":"2","code":0,"message":""},{"doc_op":"insert","id":"3","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插入请求,成功后返回实际消耗的写请求单元数 | { Usage: { write_units: 3 } } |

相关推荐
skywalk8163几秒前
python run.py “请讨论一下中文编程语言的设计“ --max-rounds 4
开发语言·人工智能
读创商闻1 分钟前
解锁强劲算力,数聚红芯 AI 智算服务器甄选指南
运维·服务器·人工智能
三掌柜6661 分钟前
自主智能体开发实战:Skills 从原理到工程化落地
人工智能
Trouvaille ~2 分钟前
【Redis篇】Hash 哈希:字段级操作与对象存储的最佳实践
数据库·redis·后端·算法·缓存·哈希算法·键值对
袁煦丞 cpolar内网穿透实验室2 分钟前
Remotion让视频创作告别剪按钮——AI生成视频+像素级精准控制!cpolar内网穿透实验室第700个成功挑战
人工智能·音视频·远程工作·内网穿透·cpolar·安全内网连接
AI品信智慧数智人5 分钟前
AI赋能智慧文旅新赛道✨数字人定制专属伴游管家,重塑出游新体验
大数据·人工智能
空圆小生10 分钟前
基于 Python+Vue3 的 AI 人脸识别门禁考勤系统
开发语言·人工智能·python
happyprince11 分钟前
10-Hugging Face Transformers 量化系统深度分析
java·前端·数据库
寺中人15 分钟前
华为韬(τ)定律:后摩尔时代,中国定义芯片新规则
人工智能·物联网·华为·韬定律
悟纤15 分钟前
AI音乐制作女团舞台MV详细教程
人工智能·seedance2.0·happyhorse·ai mv·ai音乐mv·seedance2.1