Role in the AI Workflow

Retrieval is the first step in every AI workflow. Before an application can rank results, generate an answer, recommend content, or execute an agent task, it must first identify the most relevant information. Every search engine, RAG application, recommendation system, and AI agent depends on retrieval to build the candidate set used for downstream ranking and inference.

Why AI Retrieval Matters

Every AI application begins with retrieval.

Before an application can rank results, generate an answer, recommend products, or complete an agent task, it first has to find the right information. That initial retrieval step determines everything that follows. If the relevant data isn't retrieved, no ranking model or large language model can recover it later.

The goal is to retrieve the best possible candidate set while keeping latency, infrastructure costs, and downstream inference under control. In practice, retrieval must balance two competing priorities:

  • High recall: Retrieve enough relevant information to maximize answer quality.
  • High efficiency: Limit the candidate set so ranking and AI inference remain fast and cost-effective.

Modern AI applications place far heavier demands on retrieval than traditional search. A single request may need to combine keyword matching, semantic similarity, structured filters, permissions, freshness, personalization, and business rules before returning candidates.

A single request may need to combine keyword matching, semantic similarity, sparse retrieval, structured filters, permissions, freshness, personalization, and business rules before returning candidates.

Different AI workloads place different demands on retrieval:

  • Search: Lexical search, structured filtering, facets
  • RAG: Semantic retrieval, metadata filtering, permissions
  • Recommendations: Similarity search, user preferences, behavioral signals
  • Agentic AI: Semantic retrieval, structured constraints, tool selection, contextual memory

Supporting each method on its own is not the hard part. The hard part is combining them into one retrieval pipeline that returns the highest-quality candidates without adding latency, cost, or operational complexity.

The Cost of Stitching Systems Together

Most teams solve this by stitching together multiple systems. Traditional search engines excel at lexical retrieval and filtering but often treat vector search as an extension. Vector databases provide fast semantic similarity search but typically depend on another system for keyword search, filtering, ranking, and serving. As the application scales, that split turns into a standing operational tax:

  • Data synchronization: The same documents live in two stores, so every write has to reach both. Any lag opens a consistency window where the two systems disagree about what exists.
  • Duplicated infrastructure: Two indexes, two capacity plans, two scaling stories, two failure modes, for one corpus.
  • Reconciliation logic in the app: Separate systems return separate result lists, so the application has to merge and re-rank them. Filters have to be applied to every sub-search by hand. Miss one, and that half of the query quietly ignores the filter and returns out-of-scope results.

Vespa takes a different approach. Lexical search, vector search, sparse retrieval, structured filtering, and custom retrieval logic run inside one engine, over one index, as one query. Instead of orchestrating multiple systems, you retrieve one candidate set and use it as the foundation for search, recommendations, RAG, and agentic AI.

How the Approaches Compare

The difference is less about which methods each system supports and more about how they combine.

Capability
Traditional Search Engine
Vector Database
Vespa
Lexical/keyword search
Native
Add-on or external
Native
Vector/semantic search
Add-on
Native
Sparse/learned retrieval
Add-on (e.g. ELSER)
Native (weighted-set operators)
Hybrid in a single request
Result-list fusion
Single pass over one index
Filtering with vector search
Pre- or post-filter
Pre- or post-filter
In-retrieval
Single index, no cross-system sync
Varies
Varies
Yes
Ranking on retrieved candidates
Basic
External
Multi-phase over all matches

Reading the table:

  • Hybrid in a single request: Search engines and vector databases can now issue hybrid queries in one API call, but they do it by running each retriever separately and fusing the ranked lists (reciprocal rank fusion and similar). Vespa scores lexical and vector signals in a single pass over the same index, so there is no separate list to reconcile.
  • Filtering with vector search: Pre-filtering can force a brute-force scan; post-filtering can collapse recall when the filter is selective. Vespa's query planner estimates filter selectivity and selects the strategy that protects recall, without you tuning it per query.
  • Ranking on retrieved candidates: Vespa applies a cheap first-phase score across every matched document, then re-ranks progressively smaller sets with more expensive models. Ranking is not bolted on after retrieval in a separate service.

The practical result: higher retrieval quality from combining lexical, semantic, sparse, and structured methods in one query; less infrastructure to operate; and freedom to change retrieval strategy without redesigning the rest of the application.

How AI Retrieval Works in Vespa

Every query passes through the same retrieval pipeline. Vespa organizes retrieval into four stages: query processing, retrieval execution, structured filtering, and candidate generation.  Each stage has a distinct job, and each can be tuned independently for efficiency, scalability, and retrieval quality.

1. Query Processing: One Language for Every Method

Every retrieval request begins with a query written in Vespa Query Language (YQL).

Rather than treating keyword search, vector search, and filtering as separate operations, YQL provides a single language for defining how candidates should be retrieved.

A query can combine retrieval operators, boolean logic, filters, and query parameters into one execution plan. You describe retrieval declaratively, and Vespa executes the pipeline. That keeps complex strategies easier to express, maintain, and evolve than coordinating several APIs by hand.

2. Retrieval execution: Operators over the Right Index

Retrieval operators determine how candidate documents are selected. 

Each operator is optimized for a specific retrieval strategy and can be combined with others within the same query. This allows applications to combine exact matching, semantic retrieval, sparse retrieval, and business constraints within one retrieval pipeline.

Common operators include:

  • nearestNeighbor for dense vector (semantic) retrieval.
  • weakAnd for efficient top-k lexical retrieval (Vespa's WAND-family operator for OR-style keyword queries).
  • wand for weighted-set retrieval, used for learned-sparse representations and weighted-term recommendation.
  • contains for keyword and field matching.
  • Boolean operators (and, or, not) for precise query logic.

Each operator uses the index structure best suited to its method, and all index types participate in the same distributed engine:

  • Inverted indexes for lexical search.
  • HNSW indexes for approximate nearest neighbor search. This is the same real-time, mutable HNSW index Vespa updates in place, so vector retrieval reflects fresh data without a rebuild.
  • Attributes for fast lookups, filtering, sorting, and grouping.
  • Structured document fields for metadata and field-based retrieval.

The engine selects the right index per operator and executes the whole query through one distributed runtime, so different retrieval strategies work together without extra infrastructure.

3. Structured Filtering: Constraints that Protect Recall

Structured filters participate directly in retrieval, instead of being applied after candidate selection. Because filtering is part of the retrieval model, you can combine structured constraints with lexical, semantic, and sparse retrieval in a single query.

Common examples include:

  • Show only products that are in stock.
  • Retrieve only documents a user is authorized to access.
  • Limit results to a specific language or region.
  • Search only documents published within the last year.

Filtering alongside vector search is where naive approaches break. Pre-filtering (filter first, then search) guarantees the filter is honored, but running an unaided scan over the filtered subset can be slow. Post-filtering (search first, then drop non-matches) is fast, but on a selective filter it collapses recall: you ask for 100 results and get a handful, because most of what the vector search found gets discarded.

Vespa avoids forcing that choice. When you combine nearestNeighbor with a filter, the query planner estimates how many documents the filter matches and selects a strategy automatically. For most filters it traverses the HNSW graph and skips non-matching neighbors as it goes, so the search keeps moving until it has enough real matches. For highly selective filters it switches to an exact search over just the filtered set. Either way, the goal is to expose the target number of genuine candidates to ranking, so applying constraints during retrieval improves efficiency and relevance instead of trading one for the other.

4. Candidate Generation

Candidate generation defines which documents continue into the ranking pipeline, and how many. You set the target size of the retrieved set (for example with targetHits) independently from ranking, so retrieval can be tuned for recall, latency, and cost on its own.

From there, ranking is multi-phase, and the distinction matters. Vespa's first phase runs a cheap scoring expression across every matched document, not a pre-truncated top-k. It then re-ranks a smaller set per content node with a more expensive second-phase model, and can apply a final, most expensive model to the merged top set in a global phase. Retrieval bounds the match set; multi-phase ranking then spends compute where it pays off. 

As requirements change, you refine candidate generation without redesigning the downstream ranking models.

Configuring Retrieval in Vespa

Every application has different retrieval requirements. An ecommerce search experience may prioritize lexical retrieval with inventory filters, while a RAG application may rely on semantic retrieval combined with document metadata and access controls. 

Vespa exposes configurable controls throughout the retrieval pipeline so you can tune retrieval behavior without changing the underlying architecture.

Configuration options include:

  • Retrieval strategy: Choose which retrieval operators that participate in a query.
  • Query composition: Define retrieval logic using YQL, including operators, filters, and query parameters.
  • Candidate generation: Tune candidate set sizes to balance recall, latency, and computational cost.
  • Filtering and constraints: Apply structured filters during retrieval using indexed fields such as category, language, geography, permissions, inventory, timestamps, tenant, and document type to improve retrieval efficiency and relevance.
  • Index configuration: Configure inverted indexes, HNSW indexes, and attributes for different retrieval workloads.
  • Query-time parameters: Adjust query terms, embeddings, nearest-neighbor parameters, filters, and execution settings for individual requests.

Because retrieval is broken up into configurable components,you can refine candidate generation over time without changing downstream ranking models or redesigning the search pipeline.

Learn with Vespa

Learn how to build search, recommendation, and RAG applications with Vespa through a free, self-paced course that combines hands-on exercises with links to the documentation.

Frequently Asked Questions

Need more than a quick answer?

If these FAQs don't answer your question, there are several ways to continue:

Learn the fundamentals with our free online training at learn.vespa.ai.

Experience Vespa yourself with a free Vespa Cloud trial.

Watch the Getting Started with Vespa AI Search YouTube video

Contact our team to discuss your application or migration project.
What is AI retrieval?
AI retrieval is the process of selecting the most relevant candidate documents from an index before ranking or AI inference begins. It is the first stage of every AI workflow, including search, retrieval-augmented generation (RAG), recommendations, and agentic AI. Retrieval quality sets the ceiling for every stage that follows, because a document that is not retrieved cannot be ranked or used as context by an LLM.
What is the difference between retrieval and ranking?
Retrieval selects a candidate set of potentially relevant documents, while ranking scores and orders that set with more expensive relevance models. Retrieval optimizes for recall and efficiency across a large index; ranking optimizes for precision over a much smaller set. In Vespa, retrieval produces the candidate set that the multi-phase ranking pipeline then refines.
What is hybrid retrieval?
Hybrid retrieval combines keyword (lexical) search and vector (semantic) search in a single query. Keyword search matches exact terms, identifiers, and rare tokens, while vector search captures meaning and paraphrase, so using both improves quality over either alone. Vespa runs hybrid retrieval in one pass over one index, rather than running two searches and merging their result lists.
Can lexical and vector search run in the same query in Vespa?
Yes, Vespa runs lexical search, vector nearest-neighbor search, sparse retrieval, and structured filters together in a single YQL query. All methods execute inside the same distributed engine over the same index, which removes the need to query separate systems and reconcile their results.
How does filtering work with vector search in Vespa?
In Vespa, structured filters are applied during vector retrieval rather than before or after it as a separate step. Pre-filtering alone can be slow and post-filtering can collapse recall on selective filters, so Vespa's query planner estimates filter selectivity and picks the strategy that protects recall: it constrains the HNSW graph traversal for most filters and falls back to exact search over the filtered set when the filter is highly selective. The result is that enough genuine matches reach ranking even under restrictive filters.
How does AI retrieval work for RAG?
In RAG, retrieval selects the passages an LLM uses as context, usually by combining semantic retrieval with document metadata and access controls. Because the model can only reason over what retrieval returns, accurate and permission-aware retrieval directly improves answer quality and reduces hallucination. Vespa retrieves this context by combining vector, lexical, and structured filtering in one query, then passes the top candidates through ranking before generation.
What retrieval operators does Vespa support?
Vespa supports retrieval operators including nearestNeighbor for semantic vector search, weakAnd for efficient top-k lexical retrieval, wand for weighted-set (learned-sparse) retrieval, contains for keyword matching, and boolean operators (and, or, not) for query logic. Each operator is optimized for a specific strategy and can be combined with others in the same query. All operators execute within one retrieval pipeline over a single index.
Why use a unified retrieval engine instead of a separate search engine and vector database?
A unified retrieval engine runs lexical, vector, sparse, and structured retrieval in one query over one index, so there are no separate systems to keep in sync. Separate systems force you to maintain two indexes, apply filters to each sub-search by hand, and merge and reconcile result lists in the application layer, on top of duplicated infrastructure and a consistency window between the stores. Vespa builds retrieval into its core engine, which lowers operational complexity while improving retrieval quality and developer control.