bm25

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package bm25 provides BM25 (Best Matching 25) text search implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// K1 controls term frequency saturation (default: 1.5).
	// Higher values give more weight to term frequency.
	K1 float64

	// B controls document length normalization (default: 0.75).
	// B=0 disables normalization, B=1 fully normalizes.
	B float64
}

Config configures the BM25 index.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default BM25 configuration.

type Document

type Document struct {
	// ID is the unique document identifier.
	ID string
	// Content is the document text.
	Content string
	// Metadata contains additional document metadata.
	Metadata map[string]string
}

Document represents a document in the index.

type Index

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

Index implements a BM25 index for text search.

func New

func New(cfg Config) *Index

New creates a new BM25 index with the given configuration.

func (*Index) Count

func (idx *Index) Count() int

Count returns the number of documents in the index.

func (*Index) Delete

func (idx *Index) Delete(ctx context.Context, id string) error

Delete removes a document from the index.

func (*Index) Get

func (idx *Index) Get(ctx context.Context, id string) (*Document, bool)

Get retrieves a document by ID.

func (*Index) Insert

func (idx *Index) Insert(ctx context.Context, doc Document) error

Insert adds a document to the index.

func (*Index) InsertBatch

func (idx *Index) InsertBatch(ctx context.Context, docs []Document) error

InsertBatch adds multiple documents to the index.

func (*Index) Search

func (idx *Index) Search(ctx context.Context, query string, k int) ([]ScoredDocument, error)

Search performs a BM25 search and returns the top k results.

func (*Index) SearchWithFilters

func (idx *Index) SearchWithFilters(ctx context.Context, query string, k int, filters map[string]string) ([]ScoredDocument, error)

SearchWithFilters performs a BM25 search with optional metadata filters.

type ScoredDocument

type ScoredDocument struct {
	Document Document
	Score    float64
}

ScoredDocument represents a document with its BM25 score.

Jump to

Keyboard shortcuts

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