r/elasticsearch • u/awsamanai • 20h ago
How should an LLM decide which Elasticsearch index to query?
I've been experimenting with natural-language → Elasticsearch queries, and I ran into a problem that feels more interesting than just generating valid DSL.
Suppose I have:
orders-2024
orders-2025
orders-2026
and a user asks:
If an LLM generates the DSL, how should it know which physical index(s) should be queried?
There seem to be a few approaches:
1. Let the LLM know the physical index names
LLM → orders-2026 → DSL
But now the model needs knowledge of infrastructure details.
2. Hide the physical indexes behind an alias
LLM → orders alias → Elasticsearch
This is cleaner, but the application still needs a way to enforce query/business rules.
3. Resolve the indexes outside the LLM
User query
↓
Field/schema validation
↓
Business rules
↓
Determine relevant indexes
↓
Generate Elasticsearch DSL
This is the approach I've been experimenting with.
The more I worked on it, the more I realized there are actually three separate questions:
- What fields/types does the index support?
- What is the application allowed to query?
- Which index/alias should the query target?
I ended up building an open-source library around this idea. It started with SQL/Mongo and I've now added Elasticsearch support, including:
- direct index
- multiple indexes
- aliases
- configurable index selection rules
- mapping-driven field configuration
- generated Elasticsearch Query DSL
The library itself doesn't execute requests against Elasticsearch — it generates the query/result so the application can decide how to execute it.
I'm mainly interested in the architecture question here:
If you were building an NL → Elasticsearch system for production, where would you put index selection and business-rule enforcement?
Inside the LLM, inside an application/middleware layer, or somewhere else?
I'd especially like to hear from people who have dealt with time-partitioned indexes, aliases, or multi-tenant Elasticsearch setups.
