Milvus 安装、设置权限和使用

中文文档

使用了docker compose 安装 standalone 版本,即单机docker。

pymilvus 一直在升级,一些函数有变化,中文文档有落后,建议看英文文档。

安装

复制代码
#下载docker-compose.yml 文件
wget https://github.com/milvus-io/milvus/releases/download/v2.4.9/milvus-standalone-docker-compose.yml -O docker-compose.yml

# minio端口如果不用可以注释掉
   #ports:
   #  - "9001:9001"
   #  - "9000:9000"

#standalone 也就是milvus的映射端口,主机端口:容器内端口
#可以修改主机端口,防止冲突,容器内端口不可修改。
    ports:
      - "29530:19530"
      - "29091:9091"

# 启动 Milvus
sudo docker compose up -d

#关闭Milvus
sudo docker compose down

如果 docker compose 不能work,查看一下docker 版本,使用 docker-compose 也许可以。

设置权限 通过用户名和密码进行身份验证

复制代码
在 配置 Milvus 时,将 milvus.yaml 中的 common.security.authorizationEnabled 设为 true 以启用身份验证

由于使用了docker,milvus.yaml文件在docker容器里。

复制代码
#进入容器
sudo docker exec -it  Milvus容器ID  /bin/bash
#修改 milvus.yaml
sed -i 's/authorizationEnabled: false/authorizationEnabled: true/g'  configs/milvus.yaml
#退出容器,而不关闭容器
依次按 ctrl+p 和 ctrl+q 

默认用户名:root

默认密码: Milvus

复制代码
from pymilvus import MilvusClient
#连接
client = MilvusClient(
    uri='http://localhost:29530', # replace with your own Milvus server address
    token="root:Milvus"
) 
#修改密码
client.update_password(
    user_name="root",
    old_password="Milvus",
    new_password="P@ssw0rd123"
)
#创建新用户
client.create_user(
    user_name="user_1",
    password="P@ssw0rd",
)
client.describe_user("user_1")

基本使用

python 复制代码
from pymilvus import MilvusClient,DataType

client = MilvusClient(uri="http://localhost:29530",token="用户名:密码")
coll_name = "test_zhishi"

collecitons = client.list_collections()
print(collections)

创建collection

在我使用的这个版本的Milvus docker中,id是必须是有的,即使你不定义;而且 auto_id=False这个参数,设置为True也无效,所以插入数据的时候必须插入id,不然就会报错。 可以通过describe_collection查看schema。

python 复制代码
if coll_name  not in collections:
    schema = MilvusClient.create_schema(
        auto_id=False,
        enable_dynamic_field=True,
    )

    schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)
    schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=1024)
    schema.add_field(field_name="text", datatype=DataType.VARCHAR)
    schema.add_field(field_name="subject", datatype=DataType.VARCHAR)

    index_params = client.prepare_index_params(
        field_name="vector",
        index_type="AUTOINDEX",
        metric_type="IP"
    )

    client.create_collection(
        collection_name="test_zhishi",
        schema=schema,
        dimension=1024,
        index_params=index_params
    )

删除collection

python 复制代码
status = client.drop_collection(collection_name=coll_name)
print(status)

查看collection

python 复制代码
schema = client.describe_collection(collection_name=coll_name)
print(schema)

插入数据

python 复制代码
data =[]
for i,doc in enumerate(docs[1:]):
    vector = predict_embedding(doc)
    data.append({'id':i,"vector":vector[0],"text":doc,"subject":"zhishi"})

res = client.insert(collection_name=coll_name,data=data)
print(res)

查询数据

python 复制代码
query = ["明天天气怎么样"]
query_embedding = predict_embedding(query)
res = client.search(collection_name=coll_name,data=query_embedding,limit=1,output_fields=['text','subject'])
print(res)

更新数据

复制代码
res = client.upsert( collection_name=coll_name,  data=data)
print(res)

删除数据

复制代码
res = client.delete( collection_name=coll_name, filter="id in [4,5,6]")
print(res)
相关推荐
DreamBoy_W.W.Y11 小时前
Milvus搜索机制
milvus
TheBestRucy13 天前
从零构建企业级“中文法律智能问答系统”:FastAPI+Milvus+混合检索+真流式RAG实战
fastapi·milvus
Flynn Liang13 天前
内存/Redis/PGVector/Milvus/ES 怎么选?Spring AI VectorStore 抽象与向量库选型对照表
redis·elasticsearch·milvus·rag·springai·pgvector·向量库
kyrie_sakura14 天前
大模型学习19 大模型学习 - LangChain2_Retrieval(RAG-文档处理-向量处理检索-Milvus)
学习·langchain·milvus
Maiko Star15 天前
*【HM-LC】Milvus向量库详解:Collection、索引、混合检索与LangChain实战
langchain·milvus
霸道流氓气质17 天前
AI 图片管理平台:Spring AI 原生多模态、Milvus 向量语义检索与 MinIO 预签名安全管控的 React + 全栈生产级实战
人工智能·spring·milvus
一枕槐安48717 天前
多模态RAG实战:PDF图文混合文档检索问答本地跑通
langchain·pdf·milvus·ai应用开发·多模态rag
ai大模型-探索者21 天前
BGE-M3三种向量在Milvus怎么存(稠密、稀疏、多向量)
milvus·稀疏·多向量·稠密
唠点键盘之外的23 天前
29 切片的权限隔离(实战篇):Milvus + Spring AI,从 0 搭一条带权限过滤的检索链路
人工智能·spring·milvus
ai大模型-探索者23 天前
Milvus向量数据库(介绍与使用)
数据库·milvus