Milvus Lite

2.5.1 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Milvus Lite using `MilvusClient` with a local database file, define a collection schema, insert data (vectors and scalar fields), perform a vector similarity search, and query data. The `uri` parameter for `MilvusClient` points to a local file, which acts as the Milvus Lite database.

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!")

view raw JSON →