Query Engines: The Basic Interface
What you'll learn: How query engines serve as the primary way to ask questions over your indexed data in LlamaIndex.
The Core Idea
After you've built an index in LlamaIndex (as you learned in the previous lesson), you need a way to actually ask questions and get answers. That's exactly what a query engine does—it's the interface between your natural language questions and your indexed documents.
Think of it like this: your index is a well-organized library, and the query engine is the librarian who understands your question, finds the relevant books, reads the important passages, and gives you a clear answer.
How It Works
When you create a query engine from your index, LlamaIndex sets up a complete question-answering pipeline:
- Takes your natural language query
- Retrieves relevant nodes/chunks from the index
- Synthesizes those chunks into a coherent answer using an LLM
- Returns a response object with the answer (and often metadata like sources)
# After building an index (from previous lesson)
query_engine = index.as_query_engine()
# Now you can ask questions naturally
response = query_engine.query("What are the main features of the product?")
print(response)
The beauty is that as_query_engine() handles all the complexity—retrieval, prompt construction, LLM calls, and response synthesis—behind a simple interface.
Why Query Engines Matter
Without query engines, you'd need to manually orchestrate retrieval, format prompts, call the LLM, and parse outputs. Query engines abstract this entire workflow into a single, intuitive method call.
Key Takeaway: Query engines are your primary tool for asking questions over indexed data—they wrap retrieval, LLM synthesis, and response generation into one clean interface.