How to Integrate Haystack for retail banking with Elasticsearch for startups
Combining Haystack for retail banking with Elasticsearch gives you a clean pattern for building agentic banking workflows that can search policy docs, customer notes, product catalogs, and compliance knowledge in one place. For startups, this means faster support automation, better internal copilots, and retrieval that can handle both structured bank data and unstructured operational content.
Prerequisites
- •Python 3.10+
- •An Elasticsearch cluster running locally or in the cloud
- •A Haystack for retail banking project with access to your bank-specific pipelines
- •API credentials for any managed Haystack service you’re using
- •
pipinstalled - •Basic familiarity with document indexing and retrieval
Install the core packages:
pip install haystack-ai elasticsearch python-dotenv
Integration Steps
- •
Set up your Elasticsearch connection.
Start by connecting your app to Elasticsearch and confirming the cluster is reachable. In production, keep credentials in environment variables.
from elasticsearch import Elasticsearch
import os
es = Elasticsearch(
os.getenv("ELASTICSEARCH_URL", "http://localhost:9200"),
basic_auth=(
os.getenv("ELASTICSEARCH_USER", "elastic"),
os.getenv("ELASTICSEARCH_PASSWORD", "changeme"),
),
)
print(es.info())
- •
Create an index for retail banking content.
Use a dedicated index for policy docs, FAQs, product terms, and customer support knowledge. Keep the schema simple at first: text fields plus metadata fields like
doc_type,region, andproduct.
index_name = "retail-banking-kb"
if not es.indices.exists(index=index_name):
es.indices.create(
index=index_name,
mappings={
"properties": {
"content": {"type": "text"},
"title": {"type": "text"},
"doc_type": {"type": "keyword"},
"region": {"type": "keyword"},
"product": {"type": "keyword"},
}
},
)
docs = [
{
"title": "Mortgage eligibility",
"content": "Applicants need proof of income, credit history, and address verification.",
"doc_type": "policy",
"region": "us",
"product": "mortgage",
},
{
"title": "Debit card replacement",
"content": "Cards can be replaced through the app or by calling support after identity verification.",
"doc_type": "faq",
"region": "us",
"product": "cards",
},
]
for i, doc in enumerate(docs):
es.index(index=index_name, id=i + 1, document=doc)
es.indices.refresh(index=index_name)
- •
Wire Elasticsearch into Haystack as the retriever backend.
In Haystack, use Elasticsearch as the document store and retrieval layer. The exact class names depend on your Haystack version, but the common pattern is
ElasticsearchDocumentStoreplus an embedding or BM25 retriever.
from haystack import Document
from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore
from haystack.components.retrievers import InMemoryBM25Retriever
document_store = ElasticsearchDocumentStore(
hosts=[os.getenv("ELASTICSEARCH_URL", "http://localhost:9200")],
index=index_name,
)
haystack_docs = [
Document(content=d["content"], meta=d) for d in docs
]
document_store.write_documents(haystack_docs)
retriever = InMemoryBM25Retriever(document_store=document_store)
- •
Build a retrieval pipeline for your agent.
This is where the integration becomes useful. Your agent asks a question, Haystack retrieves matching banking knowledge from Elasticsearch, and your downstream LLM uses those passages to answer safely.
from haystack import Pipeline
from haystack.components.builders import PromptBuilder
prompt_template = """
Answer the question using only the provided documents.
Question: {{question}}
Documents:
{% for doc in documents %}
- {{ doc.content }} ({{ doc.meta.title }})
{% endfor %}
"""
pipeline = Pipeline()
pipeline.add_component("retriever", retriever)
pipeline.add_component("prompt_builder", PromptBuilder(template=prompt_template))
pipeline.connect("retriever.documents", "prompt_builder.documents")
- •
Run a query through the integrated flow.
Querying should return relevant retail banking content from Elasticsearch through Haystack’s retrieval layer. This is the point where you can plug in an LLM or keep it retrieval-only for deterministic support workflows.
result = pipeline.run(
{
"retriever": {"query": "What do I need to replace my debit card?"},
"prompt_builder": {"question": "What do I need to replace my debit card?"},
}
)
print(result["prompt_builder"]["prompt"])
Testing the Integration
Use a direct Elasticsearch query first, then verify Haystack returns the same domain-relevant content.
query = {
"query": {
"match": {
"content": "debit card replacement"
}
}
}
resp = es.search(index=index_name, body=query)
print(resp["hits"]["hits"][0]["_source"]["title"])
haystack_result = pipeline.run(
{
"retriever": {"query": "debit card replacement"},
"prompt_builder": {"question": "debit card replacement"},
}
)
print(haystack_result["prompt_builder"]["prompt"])
Expected output:
Debit card replacement
Answer the question using only the provided documents.
Question: debit card replacement
Documents:
- Cards can be replaced through the app or by calling support after identity verification. (Debit card replacement)
Real-World Use Cases
- •
Retail banking support agent
- •Answer questions about fees, account opening requirements, card replacement, and loan eligibility using indexed policy content.
- •
Internal compliance assistant
- •Retrieve region-specific rules and product policies from Elasticsearch while keeping responses grounded in approved documentation.
- •
Sales enablement copilot
- •Let relationship managers search product collateral, pricing notes, and eligibility criteria before talking to customers.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit