Retrieval API (POST /api/v1/retrieval)#
POST /api/v1/retrieval retrieves ranked document chunks from one or more datasets without triggering LLM completion. This makes it the right entry point for conditional RAG workflows (retrieve → inspect → decide whether to generate), custom reranking pipelines, or any external tooling that needs raw retrieval results.
The handler is retrieval_test() in api/apps/restful_apis/chunk_api.py. Authentication uses a Bearer API key; the caller must own every dataset in dataset_ids. All datasets in a single request must share the same embedding model — mismatches are rejected with a DATA_ERROR .
Request Parameters#
POST /api/v1/retrieval
Content-Type: application/json
Authorization: Bearer <YOUR_API_KEY>
Required
| Parameter | Type | Notes |
|---|---|---|
question | string | Query text. Empty string returns {total:0, chunks:[], doc_aggs:{}} immediately . |
dataset_ids | list[string] | Datasets to search. Required unless document_ids is provided . |
Retrieval tuning
| Parameter | Default | Notes |
|---|---|---|
similarity_threshold | 0.2 | Minimum composite score; chunks below this are dropped . |
vector_similarity_weight | 0.3 | KNN weight; (1 − x) is the BM25/term weight . |
top_k | 1024 | Candidate pool size for vector computation . |
rerank_id | null | Model ID for an optional cross-encoder reranker . |
Filtering
| Parameter | Type | Notes |
|---|---|---|
document_ids | list[string] | Restrict search to specific documents within the specified datasets . |
metadata_condition | object | Filter chunks by document metadata. Supports "and"/"or" logic with operators: contains, not contains, start with, empty, not empty, =, ≠, >, <, ≥, ≤ . |
Enrichment flags
| Parameter | Default | Notes |
|---|---|---|
keyword | false | Use the tenant's default chat LLM to extract keywords from question and append them . |
highlight | false | Return matched term highlights in each chunk . |
cross_languages | [] | Translate question into additional languages before retrieval . |
use_kg | false | Prepend a knowledge-graph chunk for multi-hop queries . |
toc_enhance | false | Re-rank results using extracted table-of-contents chunks . |
page / page_size | 1 / 30 | Pagination over ranked results . |
Full parameter documentation is in the HTTP API reference under Retrieve chunks.
Response Format#
{
"code": 0,
"data": {
"total": 4,
"chunks": [
{
"id": "<chunk_id>",
"content": "...",
"document_id": "<doc_id>",
"document_keyword": "<filename>",
"similarity": 0.82,
"term_similarity": 0.75,
"vector_similarity": 0.91,
"important_keywords": ["rag", "retrieval"],
"highlight": "...<em>rag</em>...",
"positions": [[1, 120, 40, 200, 60]],
"image_id": "",
"tag_kwd": []
}
],
"doc_aggs": [
{ "doc_id": "...", "doc_name": "paper.pdf", "count": 3 }
]
}
}
Key fields :
similarity— composite score blendingterm_similarityandvector_similarityat the configured weight.doc_aggs— per-document chunk counts, useful for building attribution UIs.positions— bounding-box coordinates[page, x0, y0, x1, y1]for PDF highlighting.- Vectors are stripped before the response is sent ; internal field names are remapped (e.g.
content_with_weight→content,chunk_id→id) .
Internal Pipeline#
The handler delegates to settings.retriever.retrieval(), which maps to Dealer.retrieval() in rag/nlp/search.py — the full hybrid-search pipeline with BM25 + KNN scoring and reranking .
Call sequence inside retrieval_test():
- Auth + model resolution — Validates dataset ownership; resolves embedding model and optional rerank model from tenant config .
- Cross-language expansion (if
cross_languages) — Translatesquestionviacross_languages(). - Keyword extraction (if
keyword=true) — Calls the tenant's chat LLM viakeyword_extraction()and appends the result toquestion. - Metadata pre-filter (if
metadata_conditionand no explicitdocument_ids) — Resolves matchingdoc_idsviaDocMetadataServicebefore the vector search . - Core retrieval —
Dealer.retrieval()executes hybrid BM25+KNN search, post-retrieval reranking, and pagination . - ToC enhancement (if
toc_enhance) — Re-ranks with table-of-contents chunks viaretrieval_by_toc(). - Child chunk expansion —
retrieval_by_children()replaces parent summary chunks with their children . - Knowledge-graph injection (if
use_kg) — Prepends a KG-synthesized chunk at index 0 .
See Hybrid Search and Retrieval for scoring details (BM25/KNN weights, reranking paths, similarity threshold behavior).
Consumers and Integrations#
MCP server — The MCP retrieval() tool posts directly to /api/v1/retrieval. When dataset_ids is omitted, it auto-resolves all accessible datasets via resolve_dataset_ids(). Results are enriched with a two-level TTL cache (32-entry LRU, ~300 s TTL) for dataset and document metadata; use force_refresh: true to bypass it .
Dify integration — A separate Dify-compatible endpoint at POST /api/v1/dify/retrieval mirrors the same retrieval pipeline but uses Dify's request schema (knowledge_id, retrieval_setting.top_k, etc.) rather than RAGFlow's native schema.
SDK / direct HTTP — Any HTTP client can call this endpoint independently of the chat API. This is the standard pattern for building retrieval-only tools, evaluation harnesses, or agent components that need to conditionally decide whether to generate based on retrieved context.