milvus操作

java

引入依赖

<dependency>
    <groupId>io.milvus</groupId>
    <artifactId>milvus-sdk-java</artifactId>
    <version>2.4.1</version>
</dependency>

配置milvus客户端


import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * milvus配置
 * @author tianluhua
 * @version 1.0
 * @since 2024/8/16 10:15
 */
@Configuration
@ConfigurationProperties(prefix = "milvus.config")
@Data
public class MilvusConfig {

    private String host;

    private Integer port;

    private String database;


    @Bean
    public MilvusServiceClient getMilvusClient() {
        ConnectParam connectParam = ConnectParam.newBuilder()
                .withHost(host)
                .withPort(port)
                .withDatabaseName(database)
                .build();
        return new MilvusServiceClient(connectParam);
    }

}

查询数据

  • 查询使用 SearchParam来构建查询参数。其中
    1. withCollectionName:查询的集合
    2. withVectorFieldName:向量比对的字段
    3. withOutFields:输出的字段名
    4. withFloatVectors:查询的向量。值为2层数组,即可根据多个特征向量查询。查询结果分别返回多个特征向量的结果
    5. withTopK:返回前x条数据
    6. withMetricType:计算相似度方式
      1. L2: 欧几里得计算
      2. COSINE: 余弦相似度计算
  List<List<Float>> text_features = vectorizationResponse.getText_features();

            SearchParam searchParam = SearchParam.newBuilder()
                    .withCollectionName(COLLECTION_NAME)
                    .withVectorFieldName("embedding")
                    .withOutFields("test")
                    .withFloatVectors(text_features)
                    .withMetricType(MetricType.L2)
                    .withTopK(top)
                    .build();
            R<SearchResults> searchResults = milvusServiceClient.search(searchParam);
            SearchResults searchResultsData = searchResults.getData();

            SearchResultsWrapper wrapper = new SearchResultsWrapper(searchResultsData.getResults());

            List<TextSearchImgResponse> textSearchImgResponses = new ArrayList<>();

            for (int i = 0; i < text_features.size(); ++i) {
                List<SearchResultsWrapper.IDScore> scores = wrapper.getIDScore(i);
                if (scores.size() > 0) {
                    for (SearchResultsWrapper.IDScore idScore : scores) {
                        float score = idScore.getScore();
                        Object imagePathO = idScore.getFieldValues().get(SEARCH_RIELD_NAME);
                        if (imagePathO != null) {
                            String relativePath = (imagePathO + "").replace("/cephfs2/data", "")
                                    .replace("/cephfs2/data", "");
                            String imagePath = filePath + relativePath;
                            textSearchImgResponses.add(new TextSearchImgResponse(score, imagePath));
                        }

                    }
                }
            }

Python

使用pip安装milvus库

pip install 

milvus连接方式

MilvusClient

  • 通过MilvusClient可同时连接多个客户端,多个客户端互相不干扰
from pymilvus import MilvusClient

client = MilvusClient(uri="http://<host>:<port>", db_name="db_name")

connections

  • 通过connections连接milvus的同时,Collection和utility默认使用alias为default的连接
  • 可通过using字段切换不同的实例
from pymilvus import connections, Collection, utility

connections.connect(alias= "default",host = "<host>", port="<port>", db_name = "<db_name>")
  • 切换不同的实例
collection = Collection(name="", using="default")

utility.list_collections(using = "default")

数据库管理

posted @ 2024-08-19 13:28  柯南小海盗  阅读(42)  评论(0编辑  收藏  举报