Retrievers in Haystack need a document store that accepts indexed content and returns matching records during a query. InMemoryDocumentStore keeps that state inside the Python process, making it suitable for local experiments and automated tests that do not need a separate database service.
Each store instance owns a temporary index. Its documents disappear when the process exits, so applications that share data between processes or retain it across restarts need a persistent document store.
Keyword retrieval with InMemoryBM25Retriever exercises the stored documents without downloading an embedding model or supplying an API key. The program writes three records, reports the stored count, and returns the accounts record for a password-reset query.
Steps to create a Haystack in-memory document store:
- Create in_memory_store_demo.py with the Haystack imports and an InMemoryDocumentStore instance.
- in_memory_store_demo.py
from haystack import Document from haystack.components.retrievers.in_memory import InMemoryBM25Retriever from haystack.document_stores.in_memory import InMemoryDocumentStore document_store = InMemoryDocumentStore()
Related: How to install Haystack with pip
- Add three Document objects below the document_store assignment.
- in_memory_store_demo.py
documents = [ Document( content="Reset passwords through the identity portal.", meta={"topic": "accounts"}, ), Document( content="Restart the search service after changing the index.", meta={"topic": "operations"}, ), Document( content="Archive paid invoices from the billing dashboard.", meta={"topic": "billing"}, ), ]
Each meta dictionary records a topic that can be checked after retrieval.
- Write the document list to the in-memory store.
- in_memory_store_demo.py
document_store.write_documents(documents)
- Configure an InMemoryBM25Retriever below the write call.
- in_memory_store_demo.py
retriever = InMemoryBM25Retriever( document_store=document_store, top_k=1, )
top_k=1 keeps only the highest-scoring keyword match.
- Add the password-reset query and printed result fields below the retriever.
- in_memory_store_demo.py
matches = retriever.run(query="reset password")["documents"] top_match = matches[0] print(f"documents stored: {document_store.count_documents()}") print(f"top match: {top_match.content}") print(f"match topic: {top_match.meta['topic']}")
count_documents() checks the store, while the returned content and metadata check that the retriever can query the same instance.
- Confirm that in_memory_store_demo.py matches the consolidated source.
- in_memory_store_demo.py
from haystack import Document from haystack.components.retrievers.in_memory import InMemoryBM25Retriever from haystack.document_stores.in_memory import InMemoryDocumentStore document_store = InMemoryDocumentStore() documents = [ Document( content="Reset passwords through the identity portal.", meta={"topic": "accounts"}, ), Document( content="Restart the search service after changing the index.", meta={"topic": "operations"}, ), Document( content="Archive paid invoices from the billing dashboard.", meta={"topic": "billing"}, ), ] document_store.write_documents(documents) retriever = InMemoryBM25Retriever( document_store=document_store, top_k=1, ) matches = retriever.run(query="reset password")["documents"] top_match = matches[0] print(f"documents stored: {document_store.count_documents()}") print(f"top match: {top_match.content}") print(f"match topic: {top_match.meta['topic']}")
- Run in_memory_store_demo.py to verify the stored count and highest-scoring match.
$ python3 in_memory_store_demo.py documents stored: 3 top match: Reset passwords through the identity portal. match topic: accounts
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.