Milvus数据更新:使用Upsert API实现标量字段批量更新

  1. 最近公司项目遇到一个milvus的技术场景,需要更新Collection的标量字段值,查了一遍官网,发现milvus并不支持直接修改字段值。

  2. 所以一个替代方案是,先获取collection的所有entities,然后逐条判断当前entity是否需要修改字段值,如果需要,根据ID将这条entity删除,然后重新插入修改后的完整entity。

  3. 需要注意的是,根据标量查询的entity有size限制,因此,设置的响应返回字段中不能有向量embedding字段,如果这条entity要更新,需要根据查询到的original_text重新生成嵌入向量。

  4. 值得庆幸的是,milvus提供了这样的API,不需要我们手动删除后再插入,milvus将这两个过程合并在一个API中实现了。

  5. 这个API就是Upsert,下面这个图就是Upsert的工作过程,如果Collection中主键id是自动生成的(Auto),那么Milvus就会先自动生成一个主键id,然后整合接收的entity实体插入数据,然后,根据 Upsert 请求中包含的实体主键值执行删除旧数据操作

  6. 下面是完整java程序

java 复制代码
   @Test
    public void clean(){
    		 // 定义返回字段,这里返回A,B还有主键id字段
        List<String> outputFields = Arrays.asList("id","A","B");  
        String queryExpr = "id > 0";
			 //设置标量查询条件
        QueryReq queryReq = QueryReq.builder()
                .databaseName(CommonConstant.MATERIAL_MILVUS_DATABASE)
                .collectionName(CommonConstant.MATERIAL_NAME_MATCH_COLLECTION)
                .filter(queryExpr)
                .outputFields(outputFields)
                .build();
        QueryResp queryResp = milvusServiceClient.query(queryReq);

        List<QueryResp.QueryResult> results = queryResp.getQueryResults();
        for (QueryResp.QueryResult result : results) {
            long id = (long)result.getEntity().get("id");
            String original_text = (String) result.getEntity().get("A");
            String standard_code = (String) result.getEntity().get("B");


            String treat = treat(manufacturer);
            // 只更新需要修改的entity
            if (!treat.equals(manufacturer)){
                JsonObject jsonObject = new JsonObject();
                List<Float> embedding = embeddingService.postForEmbedding(original_text).getData().get(0).getEmbedding();
                jsonObject.addProperty("id", id);
                jsonObject.add("embedding", gson.toJsonTree(embedding));
                jsonObject.addProperty("A", original_text);
                jsonObject.addProperty("B", standard_code);
       

                List<JsonObject> dataList = Collections.singletonList(jsonObject);
                // 封装UpsertReq 跳进
                UpsertReq upsertReq = UpsertReq.builder()
                        .databaseName(CommonConstant.MATERIAL_MILVUS_DATABASE)
                        .collectionName(CommonConstant.MATERIAL_NAME_MATCH_COLLECTION)
                        .data(dataList)
                        .build();
                UpsertResp upsertResp = milvusServiceClient.upsert(upsertReq);
                log.info(manufacturer + "  处理之后: " + treat );
                System.out.println(upsertResp);

            }
        }
    }

    private String treat(String original){
        String res = original.replaceAll("\\s+", "");
        // 2. 将英文()替换为中文()
        res = res.replace("(", "(").replace(")", ")");
        return res;
    }
相关推荐
StringKai4 天前
milvus+langchain实现RAG应用
langchain·milvus
~kiss~4 天前
Milvus-云原生和分布式的开源向量数据库-介绍
分布式·云原生·milvus
l12345sy6 天前
Milvus——向量索引 :IVF_FLAT
milvus·ivf_flat
大连滚呢王6 天前
Linux(麒麟)服务器离线安装单机Milvus向量库
linux·python·milvus·银河麒麟·milvus_cli
工藤学编程7 天前
零基础学AI大模型之Milvus部署架构选型+Linux实战:Docker一键部署+WebUI使用
人工智能·架构·milvus
工藤学编程7 天前
零基础学AI大模型之Milvus核心:分区-分片-段结构全解+最佳实践
人工智能·milvus
工藤学编程10 天前
零基础学AI大模型之Milvus向量数据库全解析
数据库·人工智能·milvus
yumgpkpm15 天前
Doris 并入CMP7(类Cloudera CDP 7.3.1 404华为鲲鹏ARM版)的方案和实施源代码
大数据·oracle·sqlite·sqoop·milvus·cloudera
寒秋丶15 天前
Milvus:Json字段详解(十)
数据库·人工智能·python·ai·milvus·向量数据库·rag