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.
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
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.
document_store.write_documents(documents)
retriever = InMemoryBM25Retriever( document_store=document_store, top_k=1, )
top_k=1 keeps only the highest-scoring keyword match.
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.
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']}")
$ python3 in_memory_store_demo.py documents stored: 3 top match: Reset passwords through the identity portal. match topic: accounts