Skip to content

Query transformations

A user's question is often a poor search query. It may be a follow-up ("and how much is it?"), use different words from the documents, or ask two things at once. A query transformation has the LLM write better queries first. Pagelore searches each one and fuses the result lists into a single ranking.

curl -X POST localhost:8000/api/v1/projects/1/retrieve -H 'Content-Type: application/json' \
     -d '{"query": "how do I wipe my device", "mode": "hybrid",
          "query_transform": {"kinds": ["multi_query"], "variants": 3}}'

Query transformations need LLM_PROVIDER (see LLM). Every kind you ask for is written in one LLM call per request, however many kinds and queries it produces.

Kinds

Kind What the LLM writes Searched in Use it for
rewrite One standalone, search-ready query. With history, it resolves references like "it" or "that one" Every index of the mode Chat follow-ups and vague or wordy questions. The rewrite replaces the query unless include_original is true
multi_query variants other phrasings (default 3, at most 10) Every index of the mode Questions that use different words from the documents
hyde hypothetical_documents short passages that could answer the query (default 1, at most 5) Vector index only Short or abstract questions, where a passage that looks like the answer matches better than the question. Embedded as documents, not as queries
decompose Up to max_sub_questions simple sub-questions (default 4, at most 10). None if the question is already simple Every index of the mode Questions that ask several things at once ("what does it cost and how long is the warranty?")
step_back One more general question about the background Every index of the mode Specific questions that need general context
expand Key terms with synonyms and related words, added to the query Keyword index only Keyword and hybrid search, where exact words matter. The expanded query always matches any of its terms, whatever keyword_match says

hyde needs the vector or hybrid mode, and expand the keyword or hybrid mode. Asking for them in the other mode is a 422.

Kinds combine freely:

{"query": "and how much is it?", "mode": "hybrid",
 "query_transform": {
   "kinds": ["rewrite", "multi_query", "expand"],
   "variants": 2,
   "history": [
     {"role": "user", "content": "Tell me about the X200 router"},
     {"role": "assistant", "content": "It is a dual-band router with two antennas."}
   ]}}

Options

Option Default
kinds One to six of the kinds above, without repeats
history none The conversation the query follows, oldest first: {"role": "user" \| "assistant", "content": ...}, at most 20 messages. The LLM reads the last 2000 characters of each message. Most useful with rewrite
variants 3 multi_query: how many phrasings (1 to 10)
hypothetical_documents 1 hyde: how many passages (1 to 5)
max_sub_questions 4 decompose: the most sub-questions (1 to 10)
include_original true, or false with rewrite Also search the query as given
fallback false If the LLM fails, search the query as given and add a warning, instead of failing with a 502

A transformation needs query: a request with only a vector is a 422. With both, the vector is used for the original query's search and the written queries are embedded.

How the results are combined

  1. One LLM call writes every requested kind as structured JSON. Pagelore cleans the queries (numbering, quotes, extra spaces), caps each kind at its count and drops repeats.
  2. Each query is searched on its own, in the indexes its kind uses, and ranked as usual for the mode. In hybrid mode its vector and keyword lists are fused first (see Hybrid search). score_threshold applies to each query's list, before fusion.
  3. The lists are fused with reciprocal rank fusion (RRF). Each query adds 1 / (rrf_k + rank) to a chunk's score, with equal weight for every query and rrf_k 60 by default. A chunk that several queries find ranks high.
  4. The rest is unchanged: parents, de-duplication, max_per_document, reranking, diversity and neighbors work on the fused list. Reranking scores against the primary query: the rewrite if there is one, else the query as given.

With a single search (only rewrite, for example), there is nothing to fuse. The results keep their usual scores and hybrid fields.

If the LLM returns no usable query, the query as given is searched, with a warning. The same happens if rewrite comes back empty.

The response

The response gains a query_transform block, and each result shows where each query ranked it:

{
  "query": "how do I wipe my device",
  "results": [{
    "rank": 1,
    "score": 0.0325,
    "file_name": "reset.txt",
    "text": "Hold the pin button for ten seconds to restore factory defaults.",
    "query_ranks": [{"query": 1, "rank": 1, "score": 0.71},
                    {"query": 2, "rank": 1, "score": 0.64}],
    ...
  }],
  "query_transform": {
    "kinds": ["multi_query"],
    "primary_query": "how do I wipe my device",
    "queries": [
      {"index": 0, "kind": "original",    "text": "how do I wipe my device",  "indexes": ["vector"]},
      {"index": 1, "kind": "multi_query", "text": "restore factory defaults", "indexes": ["vector"]},
      {"index": 2, "kind": "multi_query", "text": "pin button",               "indexes": ["vector"]}
    ],
    "provider": "OPENAI", "model": "gpt-4.1-mini",
    "input_tokens": 312, "output_tokens": 41, "attempts": 1, "took_ms": 840.2,
    "error": null
  },
  "stats": {"searches": 3, ...}
}
Field
query_transform.queries Every query searched, with its kind (original for the query as given), its text and the indexes it was searched in
query_transform.primary_query The query reranking used
query_transform.input_tokens, output_tokens, attempts, took_ms The LLM call's cost and time
query_transform.error With fallback, why the LLM call failed
results[].query_ranks With several searches: each query (by index) that found the result, its rank and score in that query's list. score is the fused RRF score
stats.searches With several searches: how many

vector_score / keyword_score and the other per-list hybrid fields are only set when there is a single search. With several, query_ranks takes their place.

Errors

Status When
409 query_transform with no LLM configured
422 Invalid options: no query, hyde in keyword mode, expand in vector mode, repeated kinds, counts out of range
502 The LLM failed (or replied with invalid JSON after its retries) and fallback is false

With fallback: true, a failed LLM call searches the query as given. The response has query_transform.error and a warning starting with "Query transformation failed".

Cost and latency

Each request makes one LLM call before searching, which usually adds a few hundred milliseconds to a few seconds. Each extra query is one more search (and one more embedding, batched with the others). To keep requests fast:

  • ask only for the kinds that help your questions, and measure with evaluation first;
  • use a small, fast model: the task is short and structured;
  • set fallback: true where a slower or failed LLM must not break search.

Evaluate it

Evaluation configs accept query_transform, so you can compare kinds on your own questions:

{"questions": [...], "top_k": 5,
 "configs": [
   {"name": "plain", "options": {"mode": "hybrid"}},
   {"name": "multi_query", "options": {"mode": "hybrid",
                                       "query_transform": {"kinds": ["multi_query"]}}},
   {"name": "hyde", "options": {"mode": "hybrid", "query_transform": {"kinds": ["hyde"]}}}
 ]}

Each config with a transformation reports llm_tokens ({"input", "output"}) summed over its questions, so you can weigh the gain against the cost. history is not accepted in evaluation configs, since each question stands alone.