删除 Document
Zvec 提供两种删除 Document 的方式。请根据场景选择合适的方法:
| 方法 | 输入 | 使用场景 |
|---|---|---|
delete() | 一个或多个 Document id | 已知要删除的 Document 的确切 ID 时使用 |
delete_by_filter() | 过滤表达式(如 publish_year < 1900) | 基于字段值批量删除——适用于清理符合特定条件的 Document |
删除操作是即时且不可逆的。 执行删除前请务必仔细核对输入。
按 ID 删除
假设你已打开 Collection 并准备好 collection 对象。
使用 delete() 在已知确切 ID 时删除一个或多个 Document。
# 删除单个 Document
result = collection.delete(ids="doc_id_1")
print(result) # {"code":0} 表示成功
# 批量删除多个 Document
result = collection.delete(ids=["doc_id_2", "doc_id_3"])
print(result) # [{"code":0}, {"code":0}]- 传入单个
id时,delete()返回一个Status对象。 - 传入
id列表时,返回相同顺序的Status对象列表。
按过滤条件删除
使用 delete_by_filter() 删除所有匹配布尔 filter 表达式的 Document。
filter 可以引用标量字段(如 publish_year、language),使用比较和逻辑运算符。
# 删除所有 1900 年之前出版的书
collection.delete_by_filter(filter="publish_year < 1900")
# 组合过滤条件
collection.delete_by_filter(
filter='publish_year < 1900 AND (language = "English" OR language = "Chinese")'
)