Concepts / Evaluating Embeddings for Semantic Retrieval

Evaluating Embeddings for Semantic Retrieval

Context precision is the percentage of retrieved documents that are relevant to the query, and it is computed only from the retrieved set.

  • Programming

Why One Number Is Not Enough

Imagine a retriever receives a query and hands back five documents. Some of them clearly address the question and some do not. If you wanted to judge that retriever, you would quickly run into a problem: a single number seems too blunt, because the retriever could be failing in two different ways. It might be filling its results with irrelevant material, or it might be missing relevant material that exists elsewhere in the database.

Context precision and context recall are the two metrics that separate these failure modes. Precision asks whether what the retriever returned is worth having. Recall asks whether the retriever found everything worth having. In this article you will compute both by hand, see exactly which counts each one uses, and learn why reading them together gives a fuller picture than either one alone.

Following One Query Through the Retriever

Before defining either metric formally, follow one query from start to finish. Suppose a database holds 4 documents that are relevant to a query. The retriever returns 5 documents, and when you check them against the query, 3 of those 5 are relevant. The diagram below shows one possible arrangement of those five results. The positions of the relevant documents are illustrative; what matters is the count.

Notice what this tells you. Three relevant documents came back, so the retrieved set is partly good. But the database contained 4 relevant documents in total, which means one relevant document was missed. Keep these counts in mind, because each metric uses a different group of them. Precision looks at the five results. Recall looks at the four relevant documents in the whole database.

What do you think happens?

Using only the counts from this query, how many relevant documents did the retriever miss?

Reveal answer
Answer:

1

The database holds 4 relevant documents and the retriever returned 3 of them, so 4 minus 3 leaves 1 relevant document that was never retrieved.

Computing Context Precision

Definition

Context precision is the percentage of retrieved documents that are relevant to the query, and it is computed only from the retrieved set.

Rule

The denominator is the retrieved set, the list the retriever actually handed back. Precision never looks at documents the retriever ignored. Precision is also the more convenient of the two to compute: it only requires comparing the retrieved documents to the query, and that comparison can be carried out by an AI judge. You do not need to inspect the rest of the database to get a precision score.

Worked example

Precision for the Five-Document Query

A retriever returns 5 documents for a query, and 3 of them are relevant to that query. What is the context precision?

  1. 1
    Identify the denominator

    The denominator is the total number of retrieved documents, which is 5.

  2. 2
    Identify the numerator

    The numerator is the number of retrieved documents that are relevant, which is 3.

  3. 3
    Divide and express as a percentage

    Compute $\frac{3}{5} = 0.6$, which is 60 percent.

Result

Context precision is 60 percent. Of the documents the retriever handed back, 60 percent were relevant.

Computing Context Recall

Definition

Context recall is the percentage of all relevant documents in the database that the retriever returned.

Rule

Notice that the numerator is the same as in precision: the relevant documents that came back. Only the denominator changes. For recall, the denominator is the set of all relevant documents, which may be spread across the whole database, not just the retrieved list.

Worked example

Recall for the Same Query

The database holds 4 relevant documents for the query, and the retriever returned 3 of them. What is the context recall?

  1. 1
    Identify the numerator

    The relevant documents that were retrieved number 3.

  2. 2
    Identify the denominator

    All relevant documents in the database number 4, including the one that was never retrieved.

  3. 3
    Divide and express as a percentage

    Compute $\frac{3}{4} = 0.75$, which is 75 percent.

Result

Context recall is 75 percent. The retriever found three quarters of the relevant material that exists in the database.

The two columns share the same numerator, 3 relevant retrieved documents, and differ only in what they divide by. Precision divides by the 5 documents the retriever returned. Recall divides by the 4 relevant documents that exist in total. Because the denominators differ, a retriever can score well on one metric and poorly on the other.

Why Recall Is Harder to Measure

The denominator for recall is the set of all relevant documents, which may be spread across the whole database. Because of this, recall cannot be measured by looking only at what the retriever returned. To know how many relevant documents exist, you must annotate the relevance of every document in your database to the query. That is a much larger job than checking five results.

Precision never has to find the relevant documents that the retriever missed, because it only looks at what came back. Recall has to account for them. That is why the source notes that the precision comparison can be carried out by an AI judge without inspecting the rest of the database, while recall depends on annotation across the whole collection.

Both metrics depend on an evaluation set, which you build before you can score anything. The set contains a list of test queries and a set of documents. For each test query, every test document is annotated as relevant or not relevant. The annotation can be done by humans or by AI judges. Once those labels exist, you compute the precision and recall of the retriever on this evaluation set.

When Order of Results Matters

Precision and recall treat the retrieved documents as a group. They do not care whether the relevant document appeared first or last in the list. If the order of results matters to your system, precision and recall alone will not capture it. The source points to ranking-aware metrics for that case. For example, if more relevant documents should be ranked first, you can use NDCG (normalized discounted cumulative gain), MAP (Mean Average Precision), or MRR (Mean Reciprocal Rank).

The practical rule is this: use context precision and context recall when the question is which documents were found and how many were relevant. Reach for NDCG, MAP, or MRR when the question also depends on where in the list those relevant documents sit. Both groups of metrics are evaluations of the retriever in isolation, and neither is the final verdict on the system.

Mistakes Beginners Make With These Metrics

  • Common mistake

    Dividing recall by the number of retrieved documents

    What it looks like

    Using 3 out of 5 as the recall value for the query above.

    Why it fails

    That fraction is precision. Recall's denominator is the total number of relevant documents in the database, which is 4 here.

    Do this instead

    Divide the relevant retrieved count by the total relevant count in the whole database: 3 divided by 4 gives 75 percent.

  • Common mistake

    Concluding that high precision means the retriever is thorough

    What it looks like

    A retriever returns two documents, both relevant, while the database holds ten relevant documents. Its precision is 100 percent.

    Why it fails

    Precision never looks at documents the retriever ignored, so it cannot detect the eight relevant documents that were missed.

    Do this instead

    Compute context recall alongside precision. Here recall is 2 out of 10, or 20 percent, which reveals the gap.

  • Common mistake

    Estimating recall from only the retrieved results

    Why it fails

    Recall needs the total count of relevant documents in the database, which requires annotating the whole database for the query.

    Do this instead

    Build an evaluation set with relevance labels for every test document before scoring recall.

  • Common mistake

    Assuming precision and recall reveal whether the order of results is good

Practice: Score a New Retrieval

EASY

A retriever returns 4 documents for a query. Two of them are relevant. The database holds 5 relevant documents for that query in total. Compute the context precision and the context recall, then state which failure mode the retriever shows more of.

Hints
  • Precision divides relevant retrieved documents by the 4 retrieved documents.
  • Recall divides the same 2 relevant retrieved documents by the 5 relevant documents in the whole database.
  • Compare the two results: the lower value points to the bigger gap.

Key Takeaways

  • Context precision is the percentage of retrieved documents that are relevant to the query, computed only from the retrieved set: relevant retrieved divided by total retrieved.
  • Context recall is the percentage of all relevant documents in the database that the retriever returned: relevant retrieved divided by all relevant documents in the database.
  • Precision and recall share a numerator but use different denominators, so they can disagree; a retriever can be precise but incomplete.
  • Recall is harder to measure because it requires annotating relevance across the whole database, while precision only requires judging the retrieved documents.
  • When the order of results matters, ranking-aware metrics such as NDCG, MAP, and MRR become the relevant tools; retriever scores are also only one piece of evidence within the full RAG system.

Related concepts

More concepts starting with E