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 ¶
- type Batch
- type Hit
- type Index
- func (ix *Index) AllHashes() (map[string]string, error)
- func (ix *Index) Batch() *Batch
- func (ix *Index) BestSection(id, query string) (heading, snippet string, ok bool, err error)
- func (ix *Index) Close() error
- func (ix *Index) Count() (int, error)
- func (ix *Index) Delete(id string) error
- func (ix *Index) IDsContaining(subs []string) (map[string]bool, error)
- func (ix *Index) SearchFTS(query, scope string, limit int) ([]Hit, error)
- func (ix *Index) SearchFTSFiltered(query, scope string, limit int, allow func(id string) bool) ([]Hit, error)
- func (ix *Index) SearchFTSIDs(query, scope string, limit int, allow func(id string) bool) ([]Hit, error)
- func (ix *Index) Upsert(id, contentHash, body, assetsText string) error
- func (ix *Index) WithContext(ctx context.Context) *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
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) Rollback ¶ added in v0.27.0
func (b *Batch) Rollback()
Rollback discards the open transaction, if any (the concepts of earlier commits stay).
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 ¶
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
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
Batch starts a Batch; the first transaction opens on the first write.
func (*Index) BestSection ¶ added in v0.27.0
func (*Index) Delete ¶
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
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 ¶
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 ¶
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.