SearchIndex
SearchIndex is a small full-text index: documents go in under a String id,
and searches for words come back ranked best first. It is what a
find-as-you-type box or a local search needs. From 0.72.
#import "SearchIndex.xc" // not in the Foundation umbrella: import it by nameOverview
Section titled “Overview”SearchIndex* idx = new SearchIndex();idx.addDocument(String.withCString("a"), String.withCString("The quick brown fox"));idx.addDocument(String.withCString("b"), String.withCString("A quick quick dog"));Array* hits = idx.search(String.withCString("quick fox"));// a (both words), then b (one word, twice)SearchResult* best = (SearchResult*)hits.get(0); // best.id is "a"Words. Text is split into runs of letters and digits, with ASCII letters
lowered, so Fox, and fox are one word. The bytes of UTF-8 count as letters,
so other scripts are indexed too, without case folding. An inverted index maps
each word to the documents that hold it and how often.
Queries are split the same way. search finds documents holding
any of the query’s words, searchAll those holding every one. A
query word ending in * matches every word it begins (qui* finds quick
and quietly).
Ranking: more of the query’s words matched first, then more occurrences of them, then the order the documents were first added.
Topics
Section titled “Topics”Documents · addDocument · removeDocument · containsDocument · documentCount · termCount
Searching · search · searchAll · SearchResult · words
Documents
Section titled “Documents”addDocument
Section titled “addDocument”void addDocument(String* id, String* text)Indexes text as the document id, replacing any document of that id (which
keeps its place in the tie-breaking order).
removeDocument
Section titled “removeDocument”void removeDocument(String* id)containsDocument
Section titled “containsDocument”bool containsDocument(String* id)documentCount
Section titled “documentCount”u32 documentCount(void)termCount
Section titled “termCount”u32 termCount(void)The number of distinct words indexed.
Searching
Section titled “Searching”search
Section titled “search”Array* search(String* query)The documents holding any of the query’s words, as
SearchResults, best first. An empty query finds nothing.
searchAll
Section titled “searchAll”Array* searchAll(String* query)The documents holding every one of the query’s words, best first.
SearchResult
Section titled “SearchResult”class SearchResult : Object { String* id; u32 matchedTerms; // how many of the query's words it holds u32 score; // how many times it holds them, in all }static Array* words(String* text)The words text splits into, in order, repeats included: what the index
stores.