search

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 24 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 search is KyDrive's content search index: a blind index of keyed token hashes in its own SQLite file, plus the tokenizer and the snippet maker that work on plaintext in memory only. Nothing it writes holds a word, a name or a blob ID.

Index

Constants

View Source
const (
	StateDone   = 1
	StateFailed = 2
	// MaxAttempts is how often a failing document is tried before it is left alone.
	MaxAttempts = 3
)

Document states. A document with no row is unindexed.

View Source
const (
	// MinPrefixRunes is the shortest prefix `*` expands; a shorter one is a whole word.
	MinPrefixRunes = 3
	// MaxPrefixWords is how many words one prefix expands to: words the searched documents hold,
	// those in most of them first.
	MaxPrefixWords = 100
)
View Source
const (
	// MaxQueryTerms is how many distinct terms a query matches; later ones are ignored.
	MaxQueryTerms = 8
	// MaxDocTerms is how many distinct terms one document indexes, first seen first.
	MaxDocTerms = 50000
)
View Source
const File = "index.db"

File is the index database's name inside its directory (<KY_DATA_DIR>/search).

View Source
const (
	// SnippetScan is how much text a snippet looks through for a match.
	SnippetScan = 4 << 20
)
View Source
const TokenizerVersion = 1

TokenizerVersion is stored in the index; changing how text becomes tokens must bump it, which rebuilds the index.

View Source
const VocabFile = "vocab"

VocabFile is the sealed vocabulary's name inside the index directory.

Variables

This section is empty.

Functions

func Count

func Count(text string) (map[string]int, int)

Count returns how often each term occurs in text (capped at 255; at most MaxDocTerms distinct, first seen first) and how many tokens text has.

func HasPhrases added in v0.6.1

func HasPhrases(text string, phrases [][]string) bool

HasPhrases reports whether text holds every phrase, each as consecutive terms (white space, punctuation, line and page breaks between them do not count).

func Retry

func Retry(state, attempts int, updated int64, now time.Time) bool

Retry reports whether a document in that status should be extracted again at now: unindexed, or failed fewer than MaxAttempts times and past its backoff (2, 4 minutes).

func Terms

func Terms(q string) []string

Terms returns the distinct terms of query q in order, at most MaxQueryTerms.

Types

type DocStatus

type DocStatus struct {
	State, Attempts int
	Updated         int64
}

DocStatus is one document's state, failed attempts and last update (Unix).

type Index

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

Index is the content search index. Postings name a document by ref, a keyed hash of its blob ID and extractor kind, and a term by a keyed 64-bit hash; the database holds no plaintext, no blob ID and no file type. Queue rows hold random file IDs until processed.

func Open

func Open(ctx context.Context, dir string, key []byte) (*Index, error)

Open opens or creates the index under dir. An index built under another deployment key or tokenizer version is deleted and started again, empty.

func (*Index) BuildVocab added in v0.6.1

func (x *Index) BuildVocab(fresh bool, now time.Time) *VocabBuild

BuildVocab starts a vocabulary update: a fresh one replaces the vocabulary, otherwise the words are added to it.

func (*Index) Close

func (x *Index) Close() error

Close seals the vocabulary and closes the index database.

func (*Index) Cursor

func (x *Index) Cursor(ctx context.Context) (int64, error)

Cursor is the last drive change sequence number queued.

func (*Index) Dequeue

func (x *Index) Dequeue(ctx context.Context, file string) error

Dequeue drops file from the queue.

func (*Index) Docs

func (x *Index) Docs(ctx context.Context) (map[string]DocStatus, error)

Docs returns every document's status by ref.

func (*Index) Due

func (x *Index) Due(ctx context.Context, now time.Time, force bool, limit int) ([]string, error)

Due returns up to limit queued files whose wait is over (all of them when force is set).

func (*Index) Enqueue

func (x *Index) Enqueue(ctx context.Context, files []string, now time.Time, delay time.Duration, cursor int64) error

Enqueue queues files to be (re)considered after delay, and, when cursor >= 0, records it in the same transaction. A file queued again waits for the later time, but never more than a minute after it was first queued, so an autosaving file is still indexed.

func (*Index) Fail

func (x *Index) Fail(ctx context.Context, ref, code, file string, now time.Time) error

Fail records a failed extraction of ref (code is a short reason, never content) and drops file from the queue.

func (*Index) Keep

func (x *Index) Keep(ctx context.Context, keep map[string]bool) (int, error)

Keep deletes every document whose ref is not in keep, with its postings, and returns how many went. Postings have no index by document, so they are deleted in batched scans.

func (*Index) Learn added in v0.6.1

func (x *Index) Learn(text string)

Learn adds a document's words to the vocabulary without touching its postings (its text was extracted again after the text cache was lost).

func (*Index) Match

func (x *Index) Match(ctx context.Context, q Query, refs map[string]bool) (Matched, error)

Match scores the documents among refs that hold every word, a word starting with each prefix (its expansions count as one term) and every phrase term (BM25), with document counts, frequencies and lengths taken from refs only, so what the caller cannot read never shapes the result. Phrase order is not checked here (HasPhrases).

func (*Index) Put

func (x *Index) Put(ctx context.Context, ref string, counts map[string]int, length int, file string, now time.Time) error

Put records ref's term counts and token count, and drops file from the queue, in one transaction. A failed document has no postings, so none need removing.

func (*Index) Queued

func (x *Index) Queued(ctx context.Context) (int, error)

Queued is how many files wait in the queue.

func (*Index) Ref

func (x *Index) Ref(blob, kind string) string

Ref identifies the text of blob extracted as kind. Equal content shares one ref.

func (*Index) SaveVocab added in v0.6.1

func (x *Index) SaveVocab(now bool) error

SaveVocab seals the vocabulary to disk when it changed, at most once a minute unless now is set; Close saves it too. The file is replaced atomically.

func (*Index) Status

func (x *Index) Status(ctx context.Context, ref string) (state, attempts int, updated int64, err error)

Status is a document's state (0 when unindexed), failed attempts and last update (Unix).

func (*Index) VocabDue added in v0.6.1

func (x *Index) VocabDue(now time.Time) (since int64, fresh bool)

VocabDue says which documents' words the vocabulary may lack: those updated at or after since. fresh means a full rebuild is due (none yet, or the last is a day old), since is then 0.

type Matched added in v0.6.1

type Matched struct {
	Scores   map[string]float64
	Pending  int
	Expanded []string
	Capped   bool
}

Matched is what Match found: scores by ref, refs not yet settled, the words the prefixes expanded to among the given refs (for highlighting) and whether a prefix had more than MaxPrefixWords such words.

type Query added in v0.6.1

type Query struct {
	Words    []string
	Prefixes []string
	Phrases  [][]string
}

Query is a parsed content query. A document must hold every word, a word starting with each prefix, and each phrase's terms in order (Phrases are checked against the text by the caller, HasPhrases; the index only knows the terms are there).

func ParseQuery added in v0.6.1

func ParseQuery(q string) Query

ParseQuery reads q: a run of letters or digits followed by `*` is a prefix when it has at least MinPrefixRunes runes and is not CJK; text in double quotes is a phrase (one term in quotes is a word; an unclosed quote is ignored). Everything else is words. At most MaxQueryTerms distinct terms are kept, first come first; a phrase that does not fit is dropped.

func (Query) Empty added in v0.6.1

func (q Query) Empty() bool

Empty reports whether q has nothing to match.

type Snippet

type Snippet struct {
	Text       string   `json:"text"`
	Highlights [][2]int `json:"highlights"`
	CutStart   bool     `json:"cut_start"`
	CutEnd     bool     `json:"cut_end"`
}

Snippet is a short plain-text excerpt. Highlights are [start, end) offsets in UTF-16 code units of Text (JavaScript string indexes). It is never markup.

func MakeSnippet

func MakeSnippet(r io.Reader, terms []string, phrases [][]string) (*Snippet, error)

MakeSnippet reads at most SnippetScan bytes of text from r and returns the window with the most distinct terms, the first such, or nil when none of terms occurs there. A phrase's terms count only where the whole phrase occurs, and the whole occurrence is highlighted.

type VocabBuild added in v0.6.1

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

VocabBuild collects documents' text for the vocabulary (VocabDue); Done applies it.

func (*VocabBuild) Add added in v0.6.1

func (b *VocabBuild) Add(text string)

Add adds one document's text.

func (*VocabBuild) Done added in v0.6.1

func (b *VocabBuild) Done() error

Done applies the update and seals the result. Words indexed meanwhile (Put, Learn) are kept.

Jump to

Keyboard shortcuts

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