删除数据(delete_data)

Milvus中如何删除实体?

本文介绍在Milvus中如何删除实体。

Milvus支持通过布尔表达式筛选主键删除实体。

  • 如果一致性级别低于Strong,则删除的实体仍然可以在删除后立即检索。

  • 超出Time Travel预设时间范围的删除的实体将无法再次检索。

  • 频繁的删除操作将影响系统性能。

准备布尔表达式

准备筛选要删除的实体的布尔表达式。

Milvus仅支持删除具有明确定义主键的实体,这只需要在术语表达式中使用in就能实现。其他操作符只能用于向量搜索中的查询或标量过滤。有关更多信息,请参见布尔表达式规则

以下示例筛选主键值为01的数据。

Python Java GO Node.js CLI Curl

expr = "book_id in [0,1]"
 
const expr = "book_id in [0,1]";
 
private static final String DELETE_EXPR = "book_id in [0,1]";
 
delete entities -c book
The expression to specify entities to be deleted: book_id in [0,1]
 
"expr" = "book_id in [0,1]"
 
OptionDescription
-cThe name of the collection.
-p (Optional)The name of the partition that the entities belong to.

删除实体 Delete entities

删除使用您创建的布尔表达式的实体。 Milvus返回已删除实体的ID列表。

Python Java GO Node.js CLI Curl

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.delete(expr)
 
await milvusClient.deleteEntities({
  collection_name: "book",
  expr: expr,
});
 
// This function is under active development on the GO client.
 
milvusClient.delete(
  DeleteParam.newBuilder()
    .withCollectionName("book")
    .withExpr(DELETE_EXPR)
    .build()
);
 
You are trying to delete the entities of collection. This action cannot be undone!
Do you want to continue? [y/N]: y
 
curl -X 'DELETE' 
  'http://localhost:9091/api/v1/entities' 
  -H 'accept: application/json' 
  -H 'Content-Type: application/json' 
  -d '{
    "collection_name": "book",
    "expr": "book_id in [0,1]"
  }'
 

Output:

{
  "status":{},
  "IDs":{"IdField":{"IntId":{"data":[0,1]}}},
  "delete_cnt":2,
  "timestamp":434262178115092482
}
 
参数描述
expr用于指定要删除的实体的布尔表达式。
partition_name(可选)要从中删除实体的分区的名称。
参数描述
collection_name要从其中删除实体的集合名称。
expr用于指定要删除的实体的布尔表达式。
partition_name(可选)要从中删除实体的分区的名称。
参数描述
CollectionName要从其中删除实体的集合名称。
expr用于指定要删除的实体的布尔表达式。
PartitionName(可选)要从中删除实体的分区的名称。
参数描述
collection_name要从其中删除实体的集合名称。
expr用于指定要删除的实体的布尔表达式。

下一步是什么