search

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScaleBM25 = "bm25"
	ScaleRRF  = "rrf"
)

Score scales. BM25 magnitudes are raw and corpus-dependent; RRF scores are bounded near 1/rrf_k. They share a JSON key, so each result names its own.

View Source
const (
	HighlightOpen  = "\x02"
	HighlightClose = "\x03"
)

Highlight markers wrap matched terms in FTS5 snippets. They are sentinels, not markup: the output layer turns them into ANSI or strips them. Using <b> here leaked HTML into JSON and into the terminal.

Variables

This section is empty.

Functions

This section is empty.

Types

type BM25

type BM25 struct {
	// contains filtered or unexported fields
}

BM25 runs a full-text search using SQLite FTS5's built-in BM25 ranking.

func NewBM25

func NewBM25(database *db.DB) *BM25

func (*BM25) Search

func (b *BM25) Search(ctx context.Context, opts SearchOpts) ([]Result, error)

Search returns up to topK results ranked by BM25.

type Hybrid

type Hybrid struct {
	// contains filtered or unexported fields
}

Hybrid orchestrates BM25 + vector search + RRF fusion.

func NewHybrid

func NewHybrid(bm25 *BM25, vector *VectorSearch, embedding providers.EmbeddingProvider, cfg config.SearchConfig) *Hybrid

func (*Hybrid) Search

func (h *Hybrid) Search(ctx context.Context, opts SearchOpts) ([]Result, error)

Search runs BM25 and (optionally) vector search, then fuses with RRF.

type Result

type Result struct {
	DocID       int64   `json:"doc_id"`
	ChunkID     int64   `json:"chunk_id"`
	Collection  string  `json:"collection"`
	Path        string  `json:"path"`
	Title       string  `json:"title"`
	HeadingPath string  `json:"heading_path"`
	Snippet     string  `json:"snippet"`
	Timestamp   string  `json:"timestamp"`
	Score       float64 `json:"score"`
	// Scale names the unit of Score. BM25 magnitudes and RRF scores differ by
	// two orders of magnitude and are not comparable across commands.
	Scale   string        `json:"scale"`
	Explain *ScoreExplain `json:"explain,omitempty"`
}

Result is a single search hit.

func Finalize

func Finalize(results []Result, opts SearchOpts) []Result

Finalize applies the ordering and trimming every command needs after retrieval: date sort over the whole candidate pool (never after truncation, or "newest" would only mean "newest of the most relevant"), duplicate and per-document capping, then the caller's limit.

func ReciprocalRankFusion

func ReciprocalRankFusion(bm25 []Result, vec []Result, k int) []Result

ReciprocalRankFusion merges BM25 and vector result lists using RRF. k is the rank constant (default 60 per the paper). Returns results sorted by descending RRF score.

type ScoreExplain

type ScoreExplain struct {
	BM25Score   float64 `json:"bm25_score,omitempty"`
	BM25Rank    int     `json:"bm25_rank,omitempty"`
	VectorDist  float64 `json:"vector_distance,omitempty"`
	VectorRank  int     `json:"vector_rank,omitempty"`
	RRFScore    float64 `json:"rrf_score,omitempty"`
	RerankScore float64 `json:"rerank_score,omitempty"`
}

ScoreExplain breaks down how a score was computed.

type SearchOpts

type SearchOpts struct {
	Query      string
	Collection string // empty = all collections
	TopK       int
	Pool       int    // candidates to retrieve before dedupe/cap; defaults to TopK
	Mode       string // lexical | hybrid | deep
	Explain    bool
	Since      string // YYYY-MM-DD, inclusive lower bound on document timestamp
	Until      string // YYYY-MM-DD, inclusive upper bound
	Sort       string // "" = relevance, "date" = newest first
}

SearchOpts configures a search operation.

type VectorSearch

type VectorSearch struct {
	// contains filtered or unexported fields
}

VectorSearch performs KNN search using pure Go cosine similarity. Embeddings are loaded from the DB and compared in memory. For large corpora, a dedicated vector index (sqlite-vec, etc.) is preferred.

func NewVectorSearch

func NewVectorSearch(database *db.DB, fingerprint string) *VectorSearch

func (*VectorSearch) Search

func (v *VectorSearch) Search(ctx context.Context, queryEmbedding []float32, topK int, opts SearchOpts) ([]Result, error)

Search returns up to topK results nearest to the query embedding.

Jump to

Keyboard shortcuts

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