reranker

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package reranker provides document re-ranking functionality for improving search quality.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilContext = errors.New("context cannot be nil")

ErrNilContext is returned when a nil context is passed to Rerank.

Functions

This section is empty.

Types

type Document

type Document struct {
	ID      string  // Unique identifier for the document
	Content string  // Text content to be re-ranked
	Score   float32 // Original similarity score from search
}

Document represents a searchable document with metadata and scores.

type Reranker

type Reranker interface {
	// Rerank re-ranks documents based on query relevance.
	// Takes a query string, list of documents, and desired top K results.
	// Returns re-ranked documents sorted by RerankerScore in descending order,
	// limited to topK results.
	//
	// The caller is responsible for ensuring ctx is not nil.
	Rerank(ctx context.Context, query string, docs []Document, topK int) ([]ScoredDocument, error)

	// Close closes the reranker and releases any resources.
	// Should be called when the reranker is no longer needed.
	Close() error
}

Reranker provides an interface for document re-ranking algorithms.

type ScoredDocument

type ScoredDocument struct {
	Document
	RerankerScore float32 // Score from re-ranker (0.0-1.0)
	OriginalRank  int     // Original rank position in results (0-indexed)
}

ScoredDocument represents a document with re-ranking scores.

type SimpleReranker

type SimpleReranker struct{}

SimpleReranker implements a simple TF-IDF based reranking algorithm. It calculates term overlap between the query and documents, then combines the original score with the overlap score to produce a final ranking.

func NewSimpleReranker

func NewSimpleReranker() *SimpleReranker

NewSimpleReranker creates a new SimpleReranker instance.

func (*SimpleReranker) Close

func (r *SimpleReranker) Close() error

Close closes the reranker. SimpleReranker has no resources to clean up.

func (*SimpleReranker) Rerank

func (r *SimpleReranker) Rerank(ctx context.Context, query string, docs []Document, topK int) ([]ScoredDocument, error)

Rerank re-ranks documents using a simple TF-IDF approach. The algorithm: 1. Tokenizes the query into lowercased terms 2. For each document, calculates term overlap with the query 3. Combines original score (50% weight) with overlap score (50% weight) 4. Sorts by combined score and returns top K results

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL