Skip to content

Commit

Permalink
fix: 支持Milvus删除操作 TencentBlueKing#2573
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Sep 18, 2024
1 parent 665bf1c commit deccdcb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tencent.bkrepo.common.api.util.readJsonString
import com.tencent.bkrepo.common.api.util.toJsonString
import com.tencent.bkrepo.common.storage.innercos.http.toRequestBody
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.CreateCollectionReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.DeleteVectorReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.DropCollectionReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.HasCollectionReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.InsertVectorReq
Expand Down Expand Up @@ -89,6 +90,16 @@ class MilvusClient(
}
}

fun delete(req: DeleteVectorReq) {
val request = Request.Builder()
.url("${clientProperties.uri}/v2/vectordb/entities/delete")
.post(req.toJsonString().toRequestBody(APPLICATION_JSON))
.build()
client.newCall(request).execute().throwIfFailed<Any, Any> {
logger.info("delete vector of [${req.dbName}/${req.collectionName}] success")
}
}

fun search(req: SearchVectorReq): Map<String, Any> {
val request = Request.Builder()
.url("${clientProperties.uri}/v2/vectordb/entities/search")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.Collect
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.ConsistencyLevel
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.CreateCollectionReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.DataType
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.DeleteVectorReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.ElementTypeParams
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.FieldSchema
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.IndexParam
Expand Down Expand Up @@ -105,19 +106,13 @@ class MilvusVectorStore(

override fun delete(ids: Set<String>): Boolean {
val deleteExpression = "$DOC_ID_FIELD_NAME in [${ids.joinToString(",") { "'$it'" }}]"
val status = milvusClient.delete(
DeleteParam.newBuilder()
.withCollectionName(config.collectionName)
.withExpr(deleteExpression)
.build()
val req = DeleteVectorReq(
dbName = config.databaseName,
collectionName = config.collectionName,
filter = deleteExpression
)

val deleteCount = status.data.deleteCnt
if (deleteCount != ids.size.toLong()) {
logger.warn(String.format("Deleted only %s entries from requested %s ", deleteCount, ids.size))
}

return status.status == R.Status.Success.code
milvusClient.delete(req)
return true
}

override fun similaritySearch(request: SearchRequest): List<Document> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request

data class DeleteVectorReq(
val dbName: String,
val collectionName: String,
val filter: String,
val partitionName: String? = null,
)

0 comments on commit deccdcb

Please sign in to comment.