ThisCalculator

Everyday

Fixing Sanity Vector Search With Two-Hop Context

Updated · Standard formula, shown below

Fixing Sanity Vector Search With Two-Hop Context

Yes—adding a two-hop relational context engine can fix Sanity vector search when relevant results are missed because the query and matching content use different words or depend on linked records. It complements, rather than replaces, vector search: retrieve likely matches first, then follow selected relationships to assemble the context needed to rank or answer the query.

Why vector search can miss the right Sanity content

Sanity is a structured content platform: documents can contain typed fields and references to other documents. A vector search system turns text into numerical embeddings and retrieves items whose vectors are close to a query vector. This is useful for semantic similarity, but similarity alone does not represent every relationship in a content model.

Imagine a Sanity dataset containing a product, a brand document, and a set of care instructions. A query asking “How do I clean this maker’s travel mug?” may need the mug document, its referenced brand, and the care instructions associated with that brand or product. A vector search over product descriptions might find a mug, but fail to include the instructions if they use different wording or live in a separate document.

This is not necessarily a broken embedding model. Common causes include incomplete text being embedded, relevant facts being stored in references rather than searchable text, overly restrictive filters, and a top-results limit that excludes useful candidates.

What “two-hop relational context” means

A hop is one step along a relationship in the content graph. Starting from a retrieved document, a first hop follows its direct references; a second hop follows references from those linked documents. The engine gathers a bounded neighborhood around likely matches, then uses that additional context to improve retrieval, ranking, or the material supplied to a language model.

  • Seed: retrieve candidate documents using vector similarity, keyword search, or both.
  • Hop one: follow direct references, such as a product’s brand or an article’s author.
  • Hop two: follow selected references from those linked documents, such as a brand’s support pages or an author’s related articles.
  • Use: combine the useful records, remove duplicates, and rank or present them with their source information.

“Two-hop” describes graph traversal depth, not two separate vector searches, and it does not guarantee that a relevant document will be found. The relationships must exist, be accessible, and be chosen sensibly.

How the approach fits a Sanity search pipeline

A practical pipeline starts by identifying which fields are suitable for semantic retrieval. For each document type, prepare searchable text from meaningful fields—such as title, summary, body, and selected labels—while avoiding irrelevant content such as navigation boilerplate. Generate or update embeddings when that source text changes, and retain a link to the Sanity document so results can be traced back.

At query time, embed the user’s query and retrieve a manageable candidate set. Apply access rules and any reliable filters, then traverse only approved reference fields to a maximum depth of two. The system can add linked documents as context, or use their details to adjust the seed documents’ ranking. Finally, return concise evidence with document identifiers or URLs, rather than treating every traversed record as equally relevant.

Sanity’s content model and query tools provide the source data and references; the exact vector index, embedding service, and traversal implementation depend on the application. A two-hop context engine is an architectural pattern, not a single built-in switch that automatically repairs every search setup.

Design choices that determine whether it helps

More context is not automatically better. A popular author, category, or brand can connect to many records; traversing all of them can create noise, slow responses, and crowd out the evidence that matched the query. Set rules for which relationship types to follow, impose result and payload limits, and avoid cycles or repeated documents.

  • Choose useful edges: follow relationships that carry meaning for the task, not every reference in the schema.
  • Keep the depth bounded: two hops is a practical limit for many use cases, but it is a design choice, not a universal optimum.
  • Preserve provenance: record which seed or relationship brought each document into the result set.
  • Respect permissions: apply access controls to both seed documents and related documents; a reference must not expose content the user cannot access.
  • Measure quality: compare relevance and answer support on representative queries before and after adding traversal.

Traversal should use a clear policy for drafts, published documents, and hidden or deleted records. Otherwise, a valid-looking relationship can pull stale or unintended content into results.

Common failure modes and how to diagnose them

If the expected seed document never appears, inspect the embedding input, vector index freshness, query filters, and candidate limit before adding more hops. Relational expansion cannot reliably rescue a document that was not retrieved and has no path from a retrieved item.

If the seed is correct but the answer lacks a fact stored elsewhere, verify that the relevant Sanity reference is populated and that the traversal follows that field. Check whether the linked document is excluded by publication status or permissions. If results become less relevant after traversal, narrow the edge allowlist, reduce the number of linked records, or use linked context as supporting evidence rather than as a new top-ranked result.

Also distinguish retrieval failure from answer-generation failure. A search result may contain the right document while a downstream system ignores it, truncates it, or summarizes it incorrectly. Logging the seed IDs, traversed IDs, filters, and final context helps locate the stage where information is lost.

How to evaluate it against alternatives

Compare the two-hop approach with simpler options using a fixed set of real queries and known relevant documents. Measure whether relevant items appear near the top, whether answers have supporting sources, and how latency and context size change. Include queries that should not traverse to unrelated records, not only examples where expansion helps.

  • Improve indexed text: often the simplest fix when important facts are omitted from embeddings.
  • Keyword or hybrid search: useful for exact names, codes, and terms that embeddings may blur.
  • Structured filters: effective when the user specifies a type, category, date, or other explicit constraint.
  • Relational expansion: valuable when the answer depends on connected documents and the content graph is maintained well.

These methods can be combined. For example, hybrid retrieval can find a product by its model name, while one or two relationship hops add the manufacturer’s compatible accessories. The trade-offs are implementation complexity, traversal cost, and the need to keep search data and content references up to date.

Frequently Asked Questions

Does a two-hop engine replace vector search?
No. It typically expands or enriches candidates retrieved by vector, keyword, or hybrid search. If the seed retrieval is poor, relational expansion may not reach the missing content.

Does Sanity provide two-hop vector search automatically?
Do not assume so. Sanity stores structured documents and references, but the search index and traversal behavior depend on how the application is built.

Why not search every linked document?
Broad traversal can add irrelevant records, increase processing time, and make it harder to identify trustworthy evidence. Restrict traversal to useful relationship types and a bounded number of results.

Can two-hop retrieval expose private or draft content?
It can if access and publication rules are not applied consistently. Enforce authorization on every retrieved or traversed document, not just on the initial vector results.

How can I tell whether the change improved search?
Test a representative query set against known relevant documents. Compare ranking quality, answer support, latency, and irrelevant context, and inspect which relationship paths contributed to each result.

Bottom line

A two-hop relational context engine is a credible fix for Sanity search when the missing information lives in linked documents rather than in the text being embedded. It works best as a controlled layer around retrieval: improve the searchable text, retrieve good seeds, follow a small set of meaningful references, enforce access rules, and evaluate results against real queries. It will not compensate for missing relationships, stale indexing, or weak initial retrieval, but it can make a structured content graph useful to semantic search.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.