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
- genai-rag-embeddings-vector-search
- 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.
License: CC-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.
Cite this
iinventedlight. 2026. "Use encode_query and encode_document for embedding retrieval." iinventedlight Library, iil:note:e0fzef28, version of 2026-11-19. https://iinventedlight.com/library/notes/sentence-transformers-encode-query/. License: CC-BY-4.0.
@misc{iil_note_e0fzef28,
author = {{iinventedlight}},
title = {{Use encode\_query and encode\_document for embedding retrieval}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/sentence-transformers-encode-query/},
note = {Permanent ID iil:note:e0fzef28. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- sentence-transformers-encode-query.table.json (table; CC-BY-4.0)
- sentence-transformers-encode-query.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/sentence-transformers-encode-query.md.