Azure Cosmos DB for NoSQL integration
Azure Cosmos DB for NoSQL integration for Haystack. It provides a document store and a vector-search retriever backed by the native Cosmos DB for NoSQL vector search capabilities.
Table of Contents
Installation
pip install haystack-azure-cosmosdb
Integrations
| Integration | Class | Description |
|---|---|---|
| Document Store | AzureCosmosDBNoSqlDocumentStore |
Stores Haystack Documents in a Cosmos DB for NoSQL container with vector indexing, full-text indexing, and metadata filtering. |
| Embedding Retriever | AzureCosmosDBNoSqlEmbeddingRetriever |
Retrieves documents by vector similarity using the native VectorDistance function. |
| Full-Text Retriever | AzureCosmosDBNoSqlFullTextRetriever |
Retrieves documents by BM25 relevance using FullTextScore with ORDER BY RANK. |
| Hybrid Retriever | AzureCosmosDBNoSqlHybridRetriever |
Fuses vector and full-text relevance with Reciprocal Rank Fusion (RRF), with optional weights. |
A single AzureCosmosDBNoSqlDocumentStore powers all three retrieval modes. Full-text and hybrid
retrieval require full_text_search_enabled=True and the
full-text search feature
enabled on your Cosmos DB account.
Usage
Create a document store and write documents
from azure.cosmos import PartitionKey
from haystack import Document
from haystack_azure_cosmosdb import AzureCosmosDBNoSqlDocumentStore
vector_embedding_policy = {
"vectorEmbeddings": [
{"path": "/embedding", "dataType": "float32", "dimensions": 768, "distanceFunction": "cosine"}
]
}
indexing_policy = {
"indexingMode": "consistent",
"includedPaths": [{"path": "/*"}],
"excludedPaths": [{"path": '/"_etag"/?'}],
"vectorIndexes": [{"path": "/embedding", "type": "quantizedFlat"}],
}
# Reads the connection string from AZURE_COSMOS_NOSQL_CONNECTION_STRING by default.
store = AzureCosmosDBNoSqlDocumentStore.from_connection_string(
database_name="haystack_db",
container_name="haystack_container",
vector_embedding_policy=vector_embedding_policy,
indexing_policy=indexing_policy,
cosmos_container_properties={"partition_key": PartitionKey(path="/id")},
# Set to True to also enable full-text and hybrid retrieval.
full_text_search_enabled=True,
)
store.write_documents([Document(content="Azure Cosmos DB is a globally distributed database.")])
print(store.count_documents())
When full_text_search_enabled=True, the store creates the container with a full-text policy on the
content field and adds a matching fullTextIndexes entry to the indexing policy (unless you supply
your own full_text_policy).
Vector retrieval in a pipeline
from haystack import Pipeline
from haystack.components.embedders import SentenceTransformersTextEmbedder
from haystack_azure_cosmosdb import AzureCosmosDBNoSqlEmbeddingRetriever
pipeline = Pipeline()
pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder())
pipeline.add_component("retriever", AzureCosmosDBNoSqlEmbeddingRetriever(document_store=store))
pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
result = pipeline.run({"text_embedder": {"text": "What is Cosmos DB?"}})
print(result["retriever"]["documents"])
Full-text retrieval
from haystack_azure_cosmosdb import AzureCosmosDBNoSqlFullTextRetriever
retriever = AzureCosmosDBNoSqlFullTextRetriever(document_store=store, top_k=5)
result = retriever.run(query_text="globally distributed database")
print(result["documents"])
Hybrid retrieval (vector + full-text)
from haystack_azure_cosmosdb import AzureCosmosDBNoSqlHybridRetriever
retriever = AzureCosmosDBNoSqlHybridRetriever(document_store=store, top_k=5)
result = retriever.run(
query_embedding=[0.1, 0.2, ...],
query_text="globally distributed database",
# Optional [full_text_weight, vector_weight] for weighted RRF:
weights=[2.0, 1.0],
)
print(result["documents"])
Hybrid results are ordered by the fused RRF rank; each document's score carries its raw
VectorDistance similarity for reference.
Metadata filtering
The document store supports the standard
Haystack filter syntax, which is
translated to parameterized Azure Cosmos DB for NoSQL WHERE clauses:
filters = {
"operator": "AND",
"conditions": [
{"field": "meta.chapter", "operator": "==", "value": "intro"},
{"field": "meta.number", "operator": ">=", "value": 100},
],
}
store.filter_documents(filters=filters)
Supported comparison operators: ==, !=, >, >=, <, <=, in, not in.
Supported logical operators: AND, OR, NOT.
Authentication
The document store supports several authentication methods. Each one reads sensible defaults from environment variables, so you can also configure it entirely through the environment:
| Variable | Used by | Purpose |
|---|---|---|
AZURE_COSMOS_NOSQL_CONNECTION_STRING |
from_connection_string |
Full account connection string |
AZURE_COSMOS_NOSQL_ENDPOINT |
from_uri_and_key, from_aad_token |
Account endpoint URI |
AZURE_COSMOS_NOSQL_KEY |
from_uri_and_key |
Account key |
from azure.cosmos import PartitionKey
from haystack.utils import Secret
from haystack_azure_cosmosdb import AzureCosmosDBNoSqlDocumentStore
common = {
"database_name": "haystack_db",
"container_name": "haystack_container",
"vector_embedding_policy": vector_embedding_policy,
"indexing_policy": indexing_policy,
"cosmos_container_properties": {"partition_key": PartitionKey(path="/id")},
}
# 1. Connection string (defaults to env var AZURE_COSMOS_NOSQL_CONNECTION_STRING)
store = AzureCosmosDBNoSqlDocumentStore.from_connection_string(**common)
# 2. Account URI + key (default to env vars AZURE_COSMOS_NOSQL_ENDPOINT and AZURE_COSMOS_NOSQL_KEY);
# you can also pass them explicitly:
store = AzureCosmosDBNoSqlDocumentStore.from_uri_and_key(
uri="https://<account>.documents.azure.com:443/",
key=Secret.from_env_var("AZURE_COSMOS_NOSQL_KEY"),
**common,
)
# 3. Microsoft Entra ID (AAD / Managed Identity) - endpoint defaults to AZURE_COSMOS_NOSQL_ENDPOINT,
# credential defaults to DefaultAzureCredential
store = AzureCosmosDBNoSqlDocumentStore.from_aad_token(
uri="https://<account>.documents.azure.com:443/", **common
)
Examples
See examples/retrieval.py for a complete, runnable script that indexes
documents and queries them with all three retrieval modes — vector, full-text, and hybrid — from a
single document store.
Development
This project uses Hatch for building and a Makefile for common tasks.
make install # install the package with test and lint extras
make test # run unit tests
make lint # run ruff and mypy
make format # auto-format with black and ruff
Integration tests require a live Azure Cosmos DB for NoSQL account. Export the connection string and run them explicitly:
export AZURE_COSMOS_NOSQL_CONNECTION_STRING="AccountEndpoint=...;AccountKey=...;"
make integration-test
If the connection string is not set, all integration tests are skipped.
License
haystack-azure-cosmosdb is distributed under the terms of the
MIT license.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file haystack_azure_cosmosdb-0.1.0.tar.gz.
File metadata
- Download URL: haystack_azure_cosmosdb-0.1.0.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cab84dd304aeeb0e512482f279dee3738518a44c5643e14d93e6681be463476
|
|
| MD5 |
b51812d82b367b61cd530f923776f731
|
|
| BLAKE2b-256 |
eb8678ca18433bfdce45d08b261ab3daa255547d323bff709c4d857c8356d07a
|
Provenance
The following attestation bundles were made for haystack_azure_cosmosdb-0.1.0.tar.gz:
Publisher:
release.yml on AzureCosmosDB/haystack-azure-cosmosdb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
haystack_azure_cosmosdb-0.1.0.tar.gz -
Subject digest:
8cab84dd304aeeb0e512482f279dee3738518a44c5643e14d93e6681be463476 - Sigstore transparency entry: 2365691964
- Sigstore integration time:
-
Permalink:
AzureCosmosDB/haystack-azure-cosmosdb@cc35da9ed23db3f5105e4aa1f0415dacc984b3ba -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AzureCosmosDB
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@cc35da9ed23db3f5105e4aa1f0415dacc984b3ba -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file haystack_azure_cosmosdb-0.1.0-py3-none-any.whl.
File metadata
- Download URL: haystack_azure_cosmosdb-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb24bf4c005adffa3c6c7a615e72cb730a0f2f338b65af67f11ab96228d5a073
|
|
| MD5 |
d789e31f166662c86027d3cb0f3ffbdc
|
|
| BLAKE2b-256 |
e4b9bb891ba6691b0fa62f78ad8a229bce9b310af5f3ce34153d06a8a3f37975
|
Provenance
The following attestation bundles were made for haystack_azure_cosmosdb-0.1.0-py3-none-any.whl:
Publisher:
release.yml on AzureCosmosDB/haystack-azure-cosmosdb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
haystack_azure_cosmosdb-0.1.0-py3-none-any.whl -
Subject digest:
cb24bf4c005adffa3c6c7a615e72cb730a0f2f338b65af67f11ab96228d5a073 - Sigstore transparency entry: 2365691982
- Sigstore integration time:
-
Permalink:
AzureCosmosDB/haystack-azure-cosmosdb@cc35da9ed23db3f5105e4aa1f0415dacc984b3ba -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AzureCosmosDB
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@cc35da9ed23db3f5105e4aa1f0415dacc984b3ba -
Trigger Event:
workflow_dispatch
-
Statement type: