Couchbase vector store
The CouchbaseVector class provides vector search using Couchbase Vector Search. It enables efficient similarity search and metadata filtering within your Couchbase collections.
RequirementsRequirements的直接链接
- Couchbase Server 7.6.4+ or a compatible Capella cluster
- Search Service enabled on your Couchbase deployment
InstallationInstallation的直接链接
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/couchbase@latest
pnpm add @mastra/couchbase@latest
yarn add @mastra/couchbase@latest
bun add @mastra/couchbase@latest
Usage exampleUsage example的直接链接
import { CouchbaseVector } from '@mastra/couchbase'
const store = new CouchbaseVector({
id: 'couchbase-vector',
connectionString: process.env.COUCHBASE_CONNECTION_STRING,
username: process.env.COUCHBASE_USERNAME,
password: process.env.COUCHBASE_PASSWORD,
bucketName: process.env.COUCHBASE_BUCKET,
scopeName: process.env.COUCHBASE_SCOPE,
collectionName: process.env.COUCHBASE_COLLECTION,
})
Constructor optionsConstructor options的直接链接
id:
connectionString:
username:
password:
bucketName:
scopeName:
collectionName:
MethodsMethods的直接链接
createIndex()createindex的直接链接
Creates a new vector index in Couchbase.
Index creation is asynchronous. After calling createIndex, allow time (typically 1 to 5 seconds for small datasets, longer for large ones) before querying. For production, implement polling to check index status rather than using fixed delays.
indexName:
dimension:
metric?:
upsert()upsert的直接链接
Adds or updates vectors and their metadata in the collection.
You can upsert data before or after creating the index. The upsert method doesn't require the index to exist. Couchbase allows multiple Search indexes over the same collection.
indexName:
vectors:
metadata?:
ids?:
query()query的直接链接
Searches for similar vectors.
The filter and includeVector parameters aren't currently supported. Filtering must be performed client-side after retrieving results or through the Couchbase SDK's Search capabilities. Retrieve the vector embedding by fetching the full document by ID with the Couchbase SDK.
indexName:
queryVector:
topK?:
filter?:
includeVector?:
true throws an error.describeIndex()describeindex的直接链接
Returns information about the index.
indexName:
Returns:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()deleteindex的直接链接
Deletes the Search index. Documents stored in the collection aren't deleted.
indexName:
listIndexes()listindexes的直接链接
Lists all vector indexes in the Couchbase bucket.
Returns: Promise<string[]>
updateVector()updatevector的直接链接
Updates a specific vector entry by its ID with new vector data and/or metadata. Filter-based updates aren't yet implemented for Couchbase.
indexName:
id:
update:
deleteVector()deletevector的直接链接
Deletes a single vector by its ID from the index.
indexName:
id:
deleteVectors()deletevectors的直接链接
Deletes multiple vectors by their IDs. Filter-based deletion isn't yet implemented for Couchbase.
indexName:
ids:
disconnect()disconnect的直接链接
Closes the Couchbase client connection. Should be called when done using the store.
Response typesResponse types的直接链接
Query results are returned in this format:
interface QueryResult {
id: string
score: number
metadata: Record<string, any>
}
Error handlingError handling的直接链接
The store throws typed errors that can be caught:
try {
await store.query({
indexName: 'my_index',
queryVector: queryVector,
})
} catch (error) {
// Handle specific error cases
if (error.message.includes('Invalid index name')) {
console.error(
'Index name must start with a letter or underscore and contain only valid characters.',
)
} else if (error.message.includes('Index not found')) {
console.error('The specified index does not exist')
} else {
console.error('Vector store error:', error.message)
}
}
NotesNotes的直接链接
- Index Deletion Caveat: Deleting a Search index doesn't delete the vectors/documents in the associated Couchbase collection. Data remains unless explicitly removed.
- Required Permissions: The Couchbase user must have permissions to connect, read/write documents in the target collection (
kvrole), and manage Search Indexes (search_adminrole on the relevant bucket/scope). - Index Definition Details & Document Structure: The
createIndexmethod builds a Search Index definition for documents inscopeName.collectionName. It indexes theembeddingfield as a vector and thecontentfield as text. Each document stores its vector inembedding, with metadata kept inmetadata. Atextproperty withinmetadatais also copied to the top-levelcontentfield for text search. - Replication & Durability: Consider using Couchbase's built-in replication and persistence features for data durability. Monitor index statistics regularly to ensure efficient search.
LimitationsLimitations的直接链接
- Index creation delays may impact immediate querying after creation.
- No hard enforcement of vector dimension at ingest time (dimension mismatches will error at query time).
- Vector insertion and index updates are eventually consistent. Strong consistency isn't guaranteed immediately after writes.