SolrDocumentStore
A Document Store for storing and retrieval from Apache Solr.
| API reference | Solr |
| GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/solr |
Apache Solr is a widely used open source search server built on Apache Lucene. Since Solr 9, it ships a DenseVectorField type and the {!knn} query parser, so a single Solr core can serve both keyword (BM25) and dense vector retrieval. For more information, see the Solr documentation.
This Document Store is a good fit if your organization already runs Solr and you want to add semantic or hybrid retrieval on top of it without introducing a separate vector database.
The Document Store requires Solr 9.6 or newer. Every operation is available both synchronously and asynchronously.
Initialization
Install and run a Solr instance. If you have Docker set up, we recommend pulling the Docker image and running it with a precreated core:
Once you have a running Solr instance, install the solr-haystack integration:
Then, initialize a SolrDocumentStore object that's connected to the Solr instance and write documents to it:
from haystack import Document
from haystack_integrations.document_stores.solr import SolrDocumentStore
document_store = SolrDocumentStore(
url="http://localhost:8983/solr",
core="haystack",
embedding_dim=768,
)
document_store.write_documents(
[Document(content="This is first"), Document(content="This is second")],
)
print(document_store.count_documents())
By default, the store manages the Solr schema itself: on first use, it creates the fields it needs and disables Solr's schemaless field guessing. Set manage_schema=False to manage the schema yourself.
A few points to keep in mind:
urlfalls back to theSOLR_URLenvironment variable, then tohttp://localhost:8983/solr. Basic authentication credentials are read from theSOLR_USERNAMEandSOLR_PASSWORDenvironment variables by default, so they never need to appear in code or serialized pipelines.- Solr fixes a vector field's dimension when the field is created, so
embedding_dimcannot be changed for an existing core. Thesimilarity_functioncan becosine(default),dot_product, oreuclidean.
Supported Retrievers
SolrBM25Retriever: A keyword-based Retriever that fetches documents matching a query from the Document Store.
SolrEmbeddingRetriever: Compares the query and document embeddings and fetches the documents most relevant to the query.
SolrHybridRetriever: A SuperComponent that combines BM25 and embedding retrieval in a single component and fuses the results.
Extended Methods
Beyond the standard Document Store protocol, SolrDocumentStore supports filter-based bulk operations and metadata introspection, each with an async twin:
delete_by_filter/update_by_filter: delete or update the metadata of all documents matching a filter.count_documents_by_filter/count_unique_metadata_by_filter: count matching documents or the distinct values of metadata fields.get_metadata_fields_info,get_metadata_field_min_max,get_metadata_field_unique_values: inspect which metadata fields exist, their types, and their value ranges.