search

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package search provides BM25-based search for the index package.

Package search provides BM25-based search implementations for the index package.

It exists to:

  • Keep index small and dependency-light
  • Enable stronger ranking strategies without forcing heavier search dependencies on every consumer

Usage

The primary type is BM25Searcher, which implements index.Searcher:

idx := index.NewInMemoryIndex(index.IndexOptions{
    Searcher: search.NewBM25Searcher(search.BM25Config{}),
})

Configuration

BM25Config allows customization of field boosts and safety limits:

cfg := search.BM25Config{
    NameBoost:      3,   // Boost name matches (default: 3)
    NamespaceBoost: 2,   // Boost namespace matches (default: 2)
    TagsBoost:      2,   // Boost tag matches (default: 2)
    MaxDocs:        1000, // Limit documents to index (0 = unlimited)
    MaxDocTextLen:  5000, // Truncate long descriptions (0 = unlimited)
}

Thread Safety

BM25Searcher is safe for concurrent use. It uses an internal RWMutex to protect index state and efficiently caches the Bleve index based on document fingerprints, only rebuilding when the document set changes.

Behavior

Empty queries return the first N documents (matching index's default behavior). Non-empty queries use BM25 ranking with deterministic tie-breaking (score DESC, then ID ASC).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BM25Config

type BM25Config struct {
	// Field weighting via token duplication.
	NameBoost      int // default 3
	NamespaceBoost int // default 2
	TagsBoost      int // default 2

	// Safety / performance controls.
	MaxDocs       int // 0 = unlimited
	MaxDocTextLen int // 0 = unlimited
}

BM25Config configures the BM25 searcher behavior.

type BM25Searcher

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

BM25Searcher implements index.Searcher using BM25 ranking.

func NewBM25Searcher

func NewBM25Searcher(cfg BM25Config) *BM25Searcher

NewBM25Searcher creates a new BM25-based searcher with the given config. Zero values in config are replaced with sensible defaults.

func (*BM25Searcher) Close

func (s *BM25Searcher) Close() error

Close releases resources held by the searcher.

func (*BM25Searcher) Deterministic

func (s *BM25Searcher) Deterministic() bool

Deterministic reports whether this searcher returns stable ordering.

func (*BM25Searcher) IndexBuildCount

func (s *BM25Searcher) IndexBuildCount() int

IndexBuildCount returns the number of times the index has been built. This is useful for testing cache behavior.

func (*BM25Searcher) Search

func (s *BM25Searcher) Search(query string, limit int, docs []index.SearchDoc) ([]index.Summary, error)

Search performs a BM25-ranked search over the provided documents.

Jump to

Keyboard shortcuts

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