sqlindex

package
v0.29.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 12 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 sqlindex implements a persistent SQLite-backed keyword index.

The schema:

concepts(id TEXT PRIMARY KEY, content_hash TEXT NOT NULL, body TEXT NOT NULL)
concepts_fts(id UNINDEXED, title, meta, body) — FTS5, trigram tokenizer
with diacritics removed, ranked by bm25 weights 5 / 2 / 1 (D246)
sections_fts(id UNINDEXED, heading UNINDEXED, title, body) — FTS5, same
tokenizer: one row per "##" section of a concept's body, bm25 weights 3 / 1
(D1207). It never ranks pages: it only answers which section of an already
ranked page matched best (BestSection).
fts_rows(id, kind, fts_rowid) — which rowids of the two FTS tables belong
to a concept, so an update or delete removes them by rowid: FTS5 cannot
index an UNINDEXED column and `WHERE id = ?` scans the table (#1507).

The schema version lives in PRAGMA user_version (schemaVersion). Opening an older database recreates concepts_fts and sections_fts and empties concepts: the index is disposable, and the next reconciliation reindexes every concept.

A database created before D135 may still carry an embeddings table: it is never read nor written, and opening such a file is not an error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch added in v0.27.0

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

Batch groups the writes of a reconciliation into transactions of batchSize concepts. It is not safe for concurrent use, and while one is open another writer waits (busy_timeout): callers are already serialised by the reconciler. Always end it with Commit or Rollback. A group that fails is replayed concept by concept outside the batch, so only the concept that fails is lost, never the ones written before it in the same group.

func (*Batch) Commit added in v0.27.0

func (b *Batch) Commit() error

Commit commits the open transaction, if any. The Batch stays usable. When the commit fails its group is replayed concept by concept and the error is returned only if that lost something.

func (*Batch) Delete added in v0.27.0

func (b *Batch) Delete(id string) error

Delete is Index.Delete inside the batch's transaction.

func (*Batch) Rollback added in v0.27.0

func (b *Batch) Rollback()

Rollback discards the open transaction, if any (the concepts of earlier commits stay).

func (*Batch) Upsert added in v0.27.0

func (b *Batch) Upsert(id, contentHash, body, assetsText string) error

Upsert is Index.Upsert inside the batch's transaction.

func (*Batch) Written added in v0.27.0

func (b *Batch) Written() int

Written is the number of upserts that were committed, replays included.

type Hit

type Hit struct {
	ID      string
	Score   float64
	Snippet string // excerpt around the match, from FTS5's native snippet()
	// Partial marks an OR-pass hit that matched only some terms (D343); it is
	// always listed after every full match.
	Partial bool
}

Hit is a single search result.

type Index

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

Index is a persistent SQLite-backed search and embedding index.

func Open

func Open(dbPath string) (*Index, error)

Open opens a SQLite index at dbPath. It creates the schema if it does not exist and enables WAL mode. If FTS5 support is missing, Open returns an error — the caller should fall back to the in-memory path.

func (*Index) AllHashes added in v0.2.0

func (ix *Index) AllHashes() (map[string]string, error)

AllHashes returns the persisted content hash for every indexed concept. Callers use it to reconcile the derived index with the KB files on disk.

func (*Index) Batch added in v0.27.0

func (ix *Index) Batch() *Batch

Batch starts a Batch; the first transaction opens on the first write.

func (*Index) BestSection added in v0.27.0

func (ix *Index) BestSection(id, query string) (heading, snippet string, ok bool, err error)

func (*Index) Close

func (ix *Index) Close() error

Close closes the underlying database connection.

func (*Index) Count

func (ix *Index) Count() (int, error)

Count returns the number of indexed concepts.

func (*Index) Delete

func (ix *Index) Delete(id string) error

Delete removes a concept's content from both the concepts table and the FTS5 index (used by concept_delete).

func (*Index) IDsContaining added in v0.28.0

func (ix *Index) IDsContaining(subs []string) (map[string]bool, error)

IDsContaining returns the ids of the indexed concepts whose stored content holds at least one of subs as a plain, case-sensitive substring: the candidates of a mentions scan, found inside SQLite instead of by reading every file. The set is exact for the content the index was last reconciled with, so callers reconcile first.

func (*Index) SearchFTS

func (ix *Index) SearchFTS(query, scope string, limit int) ([]Hit, error)

SearchFTS performs a keyword search via FTS5 trigram tokenizer. If scope is non-empty, only concepts whose id starts with scope are returned. Results are sorted by BM25 relevance (higher score = better match). Each hit carries a Snippet excerpt produced by FTS5's native snippet() function.

func (*Index) SearchFTSFiltered added in v0.4.0

func (ix *Index) SearchFTSFiltered(query, scope string, limit int, allow func(id string) bool) ([]Hit, error)

SearchFTSFiltered scans ranked FTS rows in bounded pages until it collects limit allowed hits or reaches EOF. The predicate is applied before a hit is returned, preventing IDs, snippets, and result counts of hidden concepts from escaping into caller-visible pagination.

func (*Index) SearchFTSIDs added in v0.27.0

func (ix *Index) SearchFTSIDs(query, scope string, limit int, allow func(id string) bool) ([]Hit, error)

SearchFTSIDs is SearchFTSFiltered for a caller that needs only which concepts match and in what order: the hits carry no Snippet. snippet() is quadratic in the number of matches inside one body under the trigram tokenizer, so a 76 KB body took 8 s to excerpt (#1276); the write-time repeated_fact lookup (D351) never reads an excerpt and must not pay it.

func (*Index) Upsert

func (ix *Index) Upsert(id, contentHash, body, assetsText string) error

Upsert inserts or updates a concept's content in both the concepts table and the FTS5 index. body is the raw file content; it is split into the title, meta and body columns here, the same way the in-memory index weights it. assetsText is the searchable text of the concept's assets (D277), empty for a concept that has none; contentHash should cover it (see the reconciler). Many concepts in a row go through a Batch, which is several times faster.

func (*Index) WithContext added in v0.29.0

func (ix *Index) WithContext(ctx context.Context) *Index

WithContext returns a view of ix whose queries stop when ctx ends: a search of a client that disconnected must not keep a core busy (#2189). The view shares the database; closing it is closing ix.

Jump to

Keyboard shortcuts

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