Concepts / Term-Based Retrieval

Term-Based Retrieval

Retrieval is the family of techniques that rank documents by their relevance to a query, and it is not unique to RAG; it also powers search engines, recommender systems, and log analytics.

  • Programming

A Search Box and a Ranked List

Suppose you type a few words into a search box and get back a list of results. Somewhere behind that list is a decision the system had to make for every item it could show you: how well does this item match what I asked for? Retrieval is the name for the family of techniques that make that decision and act on it. This article introduces the idea, shows where it sits in the larger picture, and sets up the two mechanisms that the rest of this chapter builds on.

What do you think happens?

When a retrieval system looks at a document in response to a query, does it simply answer yes or no, or does it do something more?

  • It answers yes or no for each document

  • It orders the documents so the most relevant come first

Reveal answer
Answer:

It orders the documents so the most relevant come first.

Retrieval does not hand back a yes or no for each document. It ranks documents by relevance to the query, so the most relevant ones appear first.

Retrieval as Ranking

At its core, retrieval works by ranking documents based on their relevance to a given query. The important word is ranking. Every retrieval method has to answer the same underlying question, which is how relevant each document is to the query, and then sort on the answer. The result is an ordered list, not a set of accept and reject decisions.

The operation has a clear shape. The query and the document collection both feed into a step that computes a relevance score for each document, and those scores determine the final order. The step that changes from one retrieval method to another is the middle one, where the score is computed. Everything before it and after it stays the same.

Querywhat the user asked forRelevance score perdocumentthe method-specific stepRanked listmost relevant firstDocument collectionthe items that could beshowncomparedeach document scoredscores sort the list
What happens to a query and a set of documents, step by step, before the final ranked list appears?

Retrieval Beyond RAG

It is easy to associate retrieval with retrieval-augmented generation (RAG), because RAG pipelines depend on fetching relevant material before a language model writes its answer. But retrieval is not unique to RAG. Information retrieval is an old idea, and it already forms the backbone of search engines, recommender systems, and log analytics. These systems existed long before RAG did.

The practical consequence is worth remembering. Many retrieval algorithms were developed for traditional retrieval systems, and those same algorithms can also be reused inside RAG. When you meet a retrieval method in a RAG setting, it may have a long history in search and recommendation work behind it.

Retrieval Versus Search

Two words appear constantly in this area, and they are not quite the same. Retrieval is typically limited to one database or system. Search involves retrieval across various systems. The difference is one of scope: retrieval describes finding relevant items within a single source, while search describes the wider act of looking across several sources.

Two Starting Mechanisms

The chapter introduces two common retrieval mechanisms as a starting point: term-based retrieval and embedding-based retrieval. Each one is a different answer to the scoring question raised earlier. Both answer the same question of how relevant each document is to the query, and they differ in how they compute the score that drives the ranking.

The central lesson is that the goal stays fixed while the scoring method varies. Term-based and embedding-based retrieval are two answers to one question. A broader way of grouping such methods, sparse versus dense retrieval, comes in the next part of the chapter, building on these scoring differences.

Finding Documents by Term

A term-based method has to connect each query term to the documents that contain it. The sketch below is an illustrative picture of one way to do that. It is not a detail the chapter specifies. Each term on the left is linked to the document IDs where it appears, so a query term can be followed directly to its documents rather than checking every document one by one.

raindoc 2, doc 5umbrelladoc 2sunnydoc 1, doc 4
How does a query term find every document that contains it without scanning each document?

Read the sketch as a toy illustration. If the query is rain umbrella, the query terms point to doc 2 and doc 5, so those are the documents a term-based method would have to score. The term lookup narrows the work, and the scoring step then decides how relevant each candidate is.

Same Goal, Different Scores

The two starting mechanisms can be lined up side by side. Both aim at the same outcome, a ranked list ordered by relevance to the query. The table below separates them by the feature they differ on, which is how each one computes its relevance score.

Term-based retrievalstarting mechanismEmbedding-basedretrievalstarting mechanismRelevance score perdocumentcomputed by this methodRelevance score perdocumentcomputed by this method
How do term-based and embedding-based methods differ in how they represent text and compute relevance scores?

Tracing One Query Through Two Scorers

Worked example

Ranking three documents for one query

A small collection holds three documents, A, B, and C. A query arrives. Each retrieval method must score every document and sort them. Using hypothetical scores, show how the ranking step works the same way regardless of which scoring method produced the numbers.

  1. 1
    Fix the goal

    Both methods must answer the same question for each document: how relevant is this document to the query? The output is a ranked list, not a yes or no for each document.

  2. 2
    Score with method one

    Suppose the term-based method gives A a score of 0.9, B 0.4, and C 0.7. These numbers are invented for illustration. The ranking step sorts them into A, C, B.

  3. 3
    Score with method two

    Suppose the embedding-based method gives A 0.6, B 0.8, and C 0.7 for the same query. The scores differ, so the ranked list becomes B, C, A.

  4. 4
    Compare the outputs

    The query and documents did not change, yet the order did. The only part that changed is the scoring step in the middle, which is exactly where the two mechanisms differ.

Result

The same query and document collection produce different rankings because the two methods compute different relevance scores. The sorting step is identical in both cases.

Mistakes Beginners Make With Retrieval

  • Common mistake

    Thinking retrieval returns a yes or no for each document

    Why it fails

    Retrieval ranks documents by relevance, so the output is an ordering where the most relevant items come first.

    Do this instead

    Describe the output as a ranked list and ask which documents rank highest.

  • Common mistake

    Believing retrieval only exists inside RAG systems

    Why it fails

    Information retrieval already forms the backbone of search engines, recommender systems, and log analytics, and existed long before RAG.

    Do this instead

    Treat RAG as one place retrieval is used, and recognize that its algorithms often have a search or recommendation history.

  • Common mistake

    Treating retrieval and search as strictly different in this chapter

    Why it fails

    The two terms differ in scope in general usage, but this chapter uses them interchangeably.

    Do this instead

    Read retrieval and search as the same activity of finding and ranking relevant documents when they appear in the chapter.

  • Common mistake

    Assuming the scoring step is the same for every method

    Why it fails

    Retrieval algorithms differ in how they compute relevance scores, so the middle step is where methods diverge.

    Do this instead

    Compare methods by their scoring step, while expecting the goal and the sorting step to stay fixed.

Applying the Ranking View

EASY

A video streaming service wants to show a list of titles that best match a viewer's search for science documentaries. Identify the query, the documents being ranked, and the output. Then explain which part of the process would differ between a term-based method and an embedding-based method.

Hints
  • The query is what the viewer typed, and the documents are the titles that could be shown.
  • The output is a ranked list, so think about how the titles are ordered.
  • Only the middle step, where each title gets a relevance score, changes between the two methods.

Key Takeaways

  • Retrieval is the family of techniques that rank documents by their relevance to a query, and its output is an ordering rather than a yes or no for each document.
  • Retrieval is not unique to RAG; it also powers search engines, recommender systems, and log analytics, and many of its algorithms are reused inside RAG.
  • Retrieval and search are used interchangeably in this chapter, referring to the same activity of finding and ranking relevant documents.
  • Term-based and embedding-based retrieval are two starting mechanisms that answer the same scoring question differently, which is why the same query can produce different rankings.

Related concepts

More concepts starting with T