search

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package search runs a semantic query against the index: embed the query, cosine-rank it against every stored chunk, and return the top matches. At personal-vault scale a brute-force scan in memory is well under a millisecond, so there is no ANN index — just a sort.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChunkSource

type ChunkSource interface {
	AllChunks(pathPrefix string) ([]index.ChunkRow, error)
}

ChunkSource is the read side of the index that search needs. *index.Store satisfies it; tests supply a fake so search runs without a database.

type DupeOptions

type DupeOptions struct {
	MinScore   float64  // report pairs at or above this cosine value
	Limit      int      // max pairs to return; <= 0 means no cap
	PathPrefix string   // restrict to files under this rel-path prefix
	Exclude    []string // drop files under any of these rel-path prefixes
	WithinFile bool     // also report pairs from the same file (default: cross-file only)
}

DupeOptions tunes a near-duplicate scan.

type Embedder

type Embedder func(text string) (embed.Vec, error)

Embedder turns the query text into a vector. Injected so search runs without the ONNX runtime; production passes embed.Get.

type Hit

type Hit struct {
	RelPath string  `json:"path"`
	Line    int     `json:"line"`
	Heading string  `json:"heading,omitempty"`
	Variant string  `json:"variant"`
	Key     string  `json:"key"`
	Score   float64 `json:"score"`
	Text    string  `json:"text"`
}

Hit is one ranked result.

func Query

func Query(src ChunkSource, embedFn Embedder, q string, opts Options) ([]Hit, error)

Query embeds q, ranks it against the index chunks, and returns hits sorted by descending cosine score. An empty query is an error; an empty index is not (it yields no hits).

type Kind

type Kind int

Kind narrows a search to one class of indexed content. Docs and Code partition the index: markdown is docs, every other indexed language is code. For a narrower cut than that one bit, see Options.Langs.

const (
	KindAny  Kind = iota // no type filter
	KindDocs             // markdown only
	KindCode             // source code only (non-markdown)
)

KindAny, KindDocs, and KindCode are the values of Kind.

type Options

type Options struct {
	Limit      int     // max hits to return; <= 0 means no cap
	PathPrefix string  // restrict to files under this rel-path prefix
	MinScore   float64 // drop hits scoring below this cosine value
	Collapse   bool    // keep only the best-scoring chunk per file
	Kind       Kind    // restrict to docs or code; KindAny searches both

	// Langs restricts results to these canonical language names (see
	// chunk.NormalizeLanguage). Empty means every language. This is a finer
	// cut than Kind: "code" is one bit, but with eighteen languages indexed
	// the useful question is usually "the Python one" or "the manifests".
	Langs []string
}

Options tunes a query.

type Pair

type Pair struct {
	A     Hit     `json:"a"`
	B     Hit     `json:"b"`
	Score float64 `json:"score"`
}

Pair is two chunks found to be near-duplicates, ordered so A sorts before B (by rel-path then key) for a stable presentation.

func Duplicates

func Duplicates(src ChunkSource, opts DupeOptions) ([]Pair, error)

Duplicates scans the index for pairs of chunks whose cosine similarity is at or above opts.MinScore — near-duplicate sections that likely encode redundant docs or guidance. By default only cross-file pairs are reported (set WithinFile to include repetition inside one file). Vectors from the embedder are L2-normalized, so this is an all-pairs dot product; at personal-vault scale the O(n²) scan is well under a second.

Jump to

Keyboard shortcuts

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