Top-k Search

Choosing k and a Minimum Score

  • k = how many results to return. For RAG, 3–10 chunks is typical: too few misses information, too many adds noise and cost.
  • A minimum score drops weak matches, so you can answer "nothing relevant found" instead of passing junk to the model.
  • For large collections, use heapq.nlargest (or the database) instead of sorting everything.

Top-k with a Threshold

Example

Scores here are sample similarity values already computed for a query:

import heapq

scores = {
    "refund-policy": 0.82, "cancel-order": 0.64, "delivery-times": 0.41,
    "size-chart": 0.12, "cod-limits": 0.37, "warranty": 0.58,
}

def top_k(scores, k=3, min_score=0.0):
    best = heapq.nlargest(k, scores.items(), key=lambda kv: kv[1])
    return [(doc, s) for doc, s in best if s >= min_score]

print("k=3             :", top_k(scores, 3))
print("k=5, min 0.5    :", top_k(scores, 5, min_score=0.5))
print("k=3, min 0.9    :", top_k(scores, 3, min_score=0.9) or "nothing relevant found")

Output

k=3             : [('refund-policy', 0.82), ('cancel-order', 0.64), ('warranty', 0.58)]
k=5, min 0.5    : [('refund-policy', 0.82), ('cancel-order', 0.64), ('warranty', 0.58)]
k=3, min 0.9    : nothing relevant found