Milvus Lite
Milvus Lite is a lightweight, embedded version of Milvus, a high-performance vector database, designed for rapid prototyping, local development, and edge devices. It provides core vector search functionalities and shares the same API as Milvus Standalone and Distributed deployments, ensuring a consistent development experience across various scales. Data is persisted locally in an SQLite file. The library is actively maintained as part of the `pymilvus` ecosystem, with the current version being 2.5.1.
Warnings
- gotcha Milvus Lite is designed for small-scale prototyping (typically less than a million vectors) or edge devices. It is not recommended for large-scale production deployments where Milvus Standalone, Distributed, or Zilliz Cloud should be used instead.
- breaking Prior to Milvus Lite version 2.4.11, only the FLAT index type was supported, regardless of any other index type specified during collection creation. For versions 2.4.11 and later, both FLAT and IVF_FLAT are supported, with automatic internal switching (FLAT for <100,000 vectors, IVF_FLAT for >=100,000 vectors).
- gotcha Milvus Lite does not support advanced Milvus features such as partitions, users/roles/RBAC (Role-Based Access Control), or aliases. Attempting to use these features will result in errors or unexpected behavior.
- gotcha While Milvus Lite uses the same Python client API (`pymilvus`) as other Milvus deployments, the `uri` parameter for `MilvusClient` differs. For Milvus Lite, it's a local file path (e.g., './milvus_demo.db'). For other deployments, it's a network endpoint (e.g., 'http://localhost:19530' or a Zilliz Cloud endpoint).
Install
-
pip install -U pymilvus[milvus-lite] -
pip install -U milvus-lite
Imports
- MilvusClient
from pymilvus import MilvusClient
- default_server
from milvus import default_server
Quickstart
from pymilvus import MilvusClient, DataType
import os
# Ensure a clean slate for demonstration
if os.path.exists("milvus_demo.db"):
os.remove("milvus_demo.db")
# 1. Set up a Milvus client with a local file for persistence
client = MilvusClient(uri="./milvus_demo.db")
# 2. Create schema
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=5)
schema.add_field(field_name="text", datatype=DataType.VARCHAR, max_length=256)
# 3. Create a collection
collection_name = "demo_collection"
client.create_collection(
collection_name=collection_name,
schema=schema,
# You can specify an index here, Milvus Lite will optimize internally
# index_params=MilvusClient.prepare_index_params(metric_type="L2")
)
# 4. Insert data
data = [
{"id": 1, "vector": [0.1, 0.2, 0.3, 0.4, 0.5], "text": "The quick brown fox"},
{"id": 2, "vector": [0.5, 0.4, 0.3, 0.2, 0.1], "text": "Jumps over the lazy dog"},
{"id": 3, "vector": [0.8, 0.7, 0.6, 0.5, 0.4], "text": "A fast animal"},
]
client.insert(collection_name=collection_name, data=data)
client.flush(collection_name=collection_name)
# 5. Search for similar vectors
search_vectors = [[0.15, 0.25, 0.35, 0.45, 0.55]]
res = client.search(
collection_name=collection_name,
data=search_vectors,
limit=2,
output_fields=["text"],
)
print("Search Results:", res)
# 6. Query data by ID
query_res = client.query(collection_name=collection_name, filter="id in", output_fields=["text"])
print("Query Results:", query_res)
# Clean up (optional for next run)
client.drop_collection(collection_name=collection_name)
client.close() # Important to close client to ensure data is written to disk
print("Milvus Lite demo finished successfully!")