如何通过HTTP API插入或更新Doc

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


说明

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

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

前提条件

Method与URL

HTTP

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

使用示例

说明

  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/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插入或更新请求,成功后返回实际消耗的写请求单元数 | { Usage: { write_units: 4 } } |

相关推荐
wangxin2082 分钟前
同一只股票三个 AI 写出三种段位:CoordClaw 多智能体如何产出投研级股评
人工智能·多智能体·团队协作·管理学·组织管理·coordclaw·ai数字社会
程序员cxuan2 分钟前
Codex 接入 DeepSeek-V4-Flash,丝滑的一批
人工智能·后端·程序员
大鱼>2 分钟前
DSPy:LLM程序自动编译与提示词优化
开发语言·人工智能·python·深度学习
AI科技星3 分钟前
全域光速运动理论体系 (GAQ-UFT)——范式重构、核心方程与传统物理的本质分野
人工智能·线性代数·机器学习·重构·数据挖掘·回归·ai科技星
2401_843253707 分钟前
金融智能:AI如何重构银行业未来
人工智能·python·金融
一路向北North12 分钟前
Spring AI(9) :解决百炼平台兼容性问题
java·人工智能·spring
叫我Paul就好13 分钟前
RAG 入门到精通 - 生成的优化
人工智能·软件工程·rag
2601_9657984717 分钟前
Build a Consulting Site in 2026: Nifty Theme Practical Review
java·linux·数据库
星核0penstarry18 分钟前
Solon AI v4.0.4 技术分析:Java Agent 框架的兼容性突破与选型考量 基于 OSCHINA 2026-07-30 报道及公开技术资料整理
java·开发语言·人工智能
杨先生哦26 分钟前
【2026热端攻防系列 11/12】前端AI风控攻防实战:验证码缺陷、人机验证绕过、智能爬虫对抗与企业智能风控加固方案
前端·人工智能·笔记·爬虫·安全