Skip to main content
Azure Cosmos DB for NoSQL provides support for querying items with flexible schemas and native support for JSON. It now offers vector indexing and search. This feature is designed to handle high-dimensional vectors, enabling efficient and accurate vector search at any scale. You can now store vectors directly in the documents alongside your data. Each document in your database can contain not only traditional schema-free data, but also high-dimensional vectors as other properties of the documents.
Learn how to leverage the vector search capabilities of Azure Cosmos DB for NoSQL from this page. If you don’t have an Azure account, you can create a free account to get started.

Setup

You’ll first need to install the @langchain/azure-cosmosdb package:
npm
You’ll also need to have an Azure Cosmos DB for NoSQL instance running. You can deploy a free version on Azure Portal without any cost, following this guide. Once you have your instance running, make sure you have the connection string. You can find them in the Azure Portal, under the “Settings / Keys” section of your instance. Then you need to set the following environment variables:
.env example

Using Azure Managed identity

If you’re using Azure Managed Identity, you can configure the credentials like this:
When using Azure Managed Identity and role-based access control, you must ensure that the database and container have been created beforehand. RBAC does not provide permissions to create databases and containers. You can get more information about the permission model in the Azure Cosmos DB documentation.

Security considerations when using filters

Using filters with user-provided input can be a security risk if the data is not sanitized properly. Follow the recommendation below to prevent potential security issues.
Allowing raw user input to be concatenated into SQL-like clauses - such as WHERE ${userFilter} - introduces a critical risk of SQL injection attacks, potentially exposing unintended data or compromising your system’s integrity. To mitigate this, always use Azure Cosmos DB’s parameterized query mechanism, passing in @param placeholders, which cleanly separates the query logic from user-provided input. Here is an example of unsafe code:
If the attacker provides 123 OR 1=1, then the query becomes SELECT * FROM c WHERE c.metadata.userId = '123' OR 1=1, which forces the condition to always be true, causing it to bypass the intended filter and delete all documents. To prevent this injection risk, you define a placeholder like @userId and Cosmos DB binds the user input separately as a parameter, ensuring it is treated strictly as data and not executable query logic as shown below.
Now, if the attacker enters 123 OR 1=1, the input will be treated as a literal string value to match, and not as part of the query structure. Please refer to the official documentation on parameterized queries in Azure Cosmos DB for NoSQL for more usage examples and best practices.

Usage example

Below is an example that indexes documents from a file in Azure Cosmos DB for NoSQL, runs a vector search query, and finally uses a chain to answer a question in natural language based on the retrieved documents.

Advanced search options

All search types are accessed through the unified similaritySearch and similaritySearchWithScore methods using the searchType parameter in the filter options. Search types are available as constants from AzureCosmosDBNoSQLSearchType: Available search types:
  • AzureCosmosDBNoSQLSearchType.Vector (default): Standard vector similarity search
  • AzureCosmosDBNoSQLSearchType.VectorScoreThreshold: Vector search with minimum score filter
  • AzureCosmosDBNoSQLSearchType.FullTextSearch: Full-text search using FullTextContains (preview)
  • AzureCosmosDBNoSQLSearchType.FullTextRanking: Full-text search with BM25 ranking (preview)
  • AzureCosmosDBNoSQLSearchType.Hybrid: Hybrid vector + full-text search using RRF (preview)
  • AzureCosmosDBNoSQLSearchType.HybridScoreThreshold: Hybrid search with score threshold (preview)
You can also set a default search type when creating the store using the defaultSearchType configuration option, so you don’t have to specify it in every query:

Vector search with score threshold

Filter results based on a minimum similarity score:
MMR search balances relevance with diversity in the results:

Full-text and hybrid search (preview)

Full-text and hybrid search are preview features in Azure Cosmos DB. You need to configure your container with a full-text policy and appropriate indexing to use these features. See the Azure Cosmos DB documentation for setup instructions.
To use full-text or hybrid search, enable it when creating the store:

Full-text ranking

Hybrid search combines vector similarity with full-text search using Reciprocal Rank Fusion (RRF):

Utility methods

Delete documents

Access the underlying container