Study guides / CCAR-P / Domain 1

Integration · Lesson 6 of 8

1.6 - Retrieval Strategies Matched to Data Shape and Query Pattern

Choose among semantic, keyword, hybrid, reranked, structured and agentic retrieval by matching the strategy to the data shape and the query pattern, and evaluate each stage separately.

There is no best retrieval method, only one that fits the shape of the data (unstructured prose, semi-structured documents, tables, code or logs) and the pattern of the query (a fuzzy concept, an exact identifier, an aggregate over rows, a multi-hop question, a conversational follow-up). Scenario questions in this area describe a data shape and a failing query type and ask you to pick the method that fits, or to spot the method that cannot possibly work. Chunking and indexing are Lesson 1.5; this lesson is about choosing how to search once the index exists.

The toolbox and where each tool breaks

StrategyStrengthTypical failure
Semantic (dense embeddings)Paraphrases and conceptsExact identifiers, rare tokens, part numbers, sometimes negation and numbers
Keyword (BM25)Exact terms: error codes, names, SKUsSynonyms and paraphrase; the user's words differ from the document's
Hybrid (both, merged by rank fusion)Covers concept and exact-match queriesTwo indexes to run; fusion and weights to tune
RerankingRe-scores a wide candidate set against the query for better top-k precisionCannot surface documents that were never in the candidate set; adds a model call
Metadata filteringNarrows by tenant, date, type or entitlementOnly as good as the metadata; wrong filters silently hide the answer
Structured or SQL retrievalFilters, joins and aggregates over tablesWrong or unsafe queries; needs schema context and safeguards
Query rewriting or decompositionTurns follow-ups into standalone queries; splits multi-part questionsExtra call; the rewrite can drift from the user's intent
Agentic retrievalThe model searches iteratively and follows referencesMore turns, tokens and latency; depends on good search tools

Anthropic's Contextual Retrieval experiments illustrate the layering: combining contextual embeddings with contextual BM25 through rank fusion cut the top-20 retrieval failure rate further than embeddings alone (5.7% to 3.7% to 2.9%), and adding a reranker over a wider candidate set (150 narrowed to 20) reached 1.9%. Those are numbers from their datasets, and the lesson is the pattern, not the digits: each stage fixes a different failure, and each adds cost.

Matching strategy to data shape and query pattern

Data and queryFitWhy
Prose knowledge base, conceptual questionsDense retrieval, plus a reranker if top-k precision is weakMeaning matters more than exact wording
Prose that queries reference by code, ID or product nameHybridDense vectors are unreliable on rare exact tokens; BM25 is strong there
Orders, metrics or events in tables; "how many", "total", "top 5"Structured query (parameterised SQL or a fixed set of query tools)Similarity search cannot count or sum; embedding a table does not make it computable
Large, fast-changing codebase or log store; exploratory questionsAgentic search (grep and glob style tools), possibly with an indexNo index staleness; the model can follow references
Multi-hop questions across documentsQuery decomposition or agentic retrievalThe second lookup depends on the first result
Chat follow-ups such as "and for contractors?"Rewrite with history into a standalone queryThe raw follow-up has no searchable content
Multi-tenant or permissioned dataMandatory metadata pre-filterAccess must be enforced before content reaches the model

Key concept: match the method to the shape of the answer

Ask what the answer looks like. A passage that means the same thing as the question wants semantic search. A row that contains a specific token wants keyword search. A number computed over many rows wants a query engine. A chain of dependent lookups wants an agent or a decomposition. Popularity of a technique is not evidence.

Agentic retrieval and structured data

Agentic retrieval lets Claude decide what to search for, inspect results, and search again. Anthropic's context engineering write-up describes the hybrid used by Claude Code: some context (project instructions) loaded up front, and the rest explored just in time with tools such as grep and glob, keeping lightweight identifiers rather than pre-loading content. It states the cost plainly: runtime exploration is slower than retrieving pre-computed data, and it needs careful tool design. Tool results can carry search_result blocks so the model's citations point at your sources (Lesson 1.5).

Structured retrieval needs different safeguards. Exposing free-form SQL to the model widens the blast radius (Lesson 1.1), so prefer parameterised query tools or a constrained query builder, run them on a read-only credential, expose the schema as context, cap result sizes and validate generated queries. Aggregates should come from the database, not from the model summing rows it was shown.

Cost, evaluation and safety

Common exam distractor

"Use vector search for everything" fails on identifiers and aggregates. "Add a reranker" is the wrong fix when the gold document is not in the candidate set, because a reranker only reorders what was retrieved. "Embed the whole orders table so the model can answer totals" asks similarity to do arithmetic. And "always retrieve more chunks" trades tokens and latency for a recall gain you should have measured first.

Exam traps

Practice question

A support knowledge base holds product manuals and troubleshooting articles. Users ask two kinds of questions: conceptual ones ('how do I speed up syncing?') and lookups by identifier ('what does ERR_4471 mean on device SKU-77821?'). The current dense-embedding retriever handles the conceptual questions well but often misses the identifier lookups even though the exact codes appear in the articles. Which change best fits the failure?

  • A Switch to an embedding model with more dimensions, re-embed the whole knowledge base, and re-run the identifier queries to see whether the exact codes now rank higher

    More dimensions can help general similarity but do not turn rare exact tokens into reliable matches. The failure is about exact-term matching, which a keyword index handles directly.

  • B Add a BM25 keyword index beside the dense index, merge the two rankings with rank fusion, and check recall separately for identifier and conceptual queries Correct

    BM25 matches the exact codes that dense vectors miss, dense retrieval keeps the conceptual coverage, and fusing the rankings serves both query types. Slicing the evaluation by query type verifies the fix helps the failing slice without hurting the other.

  • C Add a reranker on top of the existing dense retriever's top 50 candidates so that the articles containing the identifier are moved to the top of the list

    If the article with the code never appears in the dense top 50, the reranker never sees it. Reranking improves ordering of candidates, not candidate recall.

  • D Have Claude answer identifier questions from its own knowledge when retrieval returns nothing relevant, and flag those answers as unverified

    Device-specific codes and SKUs are your data. Answering from model memory risks confident fabrication, which is worse than a miss.

Build exercise: Run a retrieval bake-off across query patterns

Advanced · 120 minutes

You'll practice:

  1. Assemble a test set of at least 30 queries over a corpus that mixes prose and at least one table. Tag each query with a pattern: concept, exact identifier, aggregate, multi-hop or follow-up, and record the gold result for each.

    A bake-off is only meaningful if it reflects the real mix of queries and lets you slice results by pattern.

    You should see: A spreadsheet with query, pattern tag, gold document or gold answer, and (for follow-ups) the preceding turn.

    Hints
    1. Which patterns do you expect each strategy to fail on before you run anything?
    2. Write your predictions in a column first. Aim for roughly six queries per pattern so slices are not empty.
    3. Example rows: 'how do I speed up syncing' | concept | doc 12. 'ERR_4471 SKU-77821' | identifier | doc 88. 'total refunds by region last quarter' | aggregate | SQL result. 'and for contractors?' | follow-up | doc 31 with prior turn.
  2. Implement dense-only and BM25-only baselines. For each query record whether the gold item appears in the top 5 and top 20, and report recall by pattern tag.

    Two baselines show each method's blind spots and give the fusion step something to beat.

    You should see: A table of recall@5 and recall@20 per pattern per method, with identifier queries likely favouring BM25 and concept queries favouring dense.

    Hints
    1. Where do the two methods disagree, and does one ever find what the other misses?
    2. Keep the chunking identical across methods so only the retriever varies. Compute recall from the ranked lists you already have.
    3. For each method, ranks = search(query, k=20); hit5 = gold in ranks[:5]; hit20 = gold in ranks[:20]. Group by pattern tag with a dictionary of lists and average the booleans.
  3. Implement reciprocal rank fusion over the two ranked lists and evaluate the hybrid the same way.

    Rank fusion is a simple, tunable way to combine methods without comparing their raw scores, which are on different scales.

    You should see: A third row per pattern for the hybrid, ideally at least as good as the better baseline on each tag.

    Hints
    1. Why is fusing ranks safer than adding raw BM25 and cosine scores?
    2. For each document, sum 1 / (k + rank) over the lists it appears in, where k is a smoothing constant (60 is a common default), then sort by the total.
    3. def rrf(rankings, k=60):
          scores = {}
          for ranking in rankings:
              for rank, doc_id in enumerate(ranking, start=1):
                  scores[doc_id] = scores.get(doc_id, 0) + 1 / (k + rank)
          return sorted(scores, key=scores.get, reverse=True)
  4. Add a reranker over the top 50 hybrid candidates and measure precision at 5, latency added and the queries where it changed the outcome. Then deliberately test a query whose gold document is outside the top 50.

    This shows both what reranking is for (ordering candidates) and what it cannot do (create candidates).

    You should see: A before-and-after precision@5, the added latency per query, and at least one example where reranking could not help because the gold document was missing from the candidates.

    Hints
    1. For the failed example, at which stage did the gold document get lost?
    2. Call your reranker with the query and the candidate texts and keep only the top 5. Log the time of the rerank call separately.
    3. Report three numbers per pattern: recall@50 (candidate recall), precision@5 before rerank, precision@5 after rerank. If recall@50 is low, no reranker will fix it.
  5. Handle aggregate queries: design a parameterised, read-only query tool, route aggregate-tagged queries to it, and write the decision matrix that maps each pattern in your test set to a strategy with the trade-off you accepted.

    Aggregates are the pattern retrieval methods cannot serve. The matrix is the artifact you would defend in a design review.

    You should see: A working tool signature (for example refunds_by_region(quarter)), a read-only credential note, and a completed matrix.

    Hints
    1. What stops the model from asking the tool for something it should not see?
    2. Expose named parameters instead of raw SQL, use a read-only database role, add a row limit and log each call with the user identity.
    3. Matrix rows: concept -> dense + rerank (cost: one extra call). identifier -> hybrid (cost: second index). aggregate -> parameterised SQL tool on read-only credential (cost: tool design). follow-up -> query rewrite with history (cost: one extra call, risk of drift).

Sources