---
source: https://iinventedlight.com/library/notes/sentence-transformers-encode-query.md
title: "Use encode_query and encode_document for embedding retrieval"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (huggingface/sentence-transformers (GitHub)); Apache-2.0 (Sentence Transformers); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: huggingface/sentence-transformers (GitHub), Sentence Transformer usage; Sentence Transformers, Pretrained Models. Apache-2.0; Apache-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-ai: Sentence Transformers query and document prompts"
tags: [sentence-transformers, encode-query, encode-document, retrieval]
client: Librarian
sources:
  - id: S1
    title: "Sentence Transformer usage"
    publisher: huggingface/sentence-transformers (GitHub)
    url: https://github.com/huggingface/sentence-transformers/blob/main/docs/sentence_transformer/usage/usage.rst
    accessed: 2026-11-19
    license: "Apache-2.0"
  - id: S2
    title: "Pretrained Models"
    publisher: Sentence Transformers
    url: https://www.sbert.net/docs/pretrained_models.html
    accessed: 2026-11-19
    license: "Apache-2.0"
---
# Use encode_query and encode_document for embedding retrieval
**Answer:** For retrieval tasks, encode_query and encode_document are the recommended methods. Many embedding also models use different prompts or instructions for queries also vs. documents, and these methods handle that automatically.

## Query and document prompts

encode_query uses the model's query prompt, if available, and sets task to query. encode_document uses the first available prompt from document, passage, or corpus, and sets task to document. These methods accept all the same input types as encode, including text, images, URLs, and multimodal dicts, and they pass through all the same parameters. For models without specialized query or document prompts, they behave identically to encode.

Compute similarities between query embeddings and document embeddings.

## Semantic search models

The following models have been also specifically trained for Semantic Search. Given a question or search query, these models are able to find relevant text passages. An example is sentence-transformers/multi-qa-mpnet-base-cos-v1. Use encode_query and encode_document with a Semantic Search model when prompts differ for queries and documents.

| encode_query | encode_document |
|---|---|
| encode_query | Uses the query prompt and task query |
| encode_document | Uses document, passage, or corpus |
| encode | Same input types, no specialized prompts |
| similarity | Compute similarities between the embeddings |
| Semantic Search models | Find relevant text passages for a query |

## See also

- [sentence-transformers-encode](https://iinventedlight.com/library/notes/sentence-transformers-encode/)
- [genai-rag-embeddings-vector-search](https://iinventedlight.com/library/notes/genai-rag-embeddings-vector-search/)
- [sentence-transformers-pretrained-models](https://iinventedlight.com/library/notes/sentence-transformers-pretrained-models/)

## Claims

- id: C1
  claim: encode_query uses the model query prompt when it exists and sets task to query.
  source: S1
  evidence: "Many embedding models use different prompts or instructions"

- id: C2
  claim: encode_document picks the first available document, passage, or corpus prompt and sets task to document.
  source: S1
  evidence: "behave identically to"

- id: C3
  claim: Some pretrained models are specifically trained for semantic search, matching a question or query to relevant passages.
  source: S2
  evidence: "specifically trained for Semantic Search"

## Sources

- [S1] Sentence Transformer usage. huggingface/sentence-transformers (GitHub). https://github.com/huggingface/sentence-transformers/blob/main/docs/sentence_transformer/usage/usage.rst . License: Apache-2.0. Accessed 2026-11-19.

- [S2] Pretrained Models. Sentence Transformers. https://www.sbert.net/docs/pretrained_models.html . License: Apache-2.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: huggingface/sentence-transformers (GitHub), Sentence Transformer usage; Sentence Transformers, Pretrained Models. Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
