删除 Entity
Zilliz Cloud 支持通过过滤表达式批量删除 Entity,也支持基于指定的主键值删除 Entity。对于不再需要的 Entity,可以执行删除操作。
基于过滤表达式批量删除 Entity
在批量删除 Entity 时,可以使用过滤表达式。下方的示例代码中使用了 in 操作符,批量删除了所有 color 值为 red 和 green 的 Entity。你也可以使用其它操作符构建符合要求的过滤表达式。关于过滤表达式的更多介绍,可以参考本手册中过滤表达式一节的内容。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
res = client.delete(
collection_name="quick_setup",
# highlight-next-line
filter="color in ['red_7025', 'purple_4976]"
)
print(res)
# Output
# {'delete_count': 2}
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.DeleteReq;
import io.milvus.v2.service.vector.response.DeleteResp;
ilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("YOUR_CLUSTER_ENDPOINT")
.token("YOUR_CLUSTER_TOKEN")
.build());
DeleteResp deleteResp = client.delete(DeleteReq.builder()
.collectionName("quick_setup")
.filter("color in ['red_7025', 'purple_4976']")
.build());
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
// 7. Delete entities
res = await client.delete({
collection_name: "quick_setup",
// highlight-next-line
filter: "color in ['red_7025', 'purple_4976]"
})
console.log(res.delete_cnt)
// Output
//
// 3
//
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/column"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "localhost:19530"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
_, err = client.Delete(ctx, milvusclient.NewDeleteOption("quick_setup").WithExpr("color in ['red_7025', 'purple_4976']"))
if err != nil {
fmt.Println(err.Error())
// handle err
}
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/delete" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"filter": "color in [\"red_7025\", \"purple_4976\"]"
}'
基于主键值删除指定 Entity
在大多数情况下,主键值能够确定唯一 Entity。可以在删除请求中指定需要删除的 Entity 的主键值来删除这些 Entity。下方的示例代码中,指定了需要删除的 Entity 主键值为 18 和 19。
- Python
- Java
- NodeJS
- Go
- cURL
res = client.delete(
collection_name="quick_setup",
# highlight-next-line
ids=[18, 19]
)
print(res)
# Output
# {'delete_count': 2}
import io.milvus.v2.service.vector.request.DeleteReq;
import io.milvus.v2.service.vector.response.DeleteResp;
import java.util.Arrays;
DeleteResp deleteResp = client.delete(DeleteReq.builder()
.collectionName("quick_setup")
.ids(Arrays.asList(18, 19))
.build());
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
res = await client.delete({
collection_name: "quick_setup",
ids: [18, 19]
})
console.log(res.delete_cnt)
// Output
//
// 2
//
_, err = client.Delete(ctx, milvusclient.NewDeleteOption("quick_setup").
WithInt64IDs("id", []int64{18, 19}))
if err != nil {
fmt.Println(err.Error())
// handle err
}
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/delete" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"filter": "id in [18, 19]"
}'
## {"code":0,"cost":0,"data":{}}
从 Partition 中删除 Entity
您还可以从指定的 Partition 中删除 Entity。示例代码中假设 Collection 中存在一个名为 partitionA 的 Partition。
- Python
- Java
- NodeJS
- Go
- cURL
res = client.delete(
collection_name="quick_setup",
ids=[18, 19],
partition_name="partitionA"
)
print(res)
# Output
# {'delete_count': 2}
import io.milvus.v2.service.vector.request.DeleteReq;
import io.milvus.v2.service.vector.response.DeleteResp;
import java.util.Arrays;
DeleteResp deleteResp = client.delete(DeleteReq.builder()
.collectionName("quick_setup")
.ids(Arrays.asList(18, 19))
.partitionName("partitionA")
.build());
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
res = await client.delete({
collection_name: "quick_setup",
ids: [18, 19],
partition_name: "partitionA"
})
console.log(res.delete_cnt)
// Output
//
// 2
//
_, err = client.Delete(ctx, milvusclient.NewDeleteOption("quick_setup").
WithInt64IDs("id", []int64{18, 19}).
WithPartition("partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle err
}
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/delete" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionName": "partitionA",
"filter": "id in [18, 19]"
}'
# {
# "code": 0,
# "cost": 0,
# "data": {}
# }