本文主要是介绍linux安装milvus数据库lite版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://milvus.io/docs/milvus_lite.md
参考上述教程,直接安装该包即可。标准版和分布式版要运行docker。
pip install -U pymilvus
下面是官方的demo,一起看看
from pymilvus import MilvusClient # 导入库,客户端
import numpy as np # # 创建客户端,并连接到一个名为 milvus_demo.db 的本地数据库。这个文件将用于存储 Milvus 数据库的索引和数据
client = MilvusClient("/root/RAG/milvus/milvus_demo.db")
client.create_collection(collection_name="demo_collection",dimension=4 # The vectors we will use in this demo has 384 dimensions
) # 创建集合,设置名称和向量维度数# 待加入的文本,三条。
docs = ["Artificial intelligence was founded as an academic discipline in 1956.","Alan Turing was the first person to conduct substantial research in AI.","Born in Maida Vale, London, Turing was raised in southern England.",
]
# 为这些文本构建虚拟向量
vectors = [[ np.random.uniform(-1, 1) for _ in range(4) ] for _ in range(len(docs)) ]
print(vectors)# 构建json字典,这是真正传入数据库的东西
data = [ {"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"} for i in range(len(vectors)) ]
# insert方法插入数据
res = client.insert(collection_name="demo_collection",data=data
)
print(data)# 检索数据,传入Query向量,结合布尔检索
res = client.search(collection_name="demo_collection",data=[vectors[0]],filter="subject == 'history'",limit=2,output_fields=["text", "subject"],
)
print(res)# 查询数据,纯布尔检索
res = client.query(collection_name="demo_collection",filter="subject == 'history'",output_fields=["text", "subject"],
)
print(res)# 根据布尔条件删除记录
res = client.delete(collection_name="demo_collection",filter="subject == 'history'",
)
print(res)
输出:
vector:
[[-0.18993033343249377, -0.853204716673974, -0.6934232192055412, -0.7363553533144986], [0.6506801349937705, -0.5114297246706443, -0.6227428721418204, -0.4484672597247552], [0.3325751425210133, -0.5403168744637166, 0.6497994854622242, -0.7438264838676247]]
data:
[{'id': 0, 'vector': [-0.18993033343249377, -0.853204716673974, -0.6934232192055412, -0.7363553533144986], 'text': 'Artificial intelligence was founded as an academic discipline in 1956.', 'subject': 'history'}, {'id': 1, 'vector': [0.6506801349937705, -0.5114297246706443, -0.6227428721418204, -0.4484672597247552], 'text': 'Alan Turing was the first person to conduct substantial research in AI.', 'subject': 'history'}, {'id': 2, 'vector': [0.3325751425210133, -0.5403168744637166, 0.6497994854622242, -0.7438264838676247], 'text': 'Born in Maida Vale, London, Turing was raised in southern England.', 'subject': 'history'}]
search的res:
data: ["[{'id': 0, 'distance': 1.0, 'entity': {'text': 'Artificial intelligence was founded as an academic discipline in 1956.', 'subject': 'history'}}, {'id': 1, 'distance': 0.7123634815216064, 'entity': {'text': 'Alan Turing was the first person to conduct substantial research in AI.', 'subject': 'history'}}]"]
query的res:
data: ["{'id': 0, 'text': 'Artificial intelligence was founded as an academic discipline in 1956.', 'subject': 'history'}", "{'id': 1, 'text': 'Alan Turing was the first person to conduct substantial research in AI.', 'subject': 'history'}", "{'id': 2, 'text': 'Born in Maida Vale, London, Turing was raised in southern England.', 'subject': 'history'}"]
delete的res:
[0, 1, 2]
在创建集合并插入数据时,Milvus 会默认使用 FLAT(扁平)索引。
可以在插入数据之后随时创建或修改索引。
这篇关于linux安装milvus数据库lite版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!