Class: SearchEngine

nanosearch~SearchEngine(existingIndex, preprocessor, tokenizer) → {this}

A tiny search engine. Usage: const engine = new SearchEngine(); // Add some documents. Call for each document you need to index. engine.add(uniqueDocumentId, documentText); // Later, you can let the user search & return the first 10 results. const results = engine.search(userQuery, 10);

Constructor

new SearchEngine(existingIndex, preprocessor, tokenizer) → {this}

Creates a new search engine.
Parameters:
Name Type Description
existingIndex object The existing index or `undefined` (fresh index).
preprocessor object The preprocessor or `undefined` (`BasicPreprocessor`).
tokenizer object The tokenizer or `undefined` (`NGramTokenizer`).
Source:
Returns:
Type
this

Methods

add(docId, doc) → {undefined}

Adds a document to the index.
Parameters:
Name Type Description
docId string The unique identifier for the document.
doc string The text of the document.
Source:
Returns:
Type
undefined

clear() → {undefined}

Clears out the entire index.
Source:
Returns:
Type
undefined

fromJson(indexData) → {undefined}

Loads the index from a JSON string. This *clears* the index first!
Parameters:
Name Type Description
indexData string The JSON of a previously-built index.
Source:
Returns:
Type
undefined

remove(docId) → {undefined}

Removes a document from the index. Safe to call even if the document isn't indexed. WARNING: Depending on the size of your index, this may take a LONG time.
Parameters:
Name Type Description
docId string The unique identifier for the document.
Source:
Returns:
Type
undefined

scoreResult(rawResult) → {float}

Scores a raw result. Mostly an internal method called by `_search`, but you can override if needed/have a better way to score.
Parameters:
Name Type Description
rawResult object The raw result to score.
Source:
Returns:
Type
float
Performs a search against the index & returns results.
Parameters:
Name Type Description
query string The user's query to search on.
limit int The number of results to return.
start int The starting offset of the results.
Source:
Returns:
Type
array

toJson() → {string}

Dumps the index to a JSON string. Useful for persisting an already-built index.
Source:
Returns:
Type
string