新手如何本地构建Milvus向量数据库

简单构建一个Milvus数据库

    • 一、前言:什么是Milvus数据库
    • 二、安装Docker
      • 官方下载地址:
      • 配置Docker
    • 三、安装Milvus
    • 四、Milvus关键概念介绍
      • 1、首先创建数据库
      • 2、然后创建逻辑定义
      • 3、添加字段
      • 4、创建集合collection
      • 5、建立索引(有索引才能查询数据)
      • 6、插入更新删除数据
      • 7、查询数据(查询limit个相似向量)

一、前言:什么是Milvus数据库

Milvus 是一款 ‌开源向量数据库‌(2019年推出),主要用于RAG外接知识库,检索与query相似向量,其核心目标是解决人工智能领域(如机器学习、深度学习)中大规模向量数据的实时搜索难题,可将图像、文本、音视频等转换为高维向量,支持十亿级向量实时检索,本文将介绍Milvus的windows本地部署并介绍一些关键概念。

二、安装Docker

官方下载地址:

https://docs.docker.com/desktop/install/windows-install/

配置Docker

Docker的配置看这篇:
https://blog.csdn.net/qq_60750453/article/details/128636298

三、安装Milvus

1、以管理员模式打开 Docker Desktop

2、以管理员模式打开PowerSheel
执行:

Invoke-WebRequest https://raw.githubusercontent.com/milvus-io/milvus/refs/heads/master/scripts/standalone_embed.bat -OutFile standalone.bat
C:\>standalone.bat start
Wait for Milvus starting...
Start successfully.
To change the default Milvus configuration, edit user.yaml and restart the service.

运行安装脚本后

名为Milvus-standalone的 docker 容器已在19530 端口启动。

嵌入式 etcd 与 Milvus 安装在同一个容器中,服务端口为2379。其配置文件被映射到当前文件夹中的embedEtcd.yaml。

Milvus 数据卷映射到当前文件夹中的volumes/milvus。

可以使用以下命令管理 Milvus 容器和存储的数据。

# Stop Milvus
C:\>standalone.bat stop
Stop successfully.

# Delete Milvus container
C:\>standalone.bat delete
Delete Milvus container successfully. # Container has been removed.
Delete successfully. # Data has been removed.

四、Milvus关键概念介绍

可以将Milvus当作一个普通数据库,其中Collection相当于关系数据库中的表,Field相当于关系数据库中的字段,schema是字段和表的逻辑定义

1、首先创建数据库

client = MilvusClient(uri="http://localhost:19530")
client.create_database("milvus_demo1")
client.use_database("milvus_demo1")

2、然后创建逻辑定义

schema  = client.create_schema(
        auto_id=False,
        enable_dynamic_field=True,
        dimension=5,
        metric_type = 'IP'
    )

3、添加字段

schema.add_field(field_name='id',datatype=pymilvus.DataType.INT64,is_primary=True)

schema.add_field(field_name = 'vector',datatype=pymilvus.DataType.FLOAT_VECTOR,dim=5)

4、创建集合collection

client.create_collection(collection_name='demo',schema=schema,description='demo collection')

5、建立索引(有索引才能查询数据)

index_params = client.prepare_index_params()
    index_params.add_index(
        field_name='vector',
        metric_type='IP',
        index_type='',  # 为空使用自动索引
        index_name="vector_index"
        )
client.create_index(collection_name="demo", index_params=index_params)

6、插入更新删除数据

data = [{"id": 1, "vector": [1, 1.1 , 2 , 4 , 5]}]
res = client.insert(collection_name='demo', data=data)
#res---> {'insert_count': 1, 'ids': [1]}
res = client.upsert(collection_name='demo', data=data) #更新第一个匹配的主键数据,没有则插入
#res upsert ----> {'upsert_count': 1}
res = client.delete(collection_name='demo', filter='id in [1,2,3]')
#res delete----> {'delete_count': 1}

7、查询数据(查询limit个相似向量)

#需要先将数据加载到内存
client.load_collection(collection_name='demo') 
#按照id查询
res = client.get(collection_name='demo', ids=[1])

#向量搜索,返回相似向量
res = client.search(collection_name='demo', data=[[1, 1.1 , 2 , 4 , 5.5]], limit=2,
search_params={"metric_type": "IP", "params": {}},output_fields=["id", 'vector'],partition_names=["partition"]#无分区可以不写
,filter='id like "1%"')

#范围搜索
search_params = {
                "metric_type": "IP",
                "params": {
                        "radius": 0.6,    # 搜索圆的半径
                        "range_filter": 2    # 保留distance<2的
             }
     }
        res = client.search(
                collection_name="demo_v2",
                data=[[1, 1.1 , 2 , 4 , 5.5]],
                limit=2,    # 返回的搜索结果最大数量
                search_params=search_params,
                output_fields=["vector"],
     )
     
#混合搜索
from pymilvus import AnnSearchRequest,RRFRanker

data = [{"id": 1, "vector": [1, 1.1 , 2 , 4 , 5],"VectorA":[1,2,3,4,5],"VectorB":[1,2,3,4,5]}]

res = client.insert(collection_name='demo', data=data)

query_filmVectorA = [[1,2,3,4,5]]
query_filmVectorB = [[5,4,3,2,1]]
dense_search_params1 = {"data": query_filmVectorA ,
                       "anns_field": "VectorA", #检索字段A
                       "param": {"metric_type": "L2"},"limit": 2}
dense_search_params2 = {"data": query_filmVectorB ,
                       "anns_field": "VectorB", #检索字段B
                       "param": {"metric_type": "COSINE"},"limit": 2}
#AnnSearchRequest对向量字段执行单独搜索
request_1 = AnnSearchRequest(**sparse_search_params1)
request_2 = AnnSearchRequest(**sparse_search_params2)
reqs = [request_1 ,request_2]
#为每个结果分配分数
ranker = RRFRanker(100)
#调用多个AnnSearchRequest,用ranker排序后返回limit个结果
res=client.hybrid_search(
        collection_name="demo",
        reqs=reqs,
        ranker=ranker,
        limit=2)

以上是对Milvus的简单介绍

你可能感兴趣的:(milvus,数据库)