Milvus笔记

Milvus版本 2.3.5

    <dependency>
      <groupId>io.milvus</groupId>
      <artifactId>milvus-sdk-java</artifactId>
      <version>2.3.4</version> 还没有2.3.5依赖
        <!--      <scope>provided</scope>-->
    </dependency>

一、创建操作

1、python版本

from pymilvus import Collection, FieldSchema, DataType, CollectionSchema, connections
from pymilvus.orm import utility, db

from knowledge_brain.milvus_sink import milvus_sink
from study.connect import Connect

class MilvusOperatC:

    def __init__(self, host, port, user, password, db_name ,alias,collection_name,schema,num_shards): #,collection_name,schema,shards_num
        print("加载milvus依赖")
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db_name = db_name
        self.alias = alias
        self.collection_name = collection_name
        self.schema = schema
        self.num_shards = num_shards
        self.collection = self.con()

    #创建连接
    """
    host  ip
    port  端口
    user  用户名
    password 密码
    db_name 数据库
    alias 别名
    """

    def con(self):
        connections.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            port=self.port,
            alias=self.alias)
        # 是否有该数据库,无则新建
        print("建立数据库连接~~~~")
        if self.db_name in db.list_database():
            pass
        else:
            print("没有%s数据库,进行新建~~~~" % self.db_name)
            db.create_database(self.db_name)
            print("新建%s数据库完成!" % self.db_name)
        # 使用数据库,建立数据库连接
        db.using_database(self.db_name)
        # 是否有该集合,无则创建
        if utility.has_collection(self.collection_name):
            print("集合已存在")
            pass
        else:
            print("没有%s集合,进行新建~~~~" % self.collection_name)
            self.col_create()
            print("新建%s集合完成!" % self.collection_name)
        collection = Collection(self.collection_name)
        print("数据库连接完成!")
        return collection

    #集合创建
    """
    collection_name 集合名称
    schema   表头描述信息
    database  数据库信息
    shards_num 分片信息
    """

    def col_create(self):
        # fields = [
        #     FieldSchema(name='vec', dtype=DataType.FLOAT_VECTOR, descrition='embedding vectors', dim=1024),
        #     FieldSchema(name='doc_slicing_id', dtype=DataType.VARCHAR, descrition='doc_slicing_id', max_length=100,
        #                 is_primary=True),
        #     FieldSchema(name='doc_id', dtype=DataType.VARCHAR, descrition='doc_id', max_length=100)
        # ]
        # schema = CollectionSchema(fields=fields, description=self.col_name)
        collection = Collection(name=self.collection_name, schema=self.schema,num_shards=self.num_shards)
        print(collection)

    #索引创建
    def index_create(self,index_params:str,vec_field:str):
        # create IVF_FLAT index for collection.
        # index_params = {
        #     'metric_type': 'L2',
        #     'index_type': "IVF_FLAT",
        #     'params': {"nlist": 150}
        # }
        self.collection.create_index(field_name=vec_field, index_params=index_params)
        # collection.create_index(
        #     field_name="doc_id",
        #     index_name="doc_id_index"
        # )
        # collection.load()

    #分区创建
    def partition_create(self,partition_name:str):
        self.collection.create_partition(partition_name)


    def load(self):
        self.collection.load()
        print("数据load成功")

    def unload(self):
        self.collection.release()


if __name__ == '__main__':
    host='XX.17.38'
    port='31639'
    user='Milvus'
    password='Milvus'
    db_name='knowledge_test'
    alias='default'


    fields = [
        FieldSchema(name='pk', dtype=DataType.INT64, descrition='主键', max_length=200, is_primary=True,
                    auto_id=True),
        FieldSchema(name='car_model', dtype=DataType.VARCHAR, descrition='car_model', max_length=65535, is_primary=False,
                    auto_id=False),
        FieldSchema(name='text', dtype=DataType.VARCHAR, descrition='page_content', max_length=65535, is_primary=False,
                    auto_id=False),
        FieldSchema(name='vector', dtype=DataType.FLOAT_VECTOR, descrition='embedding vectors', dim=1024)
    ]
    schema = CollectionSchema(fields=fields, description='集合描述')

    # Collection('test',schema)
    # MilvusCreate(host,port,user,password,db_name,alias,'test',schema,2)

    milvus_sink(db_name,'test',1,1,1)

python调用

from pymilvus import FieldSchema, DataType, CollectionSchema
from study.MilvusOperatC import MilvusOperatC

if __name__ == '__main__':
    host='XXX.17.38'
    port='31639sss'
    user='Milvus'
    password='Milvus'
    db_name='knowledge_test'
    alias='default'
    fields = [
        FieldSchema(name='pk', dtype=DataType.INT64, descrition='主键', max_length=200, is_primary=True,
                    auto_id=True),
        FieldSchema(name='car_model', dtype=DataType.VARCHAR, descrition='car_model', max_length=65535,
                    is_primary=False,
                    auto_id=False),
        FieldSchema(name='text', dtype=DataType.VARCHAR, descrition='page_content', max_length=65535, is_primary=False,
                    auto_id=False),
        FieldSchema(name='vector', dtype=DataType.FLOAT_VECTOR, descrition='embedding vectors', dim=1024)
    ]
    schema = CollectionSchema(fields=fields, description='集合描述')
    index_params = {
        'metric_type': 'L2',#COSINE IP
        'index_type': "IVF_FLAT",
        'params': {"nlist": 150}
    }
    #embedding字段名称
    vec_field = "vector"
    mc = MilvusOperatC(host,port,user,password,db_name,alias,'test',schema,2)
    mc.index_create(index_params,vec_field)
    mc.partition_create('2024032103')
    mc.load()
    mc.unload()


search_params = {
        "metric_type": "L2",
        "offset": 0,
        "ignore_growing": False,
        "params": {"nprobe": 10}
    }
    mc1 = MilvusOperatR(host, port, user, password, db_name, alias,'test')
    res = mc1.search(search_params=search_params,embeddings=[[0.5441558957099915,0.9319176077842712]],anns_field='vector',topK=10,expr=None,output_fields=['id','vector'])
    print(res)
    output_fields = ["id"]
    res1 = mc1.query(expr="",offset=0,topK=10,output_fields=output_fields)
    print(res1)
    res2 = mc1.count(expr="",output_fields=["count(*)"])
    print(res2)

2、java版本

package com.gwm.milvus;

import com.google.gson.internal.$Gson$Preconditions;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.DataType;
import io.milvus.grpc.GetLoadStateResponse;
import io.milvus.grpc.GetLoadingProgressResponse;
import io.milvus.param.*;
import io.milvus.param.collection.*;
import io.milvus.param.index.CreateIndexParam;
import io.milvus.param.partition.CreatePartitionParam;

/**
 * @author yangyingchun
 * @version 1.0
 * @date 2024/3/20 16:02
 */
public class MilvusOperateC {

    /**
     * 获取连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @param database
     * @return
     */
    public static MilvusServiceClient getConn(String host,Integer port,String username,String password,String database){
        MilvusServiceClient milvusServiceClient= new MilvusServiceClient(ConnectParam
                .newBuilder()
                .withHost(host)
                .withPort(port)
                .withAuthorization(username, password)
                .withDatabaseName(database)
                .build());
        return milvusServiceClient;
    }

    /**
     * 创建集合
     * @param milvusServiceClient
     * @param collectionName
     * @param databaseName
     * @param shardsNum
     * @param description
     * @return
     */
    public static Integer col_create(MilvusServiceClient milvusServiceClient,String collectionName,String databaseName,Integer shardsNum,String description){
        FieldType fieldType1 = FieldType.newBuilder()
                .withName("id")
                .withDataType(DataType.Int64)
                .withPrimaryKey(true)
                .withAutoID(true)
                .build();
        FieldType fieldType2 = FieldType.newBuilder()
                .withName("text")
                .withDataType(DataType.VarChar)
                .withMaxLength(65535) //varchar类型必填
                .build();
        FieldType fieldType3 = FieldType.newBuilder()
                .withName("vector")
                .withDataType(DataType.FloatVector)
                .withDimension(1024)
                .build();
        CreateCollectionParam createCollectionParam
                = CreateCollectionParam.newBuilder()
                .withCollectionName(collectionName)
                .withDatabaseName(databaseName)
                .withShardsNum(shardsNum)
                .withDescription(description)
                .addFieldType(fieldType1)
                .addFieldType(fieldType2)
                .addFieldType(fieldType3)
                .build();
        R<RpcStatus> collection = milvusServiceClient.createCollection(createCollectionParam);
        Integer status = collection.getStatus();
        return status;
    }

    /**
     * 创建 索引
     * @param milvusServiceClient
     * @param metricType
     * @param collectionName
     * @param fieldName
     * @param INDEX_TYPE
     * @param INDEX_PARAM
     * @return
     */
    public static Integer index_create(MilvusServiceClient milvusServiceClient,MetricType metricType, String collectionName, String fieldName, IndexType INDEX_TYPE,String INDEX_PARAM){
        R<RpcStatus> index = milvusServiceClient.createIndex(
                CreateIndexParam.newBuilder()
                        .withCollectionName(collectionName)
                        .withFieldName(fieldName)
                        .withIndexType(INDEX_TYPE)
                        .withMetricType(metricType)
                        .withExtraParam(INDEX_PARAM)
                        .withSyncMode(Boolean.FALSE)
                        .build()
        );
        Integer status = index.getStatus();
        return status;
    }

    /**
     * 创建分区
     * @param milvusServiceClient
     * @param collectionName
     * @param partitionName
     * @return
     */
    public static Integer partition_create(MilvusServiceClient milvusServiceClient,String collectionName,String partitionName){
        R<RpcStatus> partition = milvusServiceClient.createPartition(
                CreatePartitionParam.newBuilder()
                        .withCollectionName(collectionName)
                        .withPartitionName(partitionName)
                        .build()
        );
        Integer status = partition.getStatus();
        return status;
    }

    /**
     * load数据到内存中,load进展情况
     * @param milvusServiceClient
     * @param collectionName
     * @param database
     * @return
     */
    public static Integer load(MilvusServiceClient milvusServiceClient,String collectionName,String database){
        // You can check the loading status
        Integer loadstatus = -1;
        GetLoadStateParam loadparam = GetLoadStateParam.newBuilder()
                .withCollectionName(collectionName)
                .build();
        R<GetLoadStateResponse> responseLoad = milvusServiceClient.getLoadState(loadparam);
        System.out.println("当前状态:"+responseLoad.getData().getState());
        System.out.println("当前状态:"+responseLoad.getStatus() + R.Status.Success.getCode());
        if (responseLoad.getStatus() != R.Status.Success.getCode()) {
            System.out.println(responseLoad.getMessage());
        }
//        System.out.println(responseLoad.getStatus());
//
//        // and loading progress as well
//
//        GetLoadingProgressParam loadingparam = GetLoadingProgressParam.newBuilder()
//                .withCollectionName(collectionName)
//                .build();
//        R<GetLoadingProgressResponse> responseLoading = milvusServiceClient.getLoadingProgress(loadingparam);
//        if (responseLoading.getStatus() != R.Status.Success.getCode()) {
//            System.out.println(responseLoading.getMessage());
//        }
//        /**
//         * load进展
//         */
//        System.out.println(responseLoading.getData().getProgress());
        LoadCollectionParam loadCollectionParam = LoadCollectionParam.newBuilder().withDatabaseName(database).withCollectionName(collectionName).build();
        R<RpcStatus> rpcStatusR = milvusServiceClient.loadCollection(loadCollectionParam);
        loadstatus = rpcStatusR.getStatus();
        return loadstatus;
    }

    /**
     * 释放内存数据
     * @param milvusServiceClient
     * @param collectionName
     * @return
     */
    public static Integer release(MilvusServiceClient milvusServiceClient,String collectionName){
        R<RpcStatus> rpcStatusR = milvusServiceClient.releaseCollection(
                ReleaseCollectionParam.newBuilder()
                        .withCollectionName(collectionName)
                        .build()
        );
        Integer status = rpcStatusR.getStatus();
        return status;
    }
}

rpcStatusR.getStatus()状态码
Success(0),
UnexpectedError(1),
ConnectFailed(2),           // Deprecated
PermissionDenied(3),        // Deprecated
CollectionNotExists(4),     // Deprecated
IllegalArgument(5),
IllegalDimension(7),        // Deprecated
IllegalIndexType(8),        // Deprecated
IllegalCollectionName(9),   // Deprecated
IllegalTOPK(10),            // Deprecated
IllegalRowRecord(11),       // Deprecated
IllegalVectorID(12),        // Deprecated
IllegalSearchResult(13),    // Deprecated
FileNotFound(14),           // Deprecated
MetaFailed(15),             // Deprecated
CacheFailed(16),            // Deprecated
CannotCreateFolder(17),     // Deprecated
CannotCreateFile(18),       // Deprecated
CannotDeleteFolder(19),     // Deprecated
CannotDeleteFile(20),       // Deprecated
BuildIndexError(21),        // Deprecated
IllegalNLIST(22),           // Deprecated
IllegalMetricType(23),      // Deprecated
OutOfMemory(24),            // Deprecated
IndexNotExist(25),          // Deprecated
EmptyCollection(26),
// internal error code.
DDRequestRace(1000),        // Deprecated
// Client side error
RpcError(-1),
ClientNotConnected(-2),
Unknown(-3),
VersionMismatch(-4),
ParamError(-5),
IllegalResponse(-6);
      

调用

package com.gwm.milvus;

import io.milvus.client.MilvusServiceClient;
import io.milvus.param.IndexType;
import io.milvus.param.MetricType;

/**
 * @author yangyingchun
 * @version 1.0
 * @date 2024/3/20 16:50
 */
public class MilvusTest {

    public static void main(String[] args) {

        String collectionName = "repositoryId_test1";
        String databaseName = "knowledge_cosine_test";
        Integer shards =2;
        String description = "测试java创建collection";
        MilvusServiceClient milvusClient =
                MilvusOperateC.getConn("XXX.17.38", 31630, "Milvus", "Milvus", "knowledge_test");

        Integer integer = MilvusOperateC.col_create(milvusClient,collectionName,databaseName,shards,description);
        System.out.println("collection创建状态:"+integer);
        String INDEX_PARAM = "{\"nlist\":1024}";
        Integer vector = MilvusOperateC.index_create(milvusClient, MetricType.COSINE, collectionName, "vector", IndexType.IVF_FLAT, INDEX_PARAM);
        System.out.println("索引创建状态:"+vector);
        Integer partitionCreate = MilvusOperateC.partition_create(milvusClient, collectionName, "20240320");
        System.out.println("分区创建状态:"+partitionCreate);
        Integer load = MilvusOperateC.load(milvusClient, collectionName, databaseName);
        System.out.println("集合load状态:"+load);
        Integer release = MilvusOperateC.release(milvusClient, collectionName);
        System.out.println("集合释放状态"+release);



ArrayList<ArrayList<Float>> embeddings = new ArrayList<>();
        List<Float> list = new ArrayList<>();
        list.add((float) 0.5441558957099915);
        list.add((float) 0.9319176077842712);
        ArrayList<String> outFields = new ArrayList<>();
        outFields.add("id");
        embeddings.add((ArrayList<Float>) list);
        ArrayList<String> countField = new ArrayList<>();
        countField.add("count(*)");
        SearchResults result = MilvusOperateR.search(milvusClient, collectionName, null, ConsistencyLevelEnum.STRONG, MetricType.L2,
                "vector", embeddings, 10, "", outFields
        );
        System.out.println(result.getResults());

        QueryResults query = MilvusOperateR.query(milvusClient, collectionName, null, ConsistencyLevelEnum.STRONG, "", 0L, 10L, outFields);
        System.out.println(query);

        QueryResults count = MilvusOperateR.count(milvusClient, collectionName, "", countField);
        System.out.println(count);
        List<FieldData> fieldsDataList = count.getFieldsDataList();
        for (FieldData fieldData:fieldsDataList){
            System.out.println(fieldData.getType());
            System.out.println(fieldData.getFieldName());
            System.out.println(fieldData.getScalars().getLongData().getData(0));
        }

    }


 System.out.println(MilvusCheck.databaseCheck(milvusClient, "test"));
        System.out.println(MilvusCheck.collectionCheck(milvusClient,databaseName,collectionName));
        System.out.println(MilvusCheck.partitionCheck(milvusClient,collectionName,"20240325"));
}

二、删除操作

1、python版本

from pymilvus import connections, Collection
from pymilvus.orm import db, utility


class MilvusOperatD:
    def __init__(self,host, port, user, password, db_name ,alias):
        print("加载milvus依赖")
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db_name = db_name
        self.alias = alias
        self.collection = self.con()

    def con(self):
        connections.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            port=self.port,
            alias=self.alias)
        # 是否有该数据库,无则新建
        print("建立数据库连接~~~~")
        if self.db_name in db.list_database():
            pass
        else:
            print("没有%s数据库,进行新建~~~~" % self.db_name)
            db.create_database(self.db_name)
            print("新建%s数据库完成!" % self.db_name)
        # 使用数据库,建立数据库连接
        db.using_database(self.db_name)
        # 是否有该集合,无则创建
        if utility.has_collection(self.collection_name):
            print("集合已存在")
            pass
        else:
            print("没有%s集合,进行新建~~~~" % self.collection_name)
            self.col_create()
            print("新建%s集合完成!" % self.collection_name)
        collection = Collection(self.collection_name)
        print("数据库连接完成!")
        return collection
    #删除数据库
    def database_del(self,database_name):
        db.drop_database(database_name)

    #删除集合
    def collection_del(self,collection_name):
        utility.drop_collection(collection_name)

    #删除分区
    def partition_del(self,partition_name):
        self.collection.drop_partition(partition_name)
    #删除索引
    def index_del(self,index_name):
        self.collection.drop_index(index_name=index_name)

    #删除数据
    #expr rules  https://milvus.io/docs/v2.3.x/boolean.md
    def entity_del(self,expr):
        self.collection.delete(expr)


2、java版本

package com.gwm.milvus;

import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.MutationResult;
import io.milvus.param.ConnectParam;
import io.milvus.param.R;
import io.milvus.param.RpcStatus;
import io.milvus.param.collection.DropCollectionParam;
import io.milvus.param.collection.DropDatabaseParam;
import io.milvus.param.dml.DeleteParam;
import io.milvus.param.index.DropIndexParam;
import io.milvus.param.partition.DropPartitionParam;

/**
 * @author yangyingchun
 * @version 1.0
 * @date 2024/3/25 13:18
 */
public class MilvusOperateD {
    /**
     * 获取连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @param database
     * @return
     */
    public static MilvusServiceClient getConn(String host, Integer port, String username, String password, String database){
        MilvusServiceClient milvusServiceClient= new MilvusServiceClient(ConnectParam
                .newBuilder()
                .withHost(host)
                .withPort(port)
                .withAuthorization(username, password)
                .withDatabaseName(database)
                .build());
        return milvusServiceClient;
    }

    /**
     * 删除数据库
     * @param milvusServiceClient
     * @param databaseName
     * @return
     */
    public static Integer database_del(MilvusServiceClient milvusServiceClient,String databaseName){
        DropDatabaseParam dropDatabaseParam= DropDatabaseParam.newBuilder().withDatabaseName(databaseName).build();
        R<RpcStatus> rpcStatusR = milvusServiceClient.dropDatabase(dropDatabaseParam);
        Integer status = rpcStatusR.getStatus();
        return status;
    }
    /**
     * 删除集合
     * @param milvusServiceClient
     * @param collectionName
     * @return
     */
    public static Integer collection_del(MilvusServiceClient milvusServiceClient,String databaseName,String collectionName){
        DropCollectionParam dropCollectionParam= DropCollectionParam.newBuilder().withDatabaseName(databaseName)
                .withCollectionName(collectionName).build();
        R<RpcStatus> rpcStatusR = milvusServiceClient.dropCollection(dropCollectionParam);
        Integer status = rpcStatusR.getStatus();
        return status;
    }

    /**
     * 删除分区
     * @param milvusServiceClient
     * @param collectionName
     * @return
     */
    public static Integer partition_del(MilvusServiceClient milvusServiceClient,String collectionName,String partitionName){
        DropPartitionParam dropPartitionParam= DropPartitionParam.newBuilder()
                .withCollectionName(collectionName)
                .withPartitionName(partitionName)
                .build();
        R<RpcStatus> rpcStatusR = milvusServiceClient.dropPartition(dropPartitionParam);
        Integer status = rpcStatusR.getStatus();
        return status;
    }

    /**
     * 删除索引
     * @param milvusServiceClient
     * @param collectionName
     * @return
     */
    public static Integer index_del(MilvusServiceClient milvusServiceClient,String collectionName,String indexName){
        DropIndexParam indexParam= DropIndexParam.newBuilder()
                .withCollectionName(collectionName)
                .withIndexName(indexName)
                .build();
        R<RpcStatus> rpcStatusR = milvusServiceClient.dropIndex(indexParam);
        Integer status = rpcStatusR.getStatus();
        return status;
    }

    /**
     * 删除数据
     * @param milvusServiceClient
     * @param collectionName
     * @return
     */
      public static Integer entity_del(MilvusServiceClient milvusServiceClient,String collectionName,String partitionName,String expr){
        DeleteParam deleteParam = DeleteParam.newBuilder()
                .withCollectionName(collectionName)
                .withPartitionName(partitionName)
                .withExpr(expr)
                .build();
        R<MutationResult> delete = milvusServiceClient.delete(deleteParam);
        Integer status = delete.getStatus();
        return status;
    }
}

三、修改操作

1、python版本

from random import random

from pymilvus import connections, Collection
from pymilvus.orm import db, utility


class MilvusOperatU:
    def __init__(self,host, port, user, password, db_name ,alias):
        print("加载milvus依赖")
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db_name = db_name
        self.alias = alias
        self.collection = self.con()

    def con(self):
        connections.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            port=self.port,
            alias=self.alias)
        # 是否有该数据库,无则新建
        print("建立数据库连接~~~~")
        if self.db_name in db.list_database():
            pass
        else:
            print("没有%s数据库,进行新建~~~~" % self.db_name)
            db.create_database(self.db_name)
            print("新建%s数据库完成!" % self.db_name)
        # 使用数据库,建立数据库连接
        db.using_database(self.db_name)
        # 是否有该集合,无则创建
        if utility.has_collection(self.collection_name):
            print("集合已存在")
            pass
        else:
            print("没有%s集合,进行新建~~~~" % self.collection_name)
            self.col_create()
            print("新建%s集合完成!" % self.collection_name)
        collection = Collection(self.collection_name)
        print("数据库连接完成!")
        return collection

    def collection_rename(self,old_collection,new_collection):
        utility.rename_collection(old_collection, new_collection) # Output: True

    def collection_modify(self):
        self.collection.set_properties(properties={"collection.ttl.seconds": 1800})

    def upsert_entities(self,data):
        # nb = 3000
        # dim = 8
        # vectors = [[random.random() for _ in range(dim)] for _ in range(nb)]
        # data = [
        #     [i for i in range(nb)],
        #     [str(i) for i in range(nb)],
        #     [i for i in range(10000, 10000 + nb)],
        #     vectors,
        #     [str("dy" * i) for i in range(nb)]
        # ]
        mr = self.collection.upsert(data)
        mr.delete_count
        mr.upsert_count
        mr.insert_count


2、java版本

package com.gwm.milvus;

import com.alibaba.fastjson.JSONObject;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.MutationResult;
import io.milvus.param.ConnectParam;
import io.milvus.param.R;
import io.milvus.param.dml.InsertParam;
import io.milvus.param.dml.UpsertParam;

import java.util.List;

/**
 * @author yangyingchun
 * @version 1.0
 * @date 2024/3/25 13:54
 */
public class MilvusOperateU {
    /**
     * 获取连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @param database
     * @return
     */
    public static MilvusServiceClient getConn(String host, Integer port, String username, String password, String database){
        MilvusServiceClient milvusServiceClient= new MilvusServiceClient(ConnectParam
                .newBuilder()
                .withHost(host)
                .withPort(port)
                .withAuthorization(username, password)
                .withDatabaseName(database)
                .build());
        return milvusServiceClient;
    }

    public static void collection_modify(MilvusServiceClient milvusServiceClient,String  collectionName){
        //没有java api
    }
    public static Integer upsert_entities(MilvusServiceClient milvusServiceClient, String databaseName, String colectionName, String partitionName
                    , List<InsertParam.Field> fields,List<JSONObject> rows){
        UpsertParam upsertParam = UpsertParam.newBuilder()
                .withDatabaseName(databaseName)
                .withCollectionName(colectionName)
                .withPartitionName(partitionName)
                .withFields(fields)
                .withRows(rows)
                .build();
        R<MutationResult> upsert = milvusServiceClient.upsert(upsertParam);
        Integer status = upsert.getStatus();
        return status;
    }
}

四、查询操作

1、python版本

import json

from pymilvus import connections, Collection
from pymilvus.orm import db, utility


class MilvusOperatR:
    def __init__(self,host, port, user, password, db_name ,alias,collection_name):
        print("加载milvus依赖")
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db_name = db_name
        self.collection_name = collection_name
        self.alias = alias
        self.collection = self.con()

    def con(self):
        connections.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            port=self.port,
            alias=self.alias)
        # 是否有该数据库,无则新建
        print("建立数据库连接~~~~")
        if self.db_name in db.list_database():
            pass
        else:
            print("没有%s数据库,进行新建~~~~" % self.db_name)
            db.create_database(self.db_name)
            print("新建%s数据库完成!" % self.db_name)
        # 使用数据库,建立数据库连接
        db.using_database(self.db_name)
        # 是否有该集合,无则创建
        if utility.has_collection(self.collection_name):
            print("集合已存在")
            pass
        else:
            print("没有%s集合,进行新建~~~~" % self.collection_name)
            self.col_create()
            print("新建%s集合完成!" % self.collection_name)
        collection = Collection(self.collection_name)
        print("数据库连接完成!")
        return collection

    def search(self,search_params,embeddings,anns_field,topK,expr,output_fields):
        #load状态才能查询
        self.collection.load()
        # search_params = {
        #     "metric_type": "L2",
        #     "offset": 0,
        #     "ignore_growing": False,
        #     "params": {"nprobe": 10}
        # }
        results = self.collection.search(
            data=embeddings,
            anns_field=anns_field,
            # the sum of `offset` in `param` and `limit`
            # should be less than 16384.
            param=search_params,
            limit=topK,
            expr=expr,
            # set the names of the fields you want to
            # retrieve from the search result.
            output_fields=output_fields,
            consistency_level="Strong"
        )
        return results

    def query(self, expr, offset, topK, output_fields):
        self.collection.load()
        res = self.collection.query(
            expr=expr,
            offset=offset,
            limit=topK,
            # output_fields=["book_id", "book_intro"],
            output_fields=output_fields
        )
        return res

    def count(self, expr, output_fields):
        self.collection.load()

        #count计算不允许设置limit
        res = self.collection.query(
            expr=expr,
            # output_fields = ["count(*)"],
            output_fields=output_fields
        )
        return res

2、java版本

package com.gwm.milvus;

import io.milvus.client.MilvusServiceClient;
import io.milvus.common.clientenum.ConsistencyLevelEnum;
import io.milvus.grpc.QueryResults;
import io.milvus.grpc.SearchResults;
import io.milvus.param.ConnectParam;
import io.milvus.param.MetricType;
import io.milvus.param.R;
import io.milvus.param.dml.QueryParam;
import io.milvus.param.dml.SearchParam;
import io.milvus.param.highlevel.dml.QuerySimpleParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangyingchun
 * @version 1.0
 * @date 2024/3/25 15:28
 */
public class MilvusOperateR {
    /**
     * 获取连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @param database
     * @return
     */
    public static MilvusServiceClient getConn(String host, Integer port, String username, String password, String database){
        MilvusServiceClient milvusServiceClient= new MilvusServiceClient(ConnectParam
                .newBuilder()
                .withHost(host)
                .withPort(port)
                .withAuthorization(username, password)
                .withDatabaseName(database)
                .build());
        return milvusServiceClient;
    }

    public static SearchResults search(MilvusServiceClient milvusServiceClient, String collectionName, List<String> partitionNames
                            , ConsistencyLevelEnum levelEnum    , MetricType metricType, String vectorName
                            , ArrayList<ArrayList<Float>> embeddings, Integer topK, String expr, List<String> outFields
                              ){
        SearchParam searchParam = SearchParam.newBuilder()
                .withCollectionName(collectionName)
//                .withPartitionNames(partitionNames)
                .withConsistencyLevel(levelEnum)
                .withMetricType(metricType)
                .withVectorFieldName(vectorName)
                .withVectors(embeddings)
                .withTopK(topK)
                .withExpr(expr)
                .withOutFields(outFields)
                .build();
        R<SearchResults> search = milvusServiceClient.search(searchParam);
        SearchResults data = search.getData();
        return data;

    }
    public static QueryResults query(MilvusServiceClient milvusServiceClient,String collectionName,List<String> partitionNames
                    ,ConsistencyLevelEnum levelEnum, String  expr      ,Long offset     ,Long topK,List<String> outFields
    ){
        QueryParam queryParam = QueryParam.newBuilder()
                .withCollectionName(collectionName)
//                .withPartitionNames(partitionNames)
                .withConsistencyLevel(levelEnum)
                .withExpr(expr)
                .withOffset(offset)
                .withLimit(topK)
                .withOutFields(outFields)
                .build();
        R<QueryResults> query = milvusServiceClient.query(queryParam);
        QueryResults data = query.getData();
        return data;

    }
    public static QueryResults count(MilvusServiceClient milvusServiceClient,String collectionName,String expr,List<String> outFields){
        QueryParam queryParam = QueryParam.newBuilder()
                .withCollectionName(collectionName)
                .withExpr(expr)
                .withOutFields(outFields) //["count(*)"]
                .build();
        R<QueryResults> query = milvusServiceClient.query(queryParam);
        QueryResults data = query.getData();
        return data;
    }
}

五、校验操作

1、python版本

from pymilvus import Collection
from pymilvus.orm import db, utility


class MilvusCheck:
    def __init__(self, host, port, user, password, db_name, alias, collection_name):
        print("加载milvus依赖")
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db_name = db_name
        self.collection_name = collection_name
        self.alias = alias
    def database_check(self):
        if self.db_name in db.list_database():
            return True
        else:
            return False
    def collection_check(self):
        if  utility.has_collection(self.collection_name):
            return True
        else:
            return False
    def partition_check(self,partition_name):
        collection = Collection(self.collection_name)
        if collection.has_partition(partition_name):
            return True
        else:
            return False
    def index_check(self):
        collection = Collection(self.collection_name)
        if collection.has_index():
            return True
        else:
            return False

2、java版本

package com.gwm.milvus;

import com.google.protobuf.ProtocolStringList;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.ListDatabasesResponse;
import io.milvus.param.ConnectParam;
import io.milvus.param.R;
import io.milvus.param.collection.HasCollectionParam;
import io.milvus.param.partition.HasPartitionParam;

/**
 * @author yangyingchun
 * @version 1.0
 * @date 2024/3/25 16:17
 */
public class MilvusCheck {
    /**
     * 获取连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @param database
     * @return
     */
    public static MilvusServiceClient getConn(String host, Integer port, String username, String password, String database){
        MilvusServiceClient milvusServiceClient= new MilvusServiceClient(ConnectParam
                .newBuilder()
                .withHost(host)
                .withPort(port)
                .withAuthorization(username, password)
                .withDatabaseName(database)
                .build());
        return milvusServiceClient;
    }

    public static boolean databaseCheck(MilvusServiceClient milvusServiceClient,String databaseName){
        R<ListDatabasesResponse> listDatabasesResponseR = milvusServiceClient.listDatabases();
        ListDatabasesResponse data = listDatabasesResponseR.getData();
        System.out.println(data);
        ProtocolStringList dbNamesList = data.getDbNamesList();
        if (dbNamesList.contains(databaseName)){
            return true;
        }else {
            return false;
        }
    }

    public static boolean  collectionCheck(MilvusServiceClient milvusServiceClient,String databaseName,String collectionName){
        HasCollectionParam hasCollectionParam = HasCollectionParam.newBuilder()
                .withDatabaseName(databaseName)
                .withCollectionName(collectionName)
                .build();
        R<Boolean> booleanR = milvusServiceClient.hasCollection(hasCollectionParam);
        boolean b = booleanR.getData().booleanValue();
        return b;
    }

    public static boolean partitionCheck(MilvusServiceClient milvusServiceClient,String collectionName,String partitionName){
        HasPartitionParam hasPartitionParam = HasPartitionParam.newBuilder()
                .withCollectionName(collectionName)
                .withPartitionName(partitionName)
                .build();
        R<Boolean> booleanR = milvusServiceClient.hasPartition(hasPartitionParam);
        boolean b = booleanR.getData().booleanValue();
        return b;
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Milvus 是一个开源的向量相似度搜索引擎,而Spring Boot 是一个用于构建基于 Java 的独立、生产级的应用程序的框架。 Milvus Spring Boot 是将 Milvus 与 Spring Boot 框架结合使用的一种方式。借助 Spring Boot,我们可以更方便地构建基于 Milvus 的应用程序。 首先,我们可以使用 Spring Boot 的依赖管理功能,将 MilvusJava 客户端库添加到项目中。这样,我们就可以在我们的应用程序中直接使用 Milvus 的功能,如向量的插入、查询和删除等。 其次,Spring Boot 提供了强大的配置管理功能,我们可以轻松地将 Milvus 的连接配置信息添加到应用程序的配置文件中,例如指定 Milvus 的 IP 地址、端口号和连接池大小等。这样,我们就可以灵活地管理 Milvus 与其他组件的连接。 另外,Spring Boot 还提供了便捷的 RESTful API 开发功能。我们可以利用这一特性,将 Milvus 的搜索引擎功能以接口的形式暴露给客户端,使得客户端可以通过 HTTP 请求来进行向量的检索。这样,我们可以轻松地建立一个灵活、高性能的分布式向量搜索系统。 总的来说,Milvus Spring Boot 结合了 Milvus 的强大功能和 Spring Boot 的便捷开发特性,使得我们可以更快速、灵活地搭建起一个高性能的向量搜索应用程序。它在大数据、人工智能等领域有广泛的应用前景,可以应对各种复杂的向量查询需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值