# Choosing a ranking strategy

A search ranks by evidence: the words a document shares with the query, how close it is in meaning,
and what you know about it, such as its age or popularity. WarpSearch gives you several ways to
combine that evidence. No method is best for every collection. This guide explains what each one
does well and badly, so you can pick a starting point and then measure.

The request syntax for each method is in [Hybrid search](hybrid-search.md),
[Scoring expressions](scoring-expressions.md) and
[Ranking with business signals](ranking-with-business-signals.md).

## The signals

| Signal | Examples | Carried by |
|---|---|---|
| Lexical | Shared words, phrases, exact identifiers | `match`, `multi_match`, phrase and term queries, scored with BM25 |
| Semantic | Paraphrases, questions, related concepts | `knn` over a vector field, scored by similarity |
| Business | Recency, popularity, rating, distance, stock | Scoring expressions, `function_score`, sorted fusion sources, `sort` |
| Constraints | Tenant, permissions, category, availability | `filters`, which never change scores |

Keep constraints out of the score. A rule that must hold belongs in a filter, which applies inside
the vector search too, so neighbors are chosen among eligible documents only. Boosting what should
be excluded only makes it less likely to appear. See [Filters versus query](searching.md#filters-versus-query).

## Pick a method

| Situation | Start with |
|---|---|
| A new hybrid search, with no judged queries yet | RRF, `rank_constant` 60, `rank_window_size` 100, equal weights. |
| You have a judgment list | Linear fusion, with the weight `α` tuned on that list. |
| Score scales are unknown or change a lot between queries | RRF. |
| One source is ordered by a field, such as date | RRF; linear fusion needs scores. |
| Either keywords or meaning alone is good evidence | `dis_max` over a `knn` and a `match`. |
| Results must match the keywords and be semantically close | A `bool` with both under `must`. |
| Keywords decide what is eligible, meaning decides the order | A `knn` with the keyword query as its filter. |
| One query, no fusion, and boosts you can tune | Summed scores: a `knn` section beside the query. |
| Business signals should move results | A [scoring expression](scoring-expressions.md) or `function_score`. |
| Many hits from one entity crowd the page | `collapse` on the entity's id, on any search including a fusion ([One result per entity](hybrid-search.md#one-result-per-entity)). |
| Vector neighbors cluster in a few entities | `"unit": "groups"` on the `knn`, with `collapse` ([Nearest groups](vector-search.md#nearest-groups)). |
| A few values (brands, sources) should not fill the page, but several hits each are fine | `diversify` with `max_per_key` ([Limit hits per value](searching.md#limit-hits-per-value)). |
| Vector results should also reflect popularity or recency | `function_score` around the `knn` query ([Around a vector search](scoring-expressions.md#around-a-vector-search)). |

Then change one thing at a time and measure, as described in [Tune it](#tune-it).

## Rank fusion or score fusion

RRF and linear fusion both run each source on its own and merge the lists. They differ in what
they keep from each list.

RRF keeps only positions: a document gets `weight / (60 + rank)` from each source. Linear fusion
rescales each source's scores to 0 to 1 and adds them, so it keeps how far ahead a document was.
Take four documents found by both sources:

| Document | BM25 | Cosine | RRF (k = 60) | Linear, 0.5 / 0.5 |
|---|---|---|---|---|
| A | 25 | 0.72 | 0.03227 (2nd) | 0.667 (1st) |
| B | 10 | 0.96 | 0.03252 (1st) | 0.559 (2nd) |
| C | 9 | 0.93 | 0.03200 (3rd) | 0.488 (3rd) |
| D | 8 | 0.60 | 0.03125 (4th) | 0 (4th) |

A's keyword score is more than double anyone else's. RRF sees only that A ranked first for keywords
and third for meaning, and puts B ahead. Linear fusion sees the size of A's lead and keeps it first.
Which is right depends on your data. When a big BM25 lead means a real exact match, such as a
product code or a name, linear fusion uses that. When BM25 scores swing for reasons unrelated to
relevance, such as very short fields, RRF ignores the noise.

Published comparisons point the same way. RRF was introduced as a robust default that beats its
inputs without tuning ([Cormack, Clarke and Büttcher, 2009](https://doi.org/10.1145/1571941.1572114)),
and a tuned convex combination of normalized scores has since been shown to beat RRF, needing only
a small set of judged queries to tune
([Bruch, Gai and Ingber, 2023](https://arxiv.org/abs/2210.11934)). In practice:

- Start with RRF. It needs no tuning and rarely fails badly.
- Once you have judged queries, try linear fusion and tune `α`. Keep it only if it measures better.
- Min-max rescaling is sensitive to the window: an outlier at the top or bottom of a source changes
  every other document's normalized score. Retune after changing `rank_window_size` or `k`.

## Summed scores and dis_max

A `knn` section beside a query, a `bool` with a `knn` clause, and `dis_max` all score in one pass,
with no fusion step. They add or compare raw scores, so the boosts do the job that weights and
normalization do in a fusion. A BM25 score is often 5 to 20 and a cosine similarity at most 1, so
without boosts the keyword side decides.

They are cheaper and simpler than fusion, and they make sense when boosts can be tuned once and
hold across your queries. If you find yourself wanting a different boost for every query, use a
fusion instead.

`dis_max` keeps the best clause plus `tie_breaker` times the rest. It suits queries where a strong
keyword match or a strong semantic match should each be enough on its own, without a reward for
matching both weakly.

## Candidate windows

Every method that runs sources separately sees only what each source returned. A document that is
good on both signals but top on neither can be missed entirely. With equal weights on normalized
scores:

| Document | Keyword | Vector | Combined |
|---|---|---|---|
| A | 1.00 | 0.10 | 0.55 |
| B | 0.10 | 1.00 | 0.55 |
| C | 0.85 | 0.85 | 0.85 |

If each source returns only its top 1, the keyword source returns A, the vector source B, and C,
the best document, is never considered. Larger windows make this much less likely, without ruling it
out:

- `rank_window_size` sets how many documents each source contributes to a fusion. 100 is a good
  default; raise it for deep pages or when relevant documents sit just outside it.
- `k` and `num_candidates` set how far a vector search looks, and `exact: true` removes approximation
  as a cause of misses.
- Run the same request with a larger window, or with `"exact": true`, and compare the top results.
  If they barely change, the window is large enough. See
  [Measure recall](hybrid-search.md#measure-recall).

The same applies to a `knn` clause in summed scores: only its `k` nearest documents get a vector
score.

## Tune it

1. Build a [judgment list](tuning-relevance.md#offline-evaluation-with-judgment-lists) of 50 to 200
   real queries with graded results. Include identifier lookups, short keyword queries and long
   questions, since they favor different methods.
2. Measure the starting setup with nDCG at 10, or recall at 50 if the results feed a reranker or a
   language model.
3. Change one thing: the method, a weight, `rank_constant`, or a window. Re-run the whole list.
4. Look at the queries that got worse, not only the average. A change that helps questions often
   hurts part numbers.
5. Hold back a fifth of the queries and check the final setup on them.

Your application can also pick the setup per query. A query that looks like an identifier (digits,
dashes, no spaces) can set the vector weight to 0, which turns that source off without changing
the request's shape.

## Recipes

**Product search.** Shoppers type names, brands and codes, where exact matches matter, and
descriptions, where meaning helps. Use RRF with keyword weight 2 and vector weight 1, filters for
category and stock, and a `function_score` in the keyword source for popularity (see
[A boosted source](hybrid-search.md#a-boosted-source)). Send identifier-like queries with the vector weight at 0.

**Questions over documents (RAG).** Questions are worded differently from the answers, so meaning
leads. Use linear fusion with weights near 0.3 for keywords and 0.7 for vectors, tuned on judged
questions, and a large window, since the results feed a model. When documents are split into chunks
and one document's chunks crowd the page, collapse the fused list on the parent id, and set
`"unit": "groups"` on the `knn` retriever so its `k` counts parents. See
[One result per entity](hybrid-search.md#one-result-per-entity) and
[Multi-vector fields](multi-vector-fields.md).

**Support and knowledge bases.** Error messages and product terms must appear in an answer, and the
best explanation should come first. Put the required terms in the `knn` filter, which ranks the
matching articles by meaning. See
[Vector search within keyword matches](hybrid-search.md#vector-search-within-keyword-matches).

## Rerankers

Fusion combines the scores your sources already produce. A reranker model reads the query and each
candidate again and produces a new judgment, which can catch what both sources missed. WarpSearch
does not host reranker models. Retrieve the top 50 to 100 fused results and rerank them in your
application; see [Reranking](hybrid-search.md#reranking).

## Where to go next

1. [Hybrid search](hybrid-search.md) for the request syntax of each method.
2. [Ranking with business signals](ranking-with-business-signals.md) for recency, popularity and
   distance.
3. [Tuning relevance](tuning-relevance.md) for field weights and judgment lists.
