Retrieval¶
Your app sends a question and gets back the most relevant passages, each with a ready-made citation. Pagelore does not write the answer: pass the passages to your own LLM.
curl -X POST localhost:8000/api/v1/projects/1/retrieve -H 'Content-Type: application/json' \
-d '{"query": "What is the refund policy?", "top_k": 5, "neighbors": 1}'
POST /api/v1/retrieve takes the same options plus project_ids, and ranks
the results of several projects together.
The pipeline¶
flowchart LR
Q["query"] --> VS["vector search"]
Q --> KS["BM25 keyword search"]
VS --> F["fuse"]
KS --> F
F --> T["score_threshold<br/>+ filters"]
T --> P["collapse into parents"]
P --> D["dedupe"]
D --> R["rerank"]
R --> M["MMR diversity<br/>+ max_per_document"]
M --> K["top_k"]
K --> N["neighbors + citations"]
Which steps run depends on the options: the vector mode skips the keyword
search, the keyword mode skips the vector search, and the later steps are
off unless asked for (except dedupe and return_parents, which are on).
Options¶
| Option | Default | |
|---|---|---|
query |
The search text, embedded with the collection's model. Required by the keyword and hybrid modes | |
vector |
Instead of query (vector and hybrid modes): a vector from the same model |
|
mode |
vector |
vector, keyword or hybrid; see Hybrid search |
top_k |
5 |
Results to return (1 to 100) |
score_threshold |
none | Drop results scoring below it. The scale depends on the mode |
filters |
none | See Filters |
neighbors |
0 |
Add context: each result with this many chunks before and after it, merged without repeating overlaps (0 to 10) |
diversity |
0 |
0 ranks by score only. Higher values trade relevance for variety (MMR, 0 to 1) |
max_per_document |
none | At most this many results from one document |
dedupe |
true |
Drop results whose text repeats a better one (ignoring case and spacing) |
return_parents |
true |
Return the parent of each match, for documents chunked with parent_chunk_size; see Small-to-big |
candidates |
derived | How many records to fetch from each list before filtering (at most 500) |
alpha, fusion, rrf_k |
0.5, rrf, 60 |
Hybrid fusion; see Hybrid search |
keyword_match |
any |
any or all (web search syntax); see Hybrid search |
rerank, rerank_candidates, rerank_threshold |
See Reranking | |
query_transform |
none | Have the LLM write better queries (rewrite, multi-query, HyDE, ...), search them all and fuse the results; see Query transformations |
graph |
none | Add the chunks a walk over the project's knowledge graph reaches from the query's entities and the best results; see Knowledge graph |
When candidates is not set, Pagelore fetches 2 x top_k (at least
top_k + 10), or 4 x top_k (at least 40) when results are filtered
afterwards (hybrid, parents, diversity, max_per_document, page filters),
and enough for the reranker's pool.
Filters¶
{
"query": "notice period",
"filters": {
"metadata": {"team": "hr", "year": {"$gte": 2024}},
"sources": ["upload", "s3"],
"file_extensions": ["pdf", "docx"],
"created_after": "2025-01-01T00:00:00Z",
"page_from": 1, "page_to": 20
}
}
| Filter | |
|---|---|
metadata |
Your metadata, with $eq $ne $in $nin $gt $gte $lt $lte $exists $and $or $not (Metadata) |
sources |
upload, text, url, s3 |
external_ids |
Documents with these keys |
document_ids / exclude_document_ids |
Only, or not, these documents |
file_extensions |
pdf, .docx, ... |
created_after / created_before |
Upload time |
page_from / page_to |
Page range; chunks without page numbers are left out |
Document filters are resolved in Postgres into document ids, and both the vector DB and the keyword index search only those.
To search only what one of your users may see, send user
({"id": "ana@acme.com", "groups": ["legal"]}). It is applied the same
way as the filters above (Document permissions).
The response¶
{
"query": "What is the refund policy?",
"mode": "vector",
"results": [{
"rank": 1,
"score": 0.83,
"chunk_id": 412, "project_id": 1, "document_id": 7, "chunk_index": 18,
"file_name": "terms.pdf",
"text": "Refunds are issued within 14 days ...",
"metadata": {"page_start": 3, "page_end": 4, "page_unit": "page",
"headings": ["Refunds", "Terms"]},
"citation": "terms.pdf, pages 3-4, Refunds > Terms",
"context": "... the chunk with its neighbours ...",
"context_chunks": [17, 19]
}],
"documents": [{"document_id": 7, "file_name": "terms.pdf", "hits": 1, "best_score": 0.83,
"metadata": {}, "source": "upload", "external_id": "terms.pdf", "version": 2}],
"stats": {"candidates": 20, "filtered": 0, "below_threshold": 0, "duplicate": 1,
"missing_chunk": 0, "took_ms": 24.1},
"warnings": []
}
| Result field | |
|---|---|
rank, score |
Position and score (the reranker's when reranked) |
text |
The chunk's text, or its parent's |
metadata |
Location: page_start / page_end / page_unit, titles, headings, start_time / end_time, row_start / row_end, ... |
citation |
The file name, then the page / slide / sheet range, the time range, the rows, and the heading path: report.pdf, pages 3-4, Refunds > Terms, call.mp3, 12:30-13:05, sales.xlsx, sheet 2, rows 41-80 |
context, context_chunks |
With neighbors: the passage around the result, and its chunk range |
parent_id, parent_index, matches |
With parents: the parent, and every matching chunk in it |
vector_score / vector_rank, keyword_score / keyword_rank |
Hybrid: the result's place in each list |
retrieval_score / retrieval_rank |
Reranked: the score and rank before reranking |
query_ranks |
With a query transformation that searched several queries: each query's rank and score for the result |
graph |
With graph: the result's rank in the graph's list and the search's, and the entities that tie it to the query |
documentslists the matched documents with their hit count, best score, metadata, source,external_idandversion.statscounts the candidates and why some were dropped, and the time taken.warningsmentions, for example, documents that are stale or still being indexed.
Errors¶
| Status | When |
|---|---|
| 404 | The project does not exist |
| 409 | The project's index does not match the configured vector DB or model (fix with reindex), rerank: true with no reranker, or query_transform with no LLM |
| 422 | Invalid options, for example the keyword mode without query |
| 502 | The embedding provider, reranker or LLM failed |
Using the results¶
import httpx
def retrieve(question: str) -> list[dict]:
r = httpx.post("http://localhost:8000/api/v1/projects/1/retrieve",
json={"query": question, "mode": "hybrid", "top_k": 5, "rerank": True})
r.raise_for_status()
return r.json()["results"]
hits = retrieve("How many vacation days do I get?")
sources = "\n\n".join(f"[{i}] {h['citation']}\n{h.get('context') or h['text']}"
for i, h in enumerate(hits, 1))
prompt = ("Answer the question using only these sources, and cite them as [n].\n\n"
f"{sources}\n\nQuestion: How many vacation days do I get?")
# send `prompt` to the LLM of your choice