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 MethodDirect link to 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 ExamplesDirect link to 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" },
});
MethodsDirect link to Methods
createIndex()Direct link to createIndex()
tableName:
indexName:
dimension:
metric?:
indexConfig?:
LanceIndexConfigDirect link to LanceIndexConfig
type:
ivfflat:
hnsw:
numPartitions?:
numSubVectors?:
hnsw?:
m?:
efConstruction?:
createTable()Direct link to createTable()
tableName:
data:
options?:
upsert()Direct link to upsert()
tableName:
vectors:
metadata?:
ids?:
query()Direct link to query()
tableName:
queryVector:
topK?:
filter?:
includeVector?:
columns?:
includeAllColumns?:
listTables()Direct link to listTables()
Returns an array of table names as strings.
const tables = await vectorStore.listTables();
// ['my_vectors', 'embeddings', 'documents']
getTableSchema()Direct link to getTableSchema()
tableName:
Returns the schema of the specified table.
deleteTable()Direct link to deleteTable()
tableName:
deleteAllTables()Direct link to deleteAllTables()
Deletes all tables in the database.
listIndexes()Direct link to listIndexes()
Returns an array of index names as strings.
describeIndex()Direct link to 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()Direct link to deleteIndex()
indexName:
updateVector()Direct link to updateVector()
indexName:
id:
update:
vector?:
metadata?:
deleteVector()Direct link to deleteVector()
indexName:
id:
close()Direct link to close()
Closes the database connection.
Response TypesDirect link to 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 HandlingDirect link to 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 PracticesDirect link to 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