infinity.fts¶
Provides fulltext search functionality within one infinity instance. All threads share the same indexes.
Each namespace is a separate index. The index files are stored in the folder fts/<namespace>/ next to the folder containing the INFINITY.JS executable (e.g. dist/bin/fts/books/ for dist/bin/win-x64/infinity.exe). New documents are first held in memory and written to the index files when close() is called or when enough documents have been collected. Words listed in resources/stopwords.txt (in the same parent folder) are not indexed.
Changes since the last write are additionally recorded in a log file. If the runtime was not shut down properly, open() replays this log and sends an event onFtsIndexRestore for each recorded change (see infinity.event). The event parameter is an object {namespace: string, action: 'add' | 'delete', docId: number}. The documents themselves are not restored, the application has to add (or remove) them again in the event handler, otherwise these changes are lost.
Module: infinity.fts
Example:
infinity.loadModule('infinity.fts');
infinity.event.register('onFtsIndexRestore', function (params: any): void {
//re-add (or remove) the document params.docId of params.namespace here
console.warn('Restore ' + params.action + ' of document ' + params.docId);
});
infinity.fts.open('books');
let doc = {title: 'The Little Prince', author: 'Antoine de Saint-Exupéry', genre: 'Fantasy'};
infinity.fts.add('books', 1, doc);
infinity.fts.add('books', 2, {title: 'The Adventures of Pinocchio', author: 'Carlo Collodi', genre: 'Fantasy'});
infinity.fts.add('books', 3, {title: 'Fahrenheit 451', author: 'Ray Bradbury', genre: 'Drama'});
console.debug(infinity.fts.count('books'));
//3
//Simple search:
console.debug(infinity.fts.find('books', 'little'));
//Phrase search: all search terms must occur in the exact same order
console.debug(infinity.fts.find('books', '"The Adventures of Pinocchio"'));
//And search: all search terms must occur, the order is not considered (default, or +)
console.debug(infinity.fts.find('books', 'Adventures +Pinocchio'));
//Or search: at least one search term must occur (|)
console.debug(infinity.fts.find('books', 'Adventures |Pinocchio'));
//Search with exclusion: the search term before the hyphen must occur, the term after the hyphen must not occur (-)
console.debug(infinity.fts.find('books', 'Adventures -Pinocchio'));
//Wildcard search: the search term must start with the characters before the asterisk (*)
console.debug(infinity.fts.find('books', 'Pin*'));
//Searching within specified fields
console.debug(infinity.fts.find('books', 'fantasy', 10, ['genre']));
infinity.fts.remove('books', 3);
console.debug(infinity.fts.find('books', 'drama', 10, ['genre']));
infinity.fts.close('books');
resultArray¶
Extends: Array<{id: number, relevance: number}>
An array of objects containing the search results of find(). id is the document ID, relevance a value between 0 and 1. The results are sorted by relevance, highest first.
stringArray¶
Extends: Array<string>
An array of strings.
add()¶
Adds the given document to the fulltext index of the specified namespace. If a document with the same ID already exists, it is replaced (a document without indexable words just removes the old one). The document itself is not stored, only its words are indexed.
Each property of the document is indexed as a separate attribute (field), which can be used to restrict a search with the attributes parameter of find(). Property values are converted to strings; empty values are ignored. Words are indexed case-insensitively; words shorter than 3 characters and stopwords are ignored, longer words are truncated to 16 characters.
Signature:
add( namespace: string, docId: number, document: object ): void
Parameters:
-
namespace:
stringSpecifies the namespace for the document to be stored under.
-
docId:
numberThe unique document identification number (integer).
-
document:
objectThe data to be indexed. Throws an error if the value is not an object.
Example:
let doc = {title: 'The Little Prince', author: 'Antoine de Saint-Exupéry', genre: 'Fantasy'};
infinity.fts.add('books', 1, doc);
infinity.fts.add('books', 2, {title: 'The Adventures of Pinocchio', author: 'Carlo Collodi', genre: 'Fantasy'});
clear()¶
Removes all documents from the fulltext index of the specified namespace.
Signature:
clear( namespace: string ): void
Parameters:
- namespace:
stringThe namespace under which the data has to be cleared.
Example:
infinity.fts.clear('books');
close()¶
Closes the fulltext index of the specified namespace and writes all documents held in memory to the index files. Should be called before the program ends.
Signature:
close( namespace: string ): void
Parameters:
- namespace:
stringThe namespace of the library to be closed.
Example:
infinity.fts.close('books');
count()¶
Returns the number of indexed documents of the specified namespace.
Signature:
count( namespace: string ): number
Parameters:
- namespace:
stringThe namespace of the fulltext index.
Return type: number
Example:
console.debug(infinity.fts.count('books'));
find()¶
Returns the results of a search with the given query under the specified namespace.
The query consists of search terms separated by spaces (at most 10 terms are used). By default all terms must occur. A term can be prefixed with + (must occur), | (or) or - (must not occur); the operator can also be written as a separate word (e.g. Adventures | Pinocchio). A term ending with * matches all words starting with the given characters (at least 2 characters before the * since 3.12, before at least 3; shorter terms are ignored like short words). If the whole query is enclosed in double quotes, a phrase search is performed: all terms must occur in the given order. The search is case-insensitive.
Signature:
find( namespace: string, query: string, maxResults?: number, attributes?: infinity.fts.stringArray ): infinity.fts.resultArray
Parameters:
-
namespace:
stringThe namespace for the search.
-
query:
stringThe search query. Simple term searches, "and" searches, "or" searches, searches with term exclusion, wildcard searches and phrase searches are supported.
-
maxResults:
number, optionalThe maximum number of results to be returned.
0or omitted means no limit. -
attributes:
infinity.fts.stringArray, optionalNames of the document properties (attributes) to search in. If omitted or empty, all attributes are searched. Names that were never indexed are ignored; if none of the names is known, nothing is found.
Return type: infinity.fts.resultArray
Example:
console.debug(infinity.fts.find('books', 'fantasy', 10, ['genre']));
open()¶
Opens the fulltext index of the specified namespace and loads the existing index files. The index folder is created if it does not exist. If the runtime was not shut down properly, the change log is replayed by sending onFtsIndexRestore events (see the description at the top of this page).
Signature:
open( namespace: string ): void
Parameters:
- namespace:
stringThe namespace under which the fulltext index should be opened.
Example:
infinity.fts.open('books');
purge()¶
Deletes all index files and the index folder of the specified namespace. An open namespace is closed first and opened again with an empty index.
Signature:
purge( namespace: string ): void
Parameters:
- namespace:
stringThe namespace whose index files should be deleted.
Example:
infinity.fts.purge('books');
remove()¶
Removes the specified document from the fulltext index of the specified namespace.
Signature:
remove( namespace: string, docId: number ): void
Parameters:
-
namespace:
stringThe namespace under which the document should be removed.
-
docId:
numberThe document identification number of the document to be removed.
Example:
infinity.fts.remove('books', 2);