Skip to main content
Reranking allows you to refine search results using sophisticated neural models that consider the full context of both query and documents. After an initial retrieval stage, reranking reorders the top candidates for better relevance.

Overview

With reranking, you can:
  • Improve result quality after initial retrieval
  • Use state-of-the-art cross-encoder models
  • Consider multiple fields for ranking
  • Control the candidate pool size
  • Combine with any search technique (semantic, vector, keyword, hybrid)

Basic Reranking

Add a rerank stage after your search query:
import { Client } from "topk-js";
import { text, semanticIndex } from "topk-js/schema";
import { select, field, fn } from "topk-js/query";

const client = new Client({
  apiKey: "YOUR_API_KEY",
  region: "aws-us-east-1-elastica"
});

// Basic semantic search with reranking
const results = await client.collection("books").query(
  select({
    title: field("title"),
    similarity: fn.semanticSimilarity("title", "classic American novel")
  })
  .topk(field("similarity"), 50)  // Retrieve 50 candidates
  .rerank()  // Rerank with default model
  .limit(10)  // Return top 10
);
When you call rerank() without parameters, TopK automatically uses the query from your semantic similarity function and reranks based on indexed fields.

Reranking Options

Customize the reranking behavior:
import { select, field, fn } from "topk-js/query";

const results = await client.collection("books").query(
  select({
    title: field("title"),
    similarity: fn.semanticSimilarity("title", "dystopian novel")
  })
  .topk(field("similarity"), 50)
  .rerank({
    model: "cohere/rerank-v4",  // Specify reranking model
    query: "best dystopian novels with social commentary",  // Custom query
    fields: ["title", "summary"],  // Fields to consider
    topkMultiple: 3  // Consider 150 candidates (50 * 3)
  })
  .limit(10)
);

Rerank Parameters

  • model (optional): The reranking model to use (default: "cohere/rerank-v4")
  • query (optional): Custom query text for reranking. If not provided, TopK infers from semantic similarity queries
  • fields (optional): List of fields to include in reranking context
  • topkMultiple (optional): Multiplier for candidate pool size. If topk=50 and topkMultiple=2, reranking considers 100 documents
The topkMultiple parameter allows you to expand the candidate pool for reranking without returning all candidates. This improves reranking quality while keeping the final result set small.
Rerank results from vector search:
import { select, field, fn } from "topk-js/query";

const queryEmbedding = await generateEmbedding("machine learning tutorial");

const results = await client.collection("documents").query(
  select({
    title: field("title"),
    distance: fn.vectorDistance("embedding", queryEmbedding)
  })
  .topk(field("distance"), 100, true)  // Get 100 nearest neighbors
  .rerank({
    model: "cohere/rerank-v4",
    query: "comprehensive machine learning tutorial for beginners",
    fields: ["title", "content", "description"]
  })
  .limit(20)
);
Rerank BM25 results:
import { select, filter, match, field, fn } from "topk-js/query";

const results = await client.collection("books").query(
  select({
    title: field("title"),
    bm25_score: fn.bm25Score()
  })
  .filter(
    match("dystopian", { field: "summary" }) |
    match("totalitarian", { field: "summary" })
  )
  .topk(field("bm25_score"), 50)
  .rerank({
    model: "cohere/rerank-v4",
    query: "dystopian novels about totalitarian governments",
    fields: ["title", "summary"]
  })
  .limit(10)
);
Rerank after combining multiple search signals:
import { select, filter, match, field, fn } from "topk-js/query";

const results = await client.collection("books").query(
  select({
    title: field("title"),
    semantic_score: fn.semanticSimilarity("title", "artificial intelligence"),
    keyword_score: fn.bm25Score()
  })
  .filter(match("ai machine learning", { field: "summary" }))
  .topk(
    // Hybrid scoring
    field("semantic_score").mul(0.6).add(field("keyword_score").mul(0.4)),
    100
  )
  .rerank({
    model: "cohere/rerank-v4",
    query: "introduction to artificial intelligence and machine learning",
    fields: ["title", "summary", "content"],
    topkMultiple: 2  // Consider 200 documents
  })
  .limit(15)
);

Multiple Semantic Fields

When using semantic similarity on multiple fields, you must explicitly specify the reranking query:
import { select, field, fn } from "topk-js/query";

// This requires explicit rerank query
const results = await client.collection("books").query(
  select({
    title: field("title"),
    title_sim: fn.semanticSimilarity("title", "classic literature"),
    summary_sim: fn.semanticSimilarity("summary", "American authors")
  })
  .topk(field("title_sim").add(field("summary_sim")), 50)
  .rerank({
    model: "cohere/rerank-v4",
    query: "classic American literature",  // Required when using multiple semantic fields
    fields: ["title", "summary"]
  })
  .limit(10)
);
When your query uses multiple semanticSimilarity() functions with different queries, you must provide an explicit query parameter to rerank(), otherwise an error will be raised.

Reranking Models

TopK supports Cohere’s reranking models:
  • cohere/rerank-v4 (default) - Latest and most accurate
  • Custom models may be available depending on your plan
// Use default model
.rerank()

// Specify model explicitly
.rerank({ model: "cohere/rerank-v4" })

Best Practices

Candidate Pool Size

Retrieve more candidates than your final result set:
  • Minimum: 5-10x your final result size
  • Recommended: 10-20x for best quality
  • Maximum: Balance quality with latency and cost
// Good: Retrieve 100 candidates, return 10 results
.topk(field("score"), 100)
.rerank()
.limit(10)

// Better: Expand to 200 candidates with topkMultiple
.topk(field("score"), 100)
.rerank({ topkMultiple: 2 })
.limit(10)

Field Selection

Include relevant fields for context:
  • Include the main content fields (title, description, body)
  • Exclude very long fields that might dilute relevance
  • Consider the query context when selecting fields
// Good: Select relevant fields
.rerank({
  query: "machine learning tutorial",
  fields: ["title", "summary", "introduction"]
})

// Avoid: Too many or irrelevant fields
.rerank({
  query: "machine learning tutorial",
  fields: ["title", "summary", "full_text", "metadata", "id"]
})

Query Formulation

Write descriptive queries for reranking:
  • Use natural, descriptive language
  • Include context and intent
  • Expand abbreviations and acronyms
// Good: Descriptive query
.rerank({
  query: "comprehensive beginner-friendly machine learning tutorial with code examples"
})

// Less effective: Terse query
.rerank({
  query: "ml tutorial"
})
Reranking is most effective when the initial retrieval stage has high recall (finds most relevant documents) but may have imperfect precision. The reranker can then refine the ordering.

Performance Considerations

  • Latency: Reranking adds latency proportional to the number of candidates
  • Cost: Reranking may incur additional costs based on your plan
  • Quality: More candidates generally means better results, but with diminishing returns
Start with a moderate candidate pool (50-100 documents) and measure the impact on both latency and result quality before scaling up.

Build docs developers (and LLMs) love