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
- func Count(text string) (map[string]int, int)
- func HasPhrases(text string, phrases [][]string) bool
- func Retry(state, attempts int, updated int64, now time.Time) bool
- func Terms(q string) []string
- type DocStatus
- type Index
- func (x *Index) BuildVocab(fresh bool, now time.Time) *VocabBuild
- func (x *Index) Close() error
- func (x *Index) Cursor(ctx context.Context) (int64, error)
- func (x *Index) Dequeue(ctx context.Context, file string) error
- func (x *Index) Docs(ctx context.Context) (map[string]DocStatus, error)
- func (x *Index) Due(ctx context.Context, now time.Time, force bool, limit int) ([]string, error)
- func (x *Index) Enqueue(ctx context.Context, files []string, now time.Time, delay time.Duration, ...) error
- func (x *Index) Fail(ctx context.Context, ref, code, file string, now time.Time) error
- func (x *Index) Keep(ctx context.Context, keep map[string]bool) (int, error)
- func (x *Index) Learn(text string)
- func (x *Index) Match(ctx context.Context, q Query, refs map[string]bool) (Matched, error)
- func (x *Index) Put(ctx context.Context, ref string, counts map[string]int, length int, ...) error
- func (x *Index) Queued(ctx context.Context) (int, error)
- func (x *Index) Ref(blob, kind string) string
- func (x *Index) SaveVocab(now bool) error
- func (x *Index) Status(ctx context.Context, ref string) (state, attempts int, updated int64, err error)
- func (x *Index) VocabDue(now time.Time) (since int64, fresh bool)
- type Matched
- type Query
- type Snippet
- type VocabBuild
Constants ¶
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.
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 )
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 )
const File = "index.db"
File is the index database's name inside its directory (<KY_DATA_DIR>/search).
const (
// SnippetScan is how much text a snippet looks through for a match.
SnippetScan = 4 << 20
)
const TokenizerVersion = 1
TokenizerVersion is stored in the index; changing how text becomes tokens must bump it, which rebuilds the index.
const VocabFile = "vocab"
VocabFile is the sealed vocabulary's name inside the index directory.
Variables ¶
This section is empty.
Functions ¶
func Count ¶
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
HasPhrases reports whether text holds every phrase, each as consecutive terms (white space, punctuation, line and page breaks between them do not count).
Types ¶
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 ¶
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) Due ¶
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 ¶
Fail records a failed extraction of ref (code is a short reason, never content) and drops file from the queue.
func (*Index) Keep ¶
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
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 ¶
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) Ref ¶
Ref identifies the text of blob extracted as kind. Equal content shares one ref.
func (*Index) SaveVocab ¶ added in v0.6.1
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.
type Matched ¶ added in v0.6.1
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
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
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.
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 ¶
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.