index

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 20 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 index is the rebuildable SQLite index of a vault: notes, tags (nested a/b included), frontmatter properties, links and full-text search. The .md files stay the source of truth; the index can be deleted at any time and is rebuilt from disk.

It uses modernc.org/sqlite, a pure-Go SQLite, so every target (desktop, server, phones) builds without cgo. There are no migrations: when the schema version changes the database is dropped and rebuilt.

Index

Constants

View Source
const FileName = "index.db"

FileName is the index file inside the vault's .nota directory.

View Source
const SchemaVersion = 1

SchemaVersion is bumped on every schema change; a database with another version is dropped and rebuilt.

Variables

View Source
var ErrEmptyQuery = errors.New("index: empty search query")

ErrEmptyQuery is returned by Search for a query without search terms.

Functions

func Path

func Path(v *vault.Vault) string

Path returns the index location for a vault: <vault>/.nota/index.db.

Types

type Cond

type Cond struct {
	Key   string
	Value string
	// Not inverts the condition: the key is missing or has another value.
	Not bool
}

Cond is one property condition of a query.

func ParseCond

func ParseCond(s string) (Cond, error)

ParseCond parses "key=value" or "key!=value".

type Entry

type Entry struct {
	Path     string         `json:"path"`
	Folder   string         `json:"folder"`
	Title    string         `json:"title"`
	Tags     []string       `json:"tags"`
	Props    map[string]any `json:"properties"`
	Created  time.Time      `json:"created"`
	Updated  time.Time      `json:"updated"`
	Modified time.Time      `json:"modified"`
	Size     int64          `json:"size"`
	// Snippet is the matching text, for search results.
	Snippet string `json:"snippet,omitempty"`
}

Entry is one note as the index knows it.

type Index

type Index struct {

	// Recreated is true when Open found no index or an incompatible one.
	// The caller should Sync before trusting query results.
	Recreated bool
	// contains filtered or unexported fields
}

Index wraps the SQLite database.

func Open

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

Open opens (or creates) the index at path. Use ":memory:" in tests. A database with another schema version, or one that is not readable as an index, is deleted and created again.

func OpenVault

func OpenVault(v *vault.Vault) (*Index, error)

OpenVault opens the index of a vault, creating .nota/ when needed.

func (ix *Index) Backlinks(p string) ([]Link, error)

Backlinks returns the links from other notes (or the note itself) that resolve to p.

func (*Index) Close

func (ix *Index) Close() error

Close closes the database.

func (*Index) Count

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

Count returns the number of indexed notes.

func (*Index) DB

func (ix *Index) DB() *sql.DB

DB exposes the database for diagnostics and tests.

func (*Index) Get

func (ix *Index) Get(p string) (Entry, error)

Get returns one note by its exact path.

func (ix *Index) Links(p string) ([]Link, error)

Links returns the outgoing links of a note, resolved like Obsidian does.

func (*Index) Lookup

func (ix *Index) Lookup(ref string) ([]string, error)

Lookup finds notes for a user-supplied reference such as a CLI argument: an exact path (".md" optional), else a title or file name (any case), else a prefix of a path, title or file name. It returns every candidate of the first rule that matches, sorted by path.

func (*Index) Query

func (ix *Index) Query(q Query) ([]Entry, error)

Query returns the notes matching q.

func (*Index) Rebuild

func (ix *Index) Rebuild(v *vault.Vault) (Stats, error)

Rebuild drops everything and indexes the vault from scratch.

func (*Index) Remove

func (ix *Index) Remove(rel string) error

Remove drops one path from the index.

func (*Index) Resolve

func (ix *Index) Resolve(target, from string) (string, error)

Resolve resolves a wikilink target as written in the note at from.

func (*Index) Search

func (ix *Index) Search(q string, filter Query) ([]Entry, error)

Search runs a full-text search over titles and bodies, best match first; title matches weigh more. q is plain text: words must all occur (the last one as a prefix), "quoted phrases" match as phrases, -word excludes and OR between words means either. The filters in filter (folder, tag, where) apply as in Query; its Sort is ignored.

func (*Index) Sync

func (ix *Index) Sync(v *vault.Vault) (Stats, error)

Sync brings the index up to date with the vault: new and changed notes (by mtime and size, then content hash) are indexed, missing ones removed.

func (*Index) Tags

func (ix *Index) Tags() ([]TagCount, error)

Tags returns every tag in the vault with its note count, by name.

func (*Index) Update

func (ix *Index) Update(v *vault.Vault, rel string) error

Update re-indexes one note after the vault changed it. A path that no longer exists is removed from the index.

type Link struct {
	// From is the linking note, To the linked one ("" when unresolved).
	From    string `json:"from"`
	To      string `json:"to"`
	Target  string `json:"target"`
	Heading string `json:"heading,omitempty"`
	Alias   string `json:"alias,omitempty"`
	Embed   bool   `json:"embed,omitempty"`
}

Link is a resolved link between two notes.

type Query

type Query struct {
	// Folder limits results to a folder and its subfolders.
	Folder string
	// Tag matches the tag and its nested tags: "a" matches "a" and "a/b".
	Tag   string
	Where []Cond
	Sort  Sort
	Limit int
}

Query selects notes. Empty fields do not filter.

type Sort

type Sort struct {
	// Field is one of "path", "title", "created", "updated", "modified".
	Field string
	Desc  bool
}

Sort orders query results.

func ParseSort

func ParseSort(s string) (Sort, error)

ParseSort parses "updated desc", "title", "created asc" and so on. The empty string sorts by path.

type Stats

type Stats struct {
	Notes     int           `json:"notes"`
	Added     int           `json:"added"`
	Updated   int           `json:"updated"`
	Removed   int           `json:"removed"`
	Unchanged int           `json:"unchanged"`
	Duration  time.Duration `json:"durationNs"`
}

Stats describes one Sync or Rebuild pass.

func (Stats) Changed

func (s Stats) Changed() bool

Changed reports whether the pass wrote anything.

type TagCount

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

TagCount is a tag with the number of notes that carry it.

Jump to

Keyboard shortcuts

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