search

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package search runs the retrieval queries defined in queries/retrieval.sql against the store. Milestone 2 only wires up keyword (FTS) search; SearchHybrid needs the embedding worker (milestone 5).

Index

Constants

View Source
const MaxSemanticDistance = 0.50

MaxSemanticDistance is the farthest cosine distance (0 = identical, 2 = opposite) at which a vector neighbour still counts as a semantic match.

Calibrated on real ctx data with nomic-embed-text: exact and near-exact matches sit at 0.14 to 0.30, genuinely on-topic paraphrases at 0.33 to 0.47, and the BEST match for an off-topic query (a recipe, a revenue forecast) at 0.56 to 0.60. 0.50 splits the last two groups. Recalibrate if the embedding model changes; the corpus it was measured on was small.

Variables

This section is empty.

Functions

This section is empty.

Types

type BestResult

type BestResult struct {
	Results    []Result
	UsedHybrid bool
	// FallbackReason is non-nil iff a client was provided but hybrid search
	// couldn't run — no embedding worker has caught up yet, vec0 isn't
	// available on this machine, or the local Ollama isn't reachable.
	FallbackReason error
}

BestResult reports what ran, not just the hits — callers (ctx search, the search_context MCP tool) can tell the user semantic search quietly degraded instead of presenting a fallback as if it were the real thing.

func Best

func Best(ctx context.Context, db *sql.DB, client embed.Embedder, query string, projectID int64, agent string, limit int) (BestResult, error)

Best tries hybrid (keyword+semantic) search first, falling back to keyword-only on any failure. Semantic search is always an enhancement, never a requirement, matching the same "optional, log and continue" posture migrations/002_vectors.sql takes at the schema level: a machine without Ollama running, or before the embed worker has caught up, still gets full keyword search. client may be nil to skip straight to keyword-only (e.g. a caller that already knows embeddings aren't set up).

type HybridResult

type HybridResult struct {
	ChunkID   int64
	Score     float64
	SessionID string
	Agent     string
	Title     string
	StartedAt string
	Preview   string // hit-centred excerpt of the chunk; see centerSnippet
	FirstSeq  int    // the chunk's message range, as in KeywordResult
	LastSeq   int
	At        string // when this chunk's turn happened; see KeywordResult.At
}

HybridResult is one row of SearchHybrid/SearchHybridByAgent's result set.

func Hybrid

func Hybrid(ctx context.Context, db *sql.DB, client embed.Embedder, query string, projectID int64, agent string, limit int) ([]HybridResult, error)

Hybrid runs Reciprocal Rank Fusion of keyword (FTS) and semantic (vector) search over chunks. It embeds query itself via client, so callers don't need to know embedding details — same contract as Keyword, just backed by two ranking signals instead of one.

SearchHybrid and SearchHybridByAgent are two separate named queries (see retrieval.sql's comment on SearchHybridByAgent for why vec0 needs its own copy rather than a shared ":agent IS NULL OR ..." clause); which one runs is an implementation detail callers shouldn't need to think about.

type KeywordResult

type KeywordResult struct {
	ChunkID   int64
	SessionID string
	Agent     string
	StartedAt string
	Snippet   string
	BM25      float64
	// FirstSeq and LastSeq are the message range this chunk covers, so a
	// caller can fetch exactly the matching exchange from get_session.
	FirstSeq int
	LastSeq  int
	// At is when this chunk's turn happened (its first message's timestamp),
	// falling back to the session start. StartedAt is the SESSION's start,
	// which is the same for every chunk in a long session.
	At string
}

KeywordResult is one row of SearchKeyword's result set.

func Keyword

func Keyword(db *sql.DB, query string, projectID int64, agent string, limit int) ([]KeywordResult, error)

Keyword runs SearchKeyword: BM25-ranked full-text search over chunks, scoped to a project and optionally to one agent.

type NoteResult

type NoteResult struct {
	ID        int64
	Kind      string
	Title     string
	Body      string
	Tags      []string
	Agent     string
	CreatedAt string
}

NoteResult is one row of SearchNotes's result set.

func Notes

func Notes(db *sql.DB, query string, projectID int64, limit int) ([]NoteResult, error)

Notes runs SearchNotes: keyword search over live (non-superseded) notes, scoped to a project plus any global notes (project_id IS NULL).

type Result

type Result struct {
	ChunkID   int64
	SessionID string
	Agent     string
	StartedAt string // when the SESSION started
	At        string // when this chunk's turn happened; use this for display
	Snippet   string
	FirstSeq  int // message range of the matching chunk; pass to get_session
	LastSeq   int
}

Result is search's unified hit shape, produced by either Keyword or Hybrid — callers that just want "the best available search" don't need to know which one actually ran.

Jump to

Keyboard shortcuts

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