Skip to content
mentoor

Topic 5 of 5 · 5 min read

Query and document embedding asymmetry

Goal: Spot a pipeline that embeds queries and documents inconsistently and correct the call to use the model's query and document modes.

Many embedding models expect queries and documents to be embedded differently, so call each side in its own mode, exactly as the model's documentation says. A short question and a long passage that answers it do not look alike, and the model is trained to bridge that gap.

Check yourself · 1 of 3

An E5-style model expects a query prefix and a passage prefix. What happens if queries are sent with no prefix?

Choose one answer

Why it matters

A user asks "how do I reset my password?". The matching passage says "To change your credentials, open Settings and choose Security...". They share almost no words. Asymmetric models learn that a question and its answer passage should land near each other. They learn it with a marker: a text prefix, an instruction, or a separate API setting. If you skip the marker on one side, or use the document marker for queries, retrieval quality falls with no error message.

How it shows up

  • Prefixes. The E5 models expect query: before questions and passage: before documents. Other models use an instruction sentence for queries, such as "Represent this sentence for searching relevant passages: ".
  • Input types. Hosted APIs often take a parameter, for example input_type="query" or "document", and a task type in Google's API.
  • Library methods. Sentence Transformers provides separate encode_query and encode_document methods that apply the model's configured prompts.
  • Symmetric models. Some models are trained for text-to-text similarity and need no marker. Do not add one.

How to check

  1. Read the model card for the exact strings or parameters.
  2. Put the mode in one function used by both indexing and querying, so nobody can forget it.
  3. Run your labelled question set with the correct mode and with the wrong one. A visible gap confirms the mode matters for this model.

Before and after

-vec = model.encode(query)
+vec = model.encode_query(query)

Before: Documents are embedded with the passage prefix at indexing time, but queries are embedded as raw text.

After: Queries use the query mode, documents use the document mode, and both are logged in the index config.

What goes wrong

  • Embedding queries with the document mode because one helper is reused for both.
  • Adding a prefix at query time but not at indexing time, or the reverse, so the two sides disagree.
  • Copying a prefix from one model's card to another model.
  • Changing the prefix later without re-embedding, which acts like a model change.

Examples

Prefixes for an E5-style model

doc_vecs = model.encode(["passage: " + c for c in chunks])
q_vec = model.encode("query: how do I reset my password?")

The prefixes are part of the model's training. Dropping either one moves vectors off the space the model learned.

A hosted API call

client.embed(texts=chunks, model=MODEL, input_type="search_document")
client.embed(texts=[question], model=MODEL, input_type="search_query")

The parameter names differ by provider; the principle is the same.

Key points

  • Asymmetric models need queries and documents embedded in different modes.
  • The mode may be a text prefix, an instruction, an API input type or a library method.
  • Wrong or missing modes lower retrieval quality silently, without errors.
  • Use one shared function for each mode so indexing and querying cannot drift apart.
  • Changing the prefix or mode after indexing means the stored vectors must be rebuilt.

Common mistakes

  • Using one embedding helper for both queries and documents.
  • Adding the prefix on only one side of the pipeline.
  • Applying a prefix from a different model's card.
  • Changing the prefix after indexing without re-embedding.

Check yourself · 2 of 3

Which Sentence Transformers methods match the asymmetry between questions and passages?

Choose one answer

Check yourself · 3 of 3

A team changes the document prefix in code but keeps the existing index. What is the correct step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic