← All notes

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

Claims

Sources

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

Permanent ID: iil:note:e0fzef28

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.},
}

Markdown

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.

All artifact files (JSON index)

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.