store

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CollectionChunks = "nb_chunks"
	CollectionLinks  = "nb_links"
)
View Source
const (
	FileTypeMD  = "md"
	FileTypePDF = "pdf"
)

FileTypeMD and FileTypePDF are the file_type metadata values written on ingested chunks. Kept in store (not ingest) because store queries filter on them; ingest imports store, so there is no import cycle.

Variables

This section is empty.

Functions

func CombineWhereFilters

func CombineWhereFilters(f1, f2 chroma.WhereFilter) (chroma.WhereFilter, error)

CombineWhereFilters safely combines two optional WhereFilters using And.

func TagWhereClause

func TagWhereClause(tag string) chroma.WhereClause

TagWhereClause constructs a Chroma Or filter matching tag against tag_0 through tag_19.

func TrimHeadingTitlePrefix added in v2.13.0

func TrimHeadingTitlePrefix(title, headingPath string) string

TrimHeadingTitlePrefix removes the leading heading-path segment when it duplicates the note title. PDF heading paths begin with the document's own H1 title (for example "Attention Is All You Need > 3 Model Architecture"). Showing the title and the full path together repeats the same text, so the display strips the duplicated segment.

The comparison normalizes both strings to lowercase alphanumerics. This tolerates punctuation drift between the slug-derived title and the PDF H1 (for example a colon after the first word). When the whole path is just the title, the result is empty.

Types

type BatchIngestData

type BatchIngestData struct {
	NoteSlug     string
	ChunkRecords []ChunkRecord
	Links        []string
}

type ChunkRecord

type ChunkRecord struct {
	ID           string // "<slug>:<index>"
	NoteSlug     string
	Title        string
	FilePath     string
	ChunkIndex   int
	Text         string
	Tags         []string
	FileType     string // e.g. "md" or "pdf"
	HeadingPath  string
	HeadingLevel int
	HasTask      bool
	HasCode      bool
	ContentHash  string
	Embedding    []float32
}

type HiddenOption added in v2.3.0

type HiddenOption func(*HiddenOptions)

func WithIncludeLinked added in v2.3.0

func WithIncludeLinked(includeLinked bool) HiddenOption

WithIncludeLinked allows returning notes that are already linked to/from the seed note.

type HiddenOptions added in v2.3.0

type HiddenOptions struct {
	IncludeLinked bool
}

type NoteContent

type NoteContent struct {
	NoteSlug string   `json:"note_slug"`
	Title    string   `json:"title"`
	FilePath string   `json:"file_path"`
	Tags     []string `json:"tags,omitempty"`
	Text     string   `json:"text"`
	Chunks   int      `json:"chunks"`
}

NoteContent represents the complete reconstructed text and metadata of a note.

type NoteMeta added in v2.10.0

type NoteMeta struct {
	Hash     string
	FileType string
}

NoteMeta holds metadata for a note chunk, such as its content hash and original file type.

type Option added in v2.3.0

type Option func(*Store)

Option configures Store when calling Open.

type Result

type Result struct {
	NoteSlug       string   `json:"note_slug"`
	Title          string   `json:"title"`
	FilePath       string   `json:"file_path,omitempty"`
	Score          float64  `json:"score"`
	IsPhantom      bool     `json:"is_phantom,omitempty"`
	ChunkIndex     int      `json:"chunk_index,omitempty"`
	Tags           []string `json:"tags,omitempty"`
	Extra          string   `json:"extra,omitempty"` // e.g. shared tags, hop count
	HeadingPath    string   `json:"heading_path,omitempty"`
	Text           string   `json:"text,omitempty"`    // populated only when include-text is requested
	Context        []string `json:"context,omitempty"` // adjacent chunks when windowing is enabled
	MatchedQueries []string `json:"matched_queries,omitempty"`
	FileType       string   `json:"file_type,omitempty"`
	Lexical        bool     `json:"lexical,omitempty"` // keyword-fallback match, not semantic similarity
}

Result is one row returned by any query.

type SearchFilter added in v2.13.0

type SearchFilter struct {
	Section    string   // heading_path equality
	Tag        string   // any-tag equality (OR over tag_0..tag_19)
	HasTasks   bool     // only chunks containing task lists
	HasCode    bool     // only chunks containing fenced code blocks
	IncludePDF bool     // false restricts results to file_type == "md"
	Exclude    []string // note_slugs to skip (Nin filter)
	// ResolveTags controls whether Tag participates in the filter; it is
	// false when the tag drives its own search path (TagSearch).
	ResolveTags bool
}

SearchFilter describes the user-facing filters applied to a search query. Commands build one instead of constructing Chroma's filter DSL directly, keeping the "which fields are queryable" contract inside the store.

func (*SearchFilter) Build added in v2.13.0

func (f *SearchFilter) Build() chroma.WhereFilter

Build compiles the filter into a Chroma WhereFilter, or nil when no filtering is requested.

type Stats added in v2.13.0

type Stats struct {
	Notes  int64 `json:"notes"`
	Chunks int64 `json:"chunks"`
	Links  int64 `json:"links"`
}

Stats reports collection counts. The typed struct (rather than a string-keyed map) keeps cmd/stats.go and JSON output safe from typos.

type Store

type Store struct {
	SkipAttachments bool
	// contains filtered or unexported fields
}

Store wraps two ChromaDB collections.

func Open

func Open(ctx context.Context, path string, opts ...Option) (*Store, error)

Open creates or opens the persistent ChromaDB store at path.

func (s *Store) Backlinks(ctx context.Context, targetSlug string) ([]Result, error)

Backlinks returns all notes that link TO targetSlug.

func (*Store) BatchIngest

func (s *Store) BatchIngest(ctx context.Context, data []BatchIngestData, staleSlugs []string) error

BatchIngest atomically replaces all chunks and links for a batch of notes, and deletes stale notes. It uses a single mutex lock to serialize operations on the store.

func (*Store) Close

func (s *Store) Close() error

Close releases all resources.

func (*Store) Connections

func (s *Store) Connections(ctx context.Context, seedSlug string, maxHops int) ([]Result, error)

Connections finds notes reachable from seedSlug within maxHops. BFS implemented in Go (no recursive SQL needed).

func (*Store) GetNote

func (s *Store) GetNote(ctx context.Context, slugOrPath string) (*NoteContent, error)

GetNote retrieves all chunks of a note and reconstructs its complete content.

func (*Store) GetNoteHead added in v2.13.0

func (s *Store) GetNoteHead(ctx context.Context, slugOrPath string, maxChunks int) (*NoteContent, error)

GetNoteHead retrieves only the first maxChunks chunks of a note's text. Chunks reports the note's total chunk count regardless of the cap.

func (*Store) GetNoteMeta added in v2.13.0

func (s *Store) GetNoteMeta(ctx context.Context, slugOrPath string) (*NoteContent, error)

GetNoteMeta retrieves a note's header (slug, title, path, tags) and chunk count without fetching any chunk text — the cheap alternative to GetNote when only identification is needed.

func (*Store) GetNoteMetadata added in v2.10.0

func (s *Store) GetNoteMetadata(ctx context.Context) (map[string]NoteMeta, error)

GetNoteMetadata fetches NoteMeta (content_hash and file_type) for all notes by reading chunk_index=0. Returns a map of note_slug -> NoteMeta.

func (*Store) GraphBoostedSearch

func (s *Store) GraphBoostedSearch(ctx context.Context, queryVec []float32, seedSlug string, boost float64, limit int, whereFilter chroma.WhereFilter, includeText bool) ([]Result, error)

GraphBoostedSearch runs semantic search, then boosts scores of notes directly linked to/from seedSlug.

func (*Store) HiddenConnections

func (s *Store) HiddenConnections(ctx context.Context, queryVec []float32, seedSlug string, limit int, includeText bool, options ...HiddenOption) ([]Result, error)

HiddenConnections finds notes semantically similar to queryVec but NOT already linked to/from seedSlug (unless WithIncludeLinked is set).

func (*Store) HiddenConnectionsDeep added in v2.3.0

func (s *Store) HiddenConnectionsDeep(ctx context.Context, seedSlug string, limit int, topKPerNote int, includeText bool, options ...HiddenOption) ([]Result, []string, error)

HiddenConnectionsDeep runs chunk-by-chunk semantic comparison between all chunks of seedSlug and all other chunks in the vault. Returns deduplicated results and the labels of the seed chunks analyzed.

func (*Store) LexicalSearch added in v2.13.0

func (s *Store) LexicalSearch(ctx context.Context, query string, limit int, whereFilter WhereFilter) ([]Result, error)

LexicalSearch finds notes whose title, file path, tags, or first-chunk text contains the query's tokens (case-insensitive substring). It is the fallback for queries that produce no semantic matches (e.g. short common words like "Lecture"). Rows are ranked by token hits (title hits weigh most) and marked Lexical so callers can distinguish keyword matches from semantic similarity; Score is always 0. The whereFilter (tag, section, excludes, ...) is respected.

func (*Store) ListTags added in v2.13.0

func (s *Store) ListTags(ctx context.Context, limit int) ([]TagCount, error)

ListTags returns every distinct tag in the index with the number of notes carrying it, sorted by count descending then tag ascending. A limit <= 0 returns all tags; otherwise only the top-limit entries are returned.

func (*Store) MultiSemanticSearch added in v2.2.0

func (s *Store) MultiSemanticSearch(ctx context.Context, queryVecs [][]float32, queries []string, limit int, topKPerNote int, whereFilter chroma.WhereFilter, includeText bool) ([]Result, error)

MultiSemanticSearch executes semantic searches across multiple query vectors, merging results and boosting chunks that match multiple queries.

func (*Store) PopulateContext

func (s *Store) PopulateContext(ctx context.Context, results []Result, windowSize int) error

PopulateContext fetches adjacent chunks for each result when windowSize > 0.

func (*Store) Reset

func (s *Store) Reset(ctx context.Context) error

Reset drops and recreates both collections. Used by `notebrain reset`.

func (*Store) ResolveNoteSlug added in v2.3.0

func (s *Store) ResolveNoteSlug(ctx context.Context, input string) (string, error)

ResolveNoteSlug resolves a user-provided input (exact slug, title, filename, or partial path) to its exact indexed note_slug in ChromaDB. Resolution is deterministic: title/path/suffix matching happens in one metadata scan (never against a slugified guess), so the same input resolves the same way in every subcommand. A missing note is an error, not a silently guessed slug.

func (*Store) ResolveNoteSlugs added in v2.13.0

func (s *Store) ResolveNoteSlugs(ctx context.Context, inputs []string) (resolved map[string]string, indexed map[string]struct{}, err error)

ResolveNoteSlugs resolves multiple user inputs (exact slug, title, filename, or partial path) against a single metadata scan shared across all inputs, so the cost does not grow with the number of inputs. The returned maps contain the resolution of every input (falling back to the slugified input when nothing matches) and every indexed note slug, so callers can distinguish a real match from a fallback.

func (*Store) SemanticSearch

func (s *Store) SemanticSearch(ctx context.Context, queryVec []float32, limit int, topKPerNote int, whereFilter chroma.WhereFilter, includeText bool) ([]Result, error)

SemanticSearch finds the most similar chunks to queryVec. Returns deduplicated chunks retaining up to topKPerNote chunks per note.

func (*Store) SharedTags

func (s *Store) SharedTags(ctx context.Context, noteSlug string, minShared int) ([]Result, error)

SharedTags finds notes sharing at least minShared tags with noteSlug.

func (*Store) Stats

func (s *Store) Stats(ctx context.Context) (*Stats, error)

Stats returns document counts for collections and distinct notes.

func (*Store) SuggestTags added in v2.13.0

func (s *Store) SuggestTags(ctx context.Context, input string, limit int) ([]string, error)

SuggestTags returns up to limit tags closest to input by edit distance, for "did you mean" hints after a tag search finds nothing. Exact matches are excluded. Ties are broken by note count descending, then alphabetically.

func (*Store) TagSearch

func (s *Store) TagSearch(ctx context.Context, tag string, limit int, hierarchical bool, whereFilter chroma.WhereFilter, includeText bool) ([]Result, error)

TagSearch finds notes that match a specific tag name.

type TagCount added in v2.13.0

type TagCount struct {
	Tag   string `json:"tag"`
	Count int    `json:"count"`
}

TagCount is a single tag and the number of indexed notes carrying it.

type WhereFilter added in v2.13.0

type WhereFilter = chroma.WhereFilter

WhereFilter is the ChromaDB where-filter type used by search methods. It is re-exported so callers outside the store package (e.g. cmd) can declare filters without importing the Chroma SDK directly.

Jump to

Keyboard shortcuts

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