Lance Vector Store
The LanceVectorStore class provides vector search using LanceDB, an embedded vector database built on the Lance columnar format. It offers efficient storage and fast similarity search for both local development and production deployments.
Factory Method
The LanceVectorStore uses a factory pattern for creation. You should use the static create() method rather than the constructor directly.
uri:
options?:
Constructor Examples
You can create a LanceVectorStore instance using the static create method:
import { LanceVectorStore } from "@mastra/lance";
// Connect to a local database
const vectorStore = await LanceVectorStore.create("/path/to/db");
// Connect to a LanceDB cloud database
const cloudStore = await LanceVectorStore.create("db://host:port");
// Connect to a cloud database with options
const s3Store = await LanceVectorStore.create("s3://bucket/db", {
storageOptions: { timeout: "60s" },
});
Methods
createIndex()
tableName:
indexName:
dimension:
metric?:
indexConfig?:
LanceIndexConfig
type:
ivfflat:
hnsw:
numPartitions?:
numSubVectors?:
hnsw?:
m?:
efConstruction?:
createTable()
tableName:
data:
options?:
upsert()
tableName:
vectors:
metadata?:
ids?:
query()
tableName:
queryVector:
topK?:
filter?:
includeVector?:
columns?:
includeAllColumns?:
listTables()
Returns an array of table names as strings.
const tables = await vectorStore.listTables();
// ['my_vectors', 'embeddings', 'documents']
getTableSchema()
tableName:
Returns the schema of the specified table.
deleteTable()
tableName:
deleteAllTables()
Deletes all tables in the database.
listIndexes()
Returns an array of index names as strings.
describeIndex()
indexName:
Returns information about the index:
interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
type: "ivfflat" | "hnsw";
config: {
m?: number;
efConstruction?: number;
numPartitions?: number;
numSubVectors?: number;
};
}
deleteIndex()
indexName:
updateVector()
indexName:
id:
update:
vector?:
metadata?:
deleteVector()
indexName:
id:
close()
Closes the database connection.
Response Types
Query results are returned in this format:
interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
document?: string; // Document text if available
}
Error Handling
The store throws typed errors that can be caught:
try {
await store.query({
tableName: "my_vectors",
queryVector: queryVector,
});
} catch (error) {
if (error instanceof Error) {
console.log(error.message);
}
}
Best Practices
- Use the appropriate index type for your use case:
- HNSW for better recall and performance when memory isn't constrained
- IVF for better memory efficiency with large datasets
- For optimal performance with large datasets, consider adjusting
numPartitionsandnumSubVectorsvalues - Use
close()method to properly close connections when done with the database - Store metadata with a consistent schema to simplify filtering operations