Skip to main content

Using the Momento Vector Index API

Momento Vector Index (MVI) is a scalable, developer-friendly vector index service designed for real-time storage and retrieval of vector data for use in AI-powered applications.

Vector Index Client

To interact with Momento Vector Indexes, you must use a VectorIndexClient.

new PreviewVectorIndexClient({
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
configuration: VectorIndexConfigurations.Laptop.latest(),
});

Vector Index methods

Create Index

Creates a vector index.

NameTypeDescription
indexNameStringName of the vector index.
numDimensionsStringNumber of dimensions per vector.
similarityMetricStringMetric used to quantify the distance between vectors. Can be cosine similarity, inner product, or euclidean similarity. Defaults to cosine similarity.
Method response object
  • Success
  • AlreadyExists
  • Error

See response objects for specific information.

const result = await vectorClient.createIndex('test-index', 2);
if (result instanceof CreateVectorIndex.Success) {
console.log("Index 'test-index' created");
} else if (result instanceof CreateVectorIndex.AlreadyExists) {
console.log("Index 'test-index' already exists");
} else if (result instanceof CreateVectorIndex.Error) {
throw new Error(
`An error occurred while attempting to create index 'test-index': ${result.errorCode()}: ${result.toString()}`
);
}

Delete Index

Deletes a vector index.

NameTypeDescription
indexNameStringName of the vector index.
Method response object
  • Success
  • Error

See response objects for specific information.

const result = await vectorClient.deleteIndex('test-index');
if (result instanceof DeleteVectorIndex.Success) {
console.log("Index 'test-index' deleted");
} else if (result instanceof DeleteVectorIndex.Error) {
throw new Error(
`An error occurred while attempting to delete index 'test-index': ${result.errorCode()}: ${result.toString()}`
);
}

List Indexes

Lists all vector indexes.

Method response object
  • Success
    • getIndexes(): VectorIndexInfo[]
      • VectorIndexInfo:
        • name: String
        • numDimensions: number
        • similarityMetric: String
  • Error

See response objects for specific information.

const result = await vectorClient.listIndexes();
if (result instanceof ListVectorIndexes.Success) {
console.log(
`Indexes:\n${result
.getIndexes()
.map(index => index.toString())
.join('\n')}\n\n`
);
} else if (result instanceof ListVectorIndexes.Error) {
throw new Error(`An error occurred while attempting to list caches: ${result.errorCode()}: ${result.toString()}`);
}

Upsert Item Batch

Upserts a batch of items into a vector index.

NameTypeDescription
indexNameStringName of the vector index.
itemsVectorIndexItemItems to upsert into the index.
Method response object
  • Success
  • Error

See response objects for specific information.

const result = await vectorClient.upsertItemBatch('test-index', [
{
id: 'example_item_1',
vector: [1.0, 2.0],
metadata: {key1: 'value1'},
},
{
id: 'example_item_2',
vector: [3.0, 4.0],
metadata: {key2: 'value2'},
},
]);
if (result instanceof VectorUpsertItemBatch.Success) {
console.log('Successfully added items');
} else if (result instanceof VectorUpsertItemBatch.Error) {
throw new Error(`An error occurred while adding items to index: ${result.errorCode()}: ${result.toString()}`);
}

Searches for items with vectors most similar to a query vector.

NameTypeDescription
indexNameStringName of the vector index.
queryVectornumber[]Vector to search for.
topKnumberNumber of results to return. Defaults to 10.
metadataFieldsString[] or ALL_VECTOR_METADATAA list of metadata fields to return with each result, or a value indicating all metadata should be returned. If not provided, no metadata is returned. Defaults to None.
Method response object
  • Success
    • hits(): SearchHit[]
      • SearchHit:
        • id: string
        • score: number
        • metadata: Map<string, string>
  • Error

See response objects for specific information.

const result = await vectorClient.search('test-index', [1.0, 2.0], {topK: 3, metadataFields: ALL_VECTOR_METADATA});
if (result instanceof VectorSearch.Success) {
console.log(`Found ${result.hits().length} matches`);
} else if (result instanceof VectorSearch.Error) {
throw new Error(`An error occurred searching index test-index: ${result.errorCode()}: ${result.toString()}`);
}

Search And Fetch Vectors

Searches for items with vectors most similar to a query vector. Also returns corresponding vectors to search hits.

NameTypeDescription
indexNameStringName of the vector index.
queryVectornumber[]Vector to search for.
topKnumberNumber of results to return. Defaults to 10.
metadataFieldsString[] or ALL_VECTOR_METADATAA list of metadata fields to return with each result, or a value indicating all metadata should be returned. If not provided, no metadata is returned. Defaults to None.
Method response object
  • Success
    • hits(): SearchAndFetchVectorsHit[]
      • SearchAndFetchVectorsHit:
        • id: string
        • score: number
        • metadata: Map<string, string>
        • vector: number[]
  • Error

See response objects for specific information.

const result = await vectorClient.searchAndFetchVectors('test-index', [1.0, 2.0], {
topK: 3,
metadataFields: ALL_VECTOR_METADATA,
});
if (result instanceof VectorSearchAndFetchVectors.Success) {
console.log(`Found ${result.hits().length} matches`);
} else if (result instanceof VectorSearchAndFetchVectors.Error) {
throw new Error(`An error occurred searching index test-index: ${result.errorCode()}: ${result.toString()}`);
}

Delete Item Batch

Deletes a batch of items from a vector index.

NameTypeDescription
indexNameStringName of the vector index.
itemsString[]IDs of the items to be deleted.
Method response object
  • Success
  • Error

See response objects for specific information.

const result = await vectorClient.deleteItemBatch('test-index', ['example_item_1', 'example_item_2']);
if (result instanceof VectorUpsertItemBatch.Success) {
console.log('Successfully deleted items');
} else if (result instanceof VectorUpsertItemBatch.Error) {
throw new Error(`An error occurred while deleting items: ${result.errorCode()}: ${result.toString()}`);
}