Zvec Logo

更新 Document

使用 update() 修改已有的 DocumentDoc)。

只有你包含的标量字段和向量 Embedding 会被更新;其他内容保持不变。

该方法接受单个 Doc 对象或 Doc 对象列表。


Document Doc

传入 update() 的每个 Doc 必须:

  • 指定一个 Collection 中已存在id(如果 Document 不存在,操作将失败)
  • 仅包含你打算更新的字段和向量,格式须符合 Collection 的 Schema
    1. 标量字段:以键值对形式在 fields 中提供(标量字段名作为键)
    2. 向量 Embedding:以键值对形式在 vectors 中提供(向量名作为键)
  • 省略不需要更改的标量字段或向量——它们将保持不变。

更新单个 Document

假设你已有一个 Collection,Schema 如下:

  • 标量字段
    1. book_title(字符串)
    2. category(字符串数组)
    3. publish_year(32 位整数)
  • 向量 Embedding
    1. dense_embedding:768 维稠密向量
    2. sparse_embedding:稀疏向量

要更新已有 Document,提供其 id 以及要更改的字段或向量:

更新 Document
import zvec

doc = zvec.Doc(  
    id="book_1",  # ← 必须已存在于 Collection 中
    vectors={
        "sparse_embedding": {  # ← 替换整个稀疏向量
            35: 0.25,
            237: 0.1,
            369: 0.44,
        },
    },
    fields={
        "category": [  # ← 替换当前分类列表
            "Romance",
            "Classic Literature",
            "American Civil War",
        ],
    },
    # 注意:`book_title`、`publish_year` 和 `dense_embedding` 被省略 → 保持不变
)

# 更新 Document
result = collection.update(doc)   
print(result)  # {"code": 0} 表示成功

update() 方法会先验证 Document:

  • 错误用法——如未知字段或向量维度不匹配——会抛出异常
  • 如果验证通过,方法执行更新并返回 Status 对象:
    • {"code": 0} 表示成功。
    • 非零代码表示失败(如 ID 不存在)。

成功更新的 Document 立即可查询 🚀。

更新 Document
let doc: ZVecDocInput = {   
    id: "book_1", // ← 必须已存在于 Collection 中
    vectors: {
        "sparse_embedding": { // ← 替换整个稀疏向量
            35: 0.25,
            237: 0.1,
            369: 0.44
        }
    },
    fields: {
        "category": [ // ← 替换当前分类列表
            "Romance",
            "Classic Literature",
            "American Civil War"
        ]
    }
    // 注意:`book_title`、`publish_year` 和 `dense_embedding` 被省略 → 保持不变
};

// 更新 Document
let result = collection.updateSync(doc);  
console.log(result);  // { ok: true } 表示成功

update() 方法会先验证 Document:

  • 错误用法——如未知字段或向量维度不匹配——会抛出异常
  • 如果验证通过,方法执行更新并返回 Status 对象:
    • { ok: true } 表示成功。
    • { ok: false } 表示失败(如 ID 不存在)。

成功更新的 Document 立即可查询 🚀。


批量更新 Document

传入 Doc 对象列表即可一次更新多个 Document。 每个 Doc 独立处理,方法返回 Status 对象列表——每个 Document 对应一个。

批量更新 Document
import zvec

results = collection.update(  
    [
        zvec.Doc(
            id="book_1",
            vectors={
                "sparse_embedding": {35: 0.25, 237: 0.1, 369: 0.44},
            },
            fields={
                "category": ["Romance", "Classic Literature", "American Civil War"],
            },
        ),
        zvec.Doc(
            id="book_2",
            fields={
                "book_title": "The Great Gatsby",
            },
        ),
        zvec.Doc(
            id="book_3",
            fields={
                "book_title": "A Tale of Two Cities",
                "publish_year": 1859,
            },
        ),
    ]
)

print(results)  # [{"code":0}, {"code":0}, {"code":0}]

如果批量中任何 Document 存在错误用法(如未知字段或向量维度不匹配),方法将抛出异常且不会更新任何 Document

如果所有 Document 验证通过,方法会尝试逐一更新。某个 Document 失败(如 id 不存在)不会阻止其他 Document 的更新。

🔍 请始终检查结果列表中每个 Status

目录