!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
        *   [Memory](/v0.1/docs/integrations/vectorstores/memory/)
        *   [AnalyticDB](/v0.1/docs/integrations/vectorstores/analyticdb/)
        *   [Astra DB](/v0.1/docs/integrations/vectorstores/astradb/)
        *   [Azure AI Search](/v0.1/docs/integrations/vectorstores/azure_aisearch/)
        *   [Azure Cosmos DB](/v0.1/docs/integrations/vectorstores/azure_cosmosdb/)
        *   [Cassandra](/v0.1/docs/integrations/vectorstores/cassandra/)
        *   [Chroma](/v0.1/docs/integrations/vectorstores/chroma/)
        *   [ClickHouse](/v0.1/docs/integrations/vectorstores/clickhouse/)
        *   [CloseVector](/v0.1/docs/integrations/vectorstores/closevector/)
        *   [Cloudflare Vectorize](/v0.1/docs/integrations/vectorstores/cloudflare_vectorize/)
        *   [Convex](/v0.1/docs/integrations/vectorstores/convex/)
        *   [Couchbase](/v0.1/docs/integrations/vectorstores/couchbase/)
        *   [Elasticsearch](/v0.1/docs/integrations/vectorstores/elasticsearch/)
        *   [Faiss](/v0.1/docs/integrations/vectorstores/faiss/)
        *   [Google Vertex AI Matching Engine](/v0.1/docs/integrations/vectorstores/googlevertexai/)
        *   [SAP HANA Cloud Vector Engine](/v0.1/docs/integrations/vectorstores/hanavector/)
        *   [HNSWLib](/v0.1/docs/integrations/vectorstores/hnswlib/)
        *   [LanceDB](/v0.1/docs/integrations/vectorstores/lancedb/)
        *   [Milvus](/v0.1/docs/integrations/vectorstores/milvus/)
        *   [Momento Vector Index (MVI)](/v0.1/docs/integrations/vectorstores/momento_vector_index/)
        *   [MongoDB Atlas](/v0.1/docs/integrations/vectorstores/mongodb_atlas/)
        *   [MyScale](/v0.1/docs/integrations/vectorstores/myscale/)
        *   [Neo4j Vector Index](/v0.1/docs/integrations/vectorstores/neo4jvector/)
        *   [Neon Postgres](/v0.1/docs/integrations/vectorstores/neon/)
        *   [OpenSearch](/v0.1/docs/integrations/vectorstores/opensearch/)
        *   [PGVector](/v0.1/docs/integrations/vectorstores/pgvector/)
        *   [Pinecone](/v0.1/docs/integrations/vectorstores/pinecone/)
        *   [Prisma](/v0.1/docs/integrations/vectorstores/prisma/)
        *   [Qdrant](/v0.1/docs/integrations/vectorstores/qdrant/)
        *   [Redis](/v0.1/docs/integrations/vectorstores/redis/)
        *   [Rockset](/v0.1/docs/integrations/vectorstores/rockset/)
        *   [SingleStore](/v0.1/docs/integrations/vectorstores/singlestore/)
        *   [Supabase](/v0.1/docs/integrations/vectorstores/supabase/)
        *   [Tigris](/v0.1/docs/integrations/vectorstores/tigris/)
        *   [Turbopuffer](/v0.1/docs/integrations/vectorstores/turbopuffer/)
        *   [TypeORM](/v0.1/docs/integrations/vectorstores/typeorm/)
        *   [Typesense](/v0.1/docs/integrations/vectorstores/typesense/)
        *   [Upstash Vector](/v0.1/docs/integrations/vectorstores/upstash/)
        *   [USearch](/v0.1/docs/integrations/vectorstores/usearch/)
        *   [Vectara](/v0.1/docs/integrations/vectorstores/vectara/)
        *   [Vercel Postgres](/v0.1/docs/integrations/vectorstores/vercel_postgres/)
        *   [Voy](/v0.1/docs/integrations/vectorstores/voy/)
        *   [Weaviate](/v0.1/docs/integrations/vectorstores/weaviate/)
        *   [Xata](/v0.1/docs/integrations/vectorstores/xata/)
        *   [Zep](/v0.1/docs/integrations/vectorstores/zep/)
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Vector stores](/v0.1/docs/integrations/vectorstores/)
*   Google Vertex AI Matching Engine

On this page

Google Vertex AI Matching Engine
================================

Compatibility

Only available on Node.js.

The Google Vertex AI Matching Engine "provides the industry's leading high-scale low latency vector database. These vector databases are commonly referred to as vector similarity-matching or an approximate nearest neighbor (ANN) service."

Setup[​](#setup "Direct link to Setup")
---------------------------------------

caution

This module expects an endpoint and deployed index already created as the creation time takes close to one hour. To learn more, see the LangChain python documentation [Create Index and deploy it to an Endpoint](https://python.langchain.com/docs/integrations/vectorstores/matchingengine#create-index-and-deploy-it-to-an-endpoint).

Before running this code, you should make sure the Vertex AI API is enabled for the relevant project in your Google Cloud dashboard and that you've authenticated to Google Cloud using one of these methods:

*   You are logged into an account (using `gcloud auth application-default login`) permitted to that project.
*   You are running on a machine using a service account that is permitted to the project.
*   You have downloaded the credentials for a service account that is permitted to the project and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of this file.

Install the authentication library with:

*   npm
*   Yarn
*   pnpm

    npm install google-auth-library

    yarn add google-auth-library

    pnpm add google-auth-library

The Matching Engine does not store the actual document contents, only embeddings. Therefore, you'll need a docstore. The below example uses Google Cloud Storage, which requires the following:

*   npm
*   Yarn
*   pnpm

    npm install @google-cloud/storage

    yarn add @google-cloud/storage

    pnpm add @google-cloud/storage

Usage[​](#usage "Direct link to Usage")
---------------------------------------

### Initializing the engine[​](#initializing-the-engine "Direct link to Initializing the engine")

When creating the `MatchingEngine` object, you'll need some information about the matching engine configuration. You can get this information from the Cloud Console for Matching Engine:

*   The id for the Index
*   The id for the Index Endpoint

You will also need a document store. While an `InMemoryDocstore` is ok for initial testing, you will want to use something like a [GoogleCloudStorageDocstore](https://api.js.langchain.com/classes/langchain_stores_doc_gcs.GoogleCloudStorageDocstore.html) to store it more permanently.

    import { MatchingEngine } from "langchain/vectorstores/googlevertexai";import { Document } from "langchain/document";import { SyntheticEmbeddings } from "langchain/embeddings/fake";import { GoogleCloudStorageDocstore } from "langchain/stores/doc/gcs";const embeddings = new SyntheticEmbeddings({  vectorSize: Number.parseInt(    process.env.SYNTHETIC_EMBEDDINGS_VECTOR_SIZE ?? "768",    10  ),});const store = new GoogleCloudStorageDocstore({  bucket: process.env.GOOGLE_CLOUD_STORAGE_BUCKET!,});const config = {  index: process.env.GOOGLE_VERTEXAI_MATCHINGENGINE_INDEX!,  indexEndpoint: process.env.GOOGLE_VERTEXAI_MATCHINGENGINE_INDEXENDPOINT!,  apiVersion: "v1beta1",  docstore: store,};const engine = new MatchingEngine(embeddings, config);

### Adding documents[​](#adding-documents "Direct link to Adding documents")

    const doc = new Document({ pageContent: "this" });await engine.addDocuments([doc]);

Any metadata in a document is converted into Matching Engine "allow list" values that can be used to filter during a query.

    const documents = [  new Document({    pageContent: "this apple",    metadata: {      color: "red",      category: "edible",    },  }),  new Document({    pageContent: "this blueberry",    metadata: {      color: "blue",      category: "edible",    },  }),  new Document({    pageContent: "this firetruck",    metadata: {      color: "red",      category: "machine",    },  }),];// Add all our documentsawait engine.addDocuments(documents);

The documents are assumed to have an "id" parameter available as well. If this is not set, then an ID will be assigned and returned as part of the Document.

### Querying documents[​](#querying-documents "Direct link to Querying documents")

Doing a straightforward k-nearest-neighbor search which returns all results is done using any of the standard methods:

    const results = await engine.similaritySearch("this");

### Querying documents with a filter / restriction[​](#querying-documents-with-a-filter--restriction "Direct link to Querying documents with a filter / restriction")

We can limit what documents are returned based on the metadata that was set for the document. So if we just wanted to limit the results to those with a red color, we can do:

    import { Restriction } from `langchain/vectorstores/googlevertexai`;const redFilter: Restriction[] = [  {    namespace: "color",    allowList: ["red"],  },];const redResults = await engine.similaritySearch("this", 4, redFilter);

If we wanted to do something more complicated, like things that are red, but not edible:

    const filter: Restriction[] = [  {    namespace: "color",    allowList: ["red"],  },  {    namespace: "category",    denyList: ["edible"],  },];const results = await engine.similaritySearch("this", 4, filter);

### Deleting documents[​](#deleting-documents "Direct link to Deleting documents")

Deleting documents are done using ID.

    import { IdDocument } from `langchain/vectorstores/googlevertexai`;const oldResults: IdDocument[] = await engine.similaritySearch("this", 10);const oldIds = oldResults.map( doc => doc.id! );await engine.delete({ids: oldIds});

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Faiss

](/v0.1/docs/integrations/vectorstores/faiss/)[

Next

SAP HANA Cloud Vector Engine

](/v0.1/docs/integrations/vectorstores/hanavector/)

*   [Setup](#setup)
*   [Usage](#usage)
    *   [Initializing the engine](#initializing-the-engine)
    *   [Adding documents](#adding-documents)
    *   [Querying documents](#querying-documents)
    *   [Querying documents with a filter / restriction](#querying-documents-with-a-filter--restriction)
    *   [Deleting documents](#deleting-documents)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.