You can think of the DocumentStore as a "database" that:

  • stores your texts and meta data
  • provides them to the retriever at query time

There are different DocumentStores in Haystack to fit different use cases and tech stacks.


Initialising a new DocumentStore within Haystack is straight forward.

Install Elasticsearch and then start an instance.

If you have Docker set up, we recommend pulling the Docker image and running it.

docker pull
docker run -d -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.9.2

Next you can initialize the Haystack object that will connect to this instance.

document_store = ElasticsearchDocumentStore()

Note that we also support Open Distro for Elasticsearch. Follow their documentation to run it and connect to it using Haystack's OpenDistroElasticsearchDocumentStore class.

Follow the official documentation to start a Milvus instance via Docker

You can initialize the Haystack object that will connect to this instance as follows:

document_store = MilvusDocumentStore()

The FAISSDocumentStore requires no external setup. Start it by simply using this line.

document_store = FAISSDocumentStore(faiss_index_factory_str="Flat")

The InMemoryDocumentStore() requires no external setup. Start it by simply using this line.

document_store = InMemoryDocumentStore()

The SQLDocumentStore requires SQLite, PostgresQL or MySQL to be installed and started. Note that SQLite already comes packaged with most operating systems.

document_store = SQLDocumentStore()

Each DocumentStore constructor allows for arguments specifying how to connect to existing databases and the names of indexes. See API documentation for more info.

Input Format

DocumentStores expect Documents in dictionary form, like that below. They are loaded using the DocumentStore.write_documents() method. See Preprocessing for more information on the cleaning and splitting steps that will help you maximize Haystack's performance.

document_store = ElasticsearchDocumentStore()
dicts = [
        'text': DOCUMENT_TEXT_HERE,
        'meta': {'name': DOCUMENT_NAME, ...}
    }, ...

Writing Documents (Sparse Retrievers)

Haystack allows for you to write store documents in an optimised fashion so that query times can be kept low. For sparse, keyword based retrievers such as BM25 and TF-IDF, you simply have to call DocumentStore.write_documents(). The creation of the inverted index which optimises querying speed is handled automatically.


Writing Documents (Dense Retrievers)

For dense neural network based retrievers like Dense Passage Retrieval, or Embedding Retrieval, indexing involves computing the Document embeddings which will be compared against the Query embedding.

The storing of the text is handled by DocumentStore.write_documents() and the computation of the embeddings is started by DocumentStore.update_embeddings().


This step is computationally intensive since it will engage the transformer based encoders. Having GPU acceleration will significantly speed this up.

Choosing the Right Document Store

The Document Stores have different characteristics. You should choose one depending on the maturity of your project, the use case and technical environment:


  • Fast & accurate sparse retrieval with many tuning options
  • Basic support for dense retrieval
  • Production-ready
  • Support also for Open Distro


  • Slow for dense retrieval with more than ~ 1 Mio documents


  • Scalable DocumentStore that excels at handling vectors (hence suited to dense retrieval methods like DPR)
  • Encapsulates multiple ANN libraries (e.g. FAISS and ANNOY) and provides added reliability
  • Runs as a separate service (e.g. a Docker container)
  • Allows dynamic data management


  • No efficient sparse retrieval


  • Fast & accurate dense retrieval
  • Highly scalable due to approximate nearest neighbour algorithms (ANN)
  • Many options to tune dense retrieval via different index types (more info here)


  • No efficient sparse retrieval


  • Simple
  • Exists already in many environments


  • Only compatible with minimal TF-IDF Retriever
  • Bad retrieval performance
  • Not recommended for production


  • Simple & fast to test
  • No database requirements
  • Supports MySQL, PostgreSQL and SQLite


  • Not scalable
  • Not persisting your data on disk

Our Recommendations

Restricted environment: Use the InMemoryDocumentStore, if you are just giving Haystack a quick try on a small sample and are working in a restricted environment that complicates running Elasticsearch or other databases

Allrounder: Use the ElasticSearchDocumentStore, if you want to evaluate the performance of different retrieval options (dense vs. sparse) and are aiming for a smooth transition from PoC to production

Vector Specialist: Use the MilvusDocumentStore, if you want to focus on dense retrieval and possibly deal with larger datasets

© 2020 - 2021 deepset. All rights reserved.Imprint