Skip to content

Hybrid search

Embeddings find chunks that mean the same thing in other words, but they are weak on exact tokens: names, error codes, part numbers and rare terms. Keyword search is the opposite. Pagelore has both, and can fuse them.

curl -X POST localhost:8000/api/v1/projects/1/retrieve -H 'Content-Type: application/json' \
     -d '{"query": "error XK-2041 license server", "mode": "hybrid", "alpha": 0.5}'
mode Uses Good for
vector Embeddings Questions in natural language, paraphrases
keyword BM25 over Postgres full-text search Exact names, codes, rare terms
hybrid Both, fused Most real queries: the safest default once you have measured it

Keyword search (BM25)

Every chunk gets a Postgres full-text vector (a tsvector with a GIN index), built when the chunk is stored, from its text and its LLM-written context.

The keyword mode ranks the chunks that contain the query words with Okapi BM25 (k1 1.2, b 0.75):

  • words that are rare in the project weigh more;
  • repeats in a long chunk weigh less than in a short one.

Postgres' own ts_rank has no document frequency, so Pagelore computes BM25 in SQL from the matching chunks' vectors. Words are stemmed and stop words dropped according to the project's language. stats.keyword_terms shows what was searched.

Matching

keyword_match
any (default) Chunks with any query word, best first
all Web search syntax: every word must appear, "quoted phrases" must appear in order, -word excludes a word, and or gives alternatives
{"query": "\"notice period\" contractor -draft", "mode": "keyword", "keyword_match": "all"}

Language

A project's text_search_language (default TEXT_SEARCH_LANGUAGE, english) picks the stemmer and stop words. Set it when you create the project, or PATCH it later, which rebuilds the project's keyword index.

  • Use simple for no stemming, or for languages Postgres has no stemmer for.
  • GET /api/v1/data/text-search-languages lists the choices (english, arabic, german, french, spanish, ... on a standard install).
  • Projects with different languages can be searched together; each is searched with its own language.

Fusion

The hybrid mode runs both searches and fuses the two lists.

{"mode": "hybrid", "fusion": "rrf", "alpha": 0.5, "rrf_k": 60}

Each list adds weight / (rrf_k + rank) to a chunk's score, with the vector list's weight alpha and the keyword list's 1 - alpha. It uses ranks only, so the score scales of the two lists don't matter. A larger rrf_k (1 to 1000) flattens the difference between top and lower ranks.

{"mode": "hybrid", "fusion": "relative_score", "alpha": 0.7}

Each list's scores are scaled to 0-1 (min-max), then added with the weights. It keeps how much better the first result is than the next, which RRF throws away.

alpha is 1 for vectors only and 0 for keywords only. Each hybrid result reports its vector_score / vector_rank and keyword_score / keyword_rank; a field is absent when the chunk was not in that list.

Notes

  • Filters, dedupe, neighbors, max_per_document, parents and reranking work in every mode.
  • diversity uses the embeddings in the vector mode and word overlap (Jaccard) in the others.
  • The keyword index lives in Postgres and always matches the chunks, so the keyword mode keeps working while the vector index needs a reindex.
  • score_threshold uses each mode's own scale: the vector DB's similarity, BM25 scores, or the fused score. BM25 scores can't be compared across projects with different languages, or with vector scores; to mix them, use fusion, not score_threshold.
  • Hybrid also takes vector instead of embedding query; the keyword search still uses query.