@types/lunr
- Version 2.3.7
- Published
- 40.8 kB
- No dependencies
- MIT license
Install
npm i @types/lunr
yarn add @types/lunr
pnpm add @types/lunr
Overview
TypeScript definitions for lunr
Index
Variables
Functions
Classes
Type Aliases
Namespaces
Variables
variable version
const version: string;
Functions
function generateStopWordFilter
generateStopWordFilter: (stopWords: string[]) => PipelineFunction;
lunr.generateStopWordFilter builds a stopWordFilter function from the provided list of stop words.
The built in lunr.stopWordFilter is built using this generator and can be used to generate custom stopWordFilters for applications or non English languages.
Parameter stopWords
The list of stop words
See Also
lunr.Pipeline
lunr.stopWordFilter
function lunr
lunr: typeof lunr;
Convenience function for instantiating a new lunr index and configuring it with the default pipeline functions and the passed config function.
When using this convenience function a new index will be created with the following functions already in the pipeline:
* lunr.StopWordFilter - filters out any stop words before they enter the index
* lunr.stemmer - stems the tokens before entering the index.
Example:
var idx = lunr(function () {this.field('title', 10);this.field('tags', 100);this.field('body');this.ref('cid');this.pipeline.add(function () {// some custom pipeline function});});
function stemmer
stemmer: (token: Token) => Token;
lunr.stemmer is an english language stemmer, this is a JavaScript implementation of the PorterStemmer taken from http://tartarus.org/~martin
Implements {lunr.PipelineFunction}
Parameter token
The string to stem
See Also
function stopWordFilter
stopWordFilter: (token: Token) => Token;
lunr.stopWordFilter is an English language stop word list filter, any words contained in the list will not be passed through the filter.
This is intended to be used in the Pipeline. If the token does not pass the filter then undefined will be returned.
Implements {lunr.PipelineFunction}
Parameter token
A token to check for being a stop word.
See Also
function tokenizer
tokenizer: typeof tokenizer;
A function for splitting a string into tokens ready to be inserted into the search index. Uses
lunr.tokenizer.separator
to split strings, change the value of this property to change how strings are split into tokens.This tokenizer will convert its parameter to a string by calling
toString
and then will split this string on the character inlunr.tokenizer.separator
. Arrays will have their elements converted to strings and wrapped in a lunr.Token.Parameter obj
The object to convert into tokens
function trimmer
trimmer: (token: Token) => Token;
lunr.trimmer is a pipeline function for trimming non word characters from the beginning and end of tokens before they enter the index.
This implementation may not work correctly for non latin characters and should either be removed or adapted for use with languages with non-latin characters.
Implements {lunr.PipelineFunction}
Parameter token
The token to pass through the filter
See Also
lunr.Pipeline
Classes
class Builder
class Builder {}
lunr.Builder performs indexing on a set of documents and returns instances of lunr.Index ready for querying.
All configuration of the index is done via the builder, the fields to index, the document reference, the text processing pipeline and document scoring parameters are all set on the builder before indexing.
constructor
constructor();
property documentCount
documentCount: number;
Keeps track of the total number of documents indexed.
property documentLengths
documentLengths: {};
Keeps track of the length of documents added to the index.
property documentTermFrequencies
documentTermFrequencies: {};
Keeps track of document term frequencies.
property invertedIndex
invertedIndex: {};
The inverted index maps terms to document fields.
property metadataWhitelist
metadataWhitelist: string[];
A list of metadata keys that have been whitelisted for entry in the index.
property pipeline
pipeline: Pipeline;
The pipeline performs text processing on tokens before indexing.
property searchPipeline
searchPipeline: Pipeline;
A pipeline for processing search terms before querying the index.
property termIndex
termIndex: number;
A counter incremented for each unique term, used to identify a terms position in the vector space.
property tokenizer
tokenizer: typeof tokenizer;
Function for splitting strings into tokens for indexing.
method add
add: (doc: object, attributes?: { boost?: number | undefined }) => void;
Adds a document to the index.
Before adding fields to the index the index should have been fully setup, with the document ref and all fields to index already having been specified.
The document must have a field name as specified by the ref (by default this is 'id') and it should have all fields defined for indexing, though null or undefined values will not cause errors.
Entire documents can be boosted at build time. Applying a boost to a document indicates that this document should rank higher in search results than other documents.
Parameter doc
The document to add to the index.
Parameter attributes
Optional attributes associated with this document.
method b
b: (number: number) => void;
A parameter to tune the amount of field length normalisation that is applied when calculating relevance scores. A value of 0 will completely disable any normalisation and a value of 1 will fully normalise field lengths. The default is 0.75. Values of b will be clamped to the range 0 - 1.
Parameter number
The value to set for this tuning parameter.
method build
build: () => Index;
Builds the index, creating an instance of lunr.Index.
This completes the indexing process and should only be called once all documents have been added to the index.
method field
field: ( fieldName: string, attributes?: { boost?: number | undefined; extractor?: (doc: object) => string | object | object[]; }) => void;
Adds a field to the list of document fields that will be indexed. Every document being indexed should have this field. Null values for this field in indexed documents will not cause errors but will limit the chance of that document being retrieved by searches.
All fields should be added before adding documents to the index. Adding fields after a document has been indexed will have no effect on already indexed documents.
Fields can be boosted at build time. This allows terms within that field to have more importance when ranking search results. Use a field boost to specify that matches within one field are more important than other fields.
Parameter fieldName
The name of a field to index in all documents.
Parameter attributes
Optional attributes associated with this field.
method k1
k1: (number: number) => void;
A parameter that controls the speed at which a rise in term frequency results in term frequency saturation. The default value is 1.2. Setting this to a higher value will give slower saturation levels, a lower value will result in quicker saturation.
Parameter number
The value to set for this tuning parameter.
method ref
ref: (ref: string) => void;
Sets the document field used as the document reference. Every document must have this field. The type of this field in the document should be a string, if it is not a string it will be coerced into a string by calling toString.
The default ref is 'id'.
The ref should _not_ be changed during indexing, it should be set before any documents are added to the index. Changing it during indexing can lead to inconsistent results.
Parameter ref
The name of the reference field in the document.
method use
use: (plugin: Builder.Plugin, ...args: any[]) => void;
Applies a plugin to the index builder.
A plugin is a function that is called with the index builder as its context. Plugins can be used to customise or extend the behaviour of the index in some way. A plugin is just a function, that encapsulated the custom behaviour that should be applied when building the index.
The plugin function will be called with the index builder as its argument, additional arguments can also be passed when calling use. The function will be called with the index builder as its context.
Parameter plugin
The plugin to apply.
class Index
class Index {}
An index contains the built index of all documents and provides a query interface to the index.
Usually instances of lunr.Index will not be created using this constructor, instead lunr.Builder should be used to construct new indexes, or lunr.Index.load should be used to load previously built and serialized indexes.
constructor
constructor(attrs: Index.Attributes);
Parameter attrs
The attributes of the built search index.
method load
static load: (serializedIndex: object) => Index;
Loads a previously serialized lunr.Index
Parameter serializedIndex
A previously serialized lunr.Index
method query
query: (fn: Index.QueryBuilder) => Index.Result[];
Performs a query against the index using the yielded lunr.Query object.
If performing programmatic queries against the index, this method is preferred over lunr.Index#search so as to avoid the additional query parsing overhead.
A query object is yielded to the supplied function which should be used to express the query to be run against the index.
Note that although this function takes a callback parameter it is _not_ an asynchronous operation, the callback is just yielded a query object to be customized.
Parameter fn
A function that is used to build the query.
method search
search: (queryString: string) => Index.Result[];
Performs a search against the index using lunr query syntax.
Results will be returned sorted by their score, the most relevant results will be returned first.
For more programmatic querying use lunr.Index#query.
Parameter queryString
A string containing a lunr query.
Throws
{lunr.QueryParseError} If the passed query string cannot be parsed.
method toJSON
toJSON: () => object;
Prepares the index for JSON serialization.
The schema for this JSON blob will be described in a separate JSON schema file.
class MatchData
class MatchData {}
Contains and collects metadata about a matching document. A single instance of lunr.MatchData is returned as part of every lunr.IndexResult.
constructor
constructor(term: string, field: string, metadata: {});
Parameter term
The term this match data is associated with
Parameter field
The field in which the term was found
Parameter metadata
The metadata recorded about this term in this field
property metadata
metadata: {};
A cloned collection of metadata associated with this document.
method combine
combine: (otherMatchData: MatchData) => void;
An instance of lunr.MatchData will be created for every term that matches a document. However only one instance is required in a lunr.Index~Result. This method combines metadata from another instance of lunr.MatchData with this objects metadata.
Parameter otherMatchData
Another instance of match data to merge with this one.
See Also
class Pipeline
class Pipeline {}
lunr.Pipelines maintain an ordered list of functions to be applied to all tokens in documents entering the search index and queries being ran against the index.
An instance of lunr.Index created with the lunr shortcut will contain a pipeline with a stop word filter and an English language stemmer. Extra functions can be added before or after either of these functions or these default functions can be removed.
When run the pipeline will call each function in turn, passing a token, the index of that token in the original list of all tokens and finally a list of all the original tokens.
The output of functions in the pipeline will be passed to the next function in the pipeline. To exclude a token from entering the index the function should return undefined, the rest of the pipeline will not be called with this token.
For serialisation of pipelines to work, all functions used in an instance of a pipeline should be registered with lunr.Pipeline. Registered functions can then be loaded. If trying to load a serialised pipeline that uses functions that are not registered an error will be thrown.
If not planning on serialising the pipeline then registering pipeline functions is not necessary.
constructor
constructor();
method add
add: (...functions: PipelineFunction[]) => void;
Adds new functions to the end of the pipeline.
Logs a warning if the function has not been registered.
Parameter functions
Any number of functions to add to the pipeline.
method after
after: (existingFn: PipelineFunction, newFn: PipelineFunction) => void;
Adds a single function after a function that already exists in the pipeline.
Logs a warning if the function has not been registered.
Parameter existingFn
A function that already exists in the pipeline.
Parameter newFn
The new function to add to the pipeline.
method before
before: (existingFn: PipelineFunction, newFn: PipelineFunction) => void;
Adds a single function before a function that already exists in the pipeline.
Logs a warning if the function has not been registered.
Parameter existingFn
A function that already exists in the pipeline.
Parameter newFn
The new function to add to the pipeline.
method load
static load: (serialised: object) => Pipeline;
Loads a previously serialised pipeline.
All functions to be loaded must already be registered with lunr.Pipeline. If any function from the serialised data has not been registered then an error will be thrown.
Parameter serialised
The serialised pipeline to load.
method registerFunction
static registerFunction: (fn: PipelineFunction, label: string) => void;
Register a function with the pipeline.
Functions that are used in the pipeline should be registered if the pipeline needs to be serialised, or a serialised pipeline needs to be loaded.
Registering a function does not add it to a pipeline, functions must still be added to instances of the pipeline for them to be used when running a pipeline.
Parameter fn
The function to check for.
Parameter label
The label to register this function with
method remove
remove: (fn: PipelineFunction) => void;
Removes a function from the pipeline.
Parameter fn
The function to remove from the pipeline.
method reset
reset: () => void;
Resets the pipeline by removing any existing processors.
method run
run: (tokens: Token[]) => Token[];
Runs the current list of functions that make up the pipeline against the passed tokens.
Parameter tokens
The tokens to run through the pipeline.
method runString
runString: (str: string) => string[];
Convenience method for passing a string through a pipeline and getting strings out. This method takes care of wrapping the passed string in a token and mapping the resulting tokens back to strings.
Parameter str
The string to pass through the pipeline.
method toJSON
toJSON: () => PipelineFunction[];
Returns a representation of the pipeline ready for serialisation.
Logs a warning if the function has not been registered.
class Query
class Query {}
A lunr.Query provides a programmatic way of defining queries to be performed against a lunr.Index.
Prefer constructing a lunr.Query using the lunr.Index#query method so the query object is pre-initialized with the right index fields.
constructor
constructor(allFields: string[]);
Parameter allFields
An array of all available fields in a lunr.Index.
property allFields
allFields: string[];
An array of all available fields in a lunr.Index.
property clauses
clauses: Query.Clause[];
An array of query clauses.
method clause
clause: (clause: Query.Clause) => Query;
Adds a to this query.
Unless the clause contains the fields to be matched all fields will be matched. In addition a default boost of 1 is applied to the clause.
Parameter clause
The clause to add to this query.
See Also
lunr.Query~Clause
method term
term: (term: string | Token | string[] | Token[], options: object) => Query;
Adds a term to the current query, under the covers this will create a to the list of clauses that make up this query.
The term is used as is, i.e. no tokenization will be performed by this method. Instead conversion to a token or token-like string should be done before calling this method.
The term will be converted to a string by calling
toString
. Multiple terms can be passed as an array, each term in the array will share the same options.Parameter term
The term to add to the query.
Parameter options
Any additional properties to add to the query clause.
Example 1
adding a single term to a query query.term("foo")
Example 2
adding a single term to a query and specifying search fields, term boost and automatic trailing wildcard query.term("foo", { fields: ["title"], boost: 10, wildcard: lunr.Query.wildcard.TRAILING })
See Also
lunr.Query#clause
lunr.Query~Clause
class QueryParseError
class QueryParseError extends Error {}
constructor
constructor(message: string, start: string, end: string);
property end
end: number;
property message
message: string;
property name
name: string;
property start
start: number;
class Token
class Token {}
A token wraps a string representation of a token as it is passed through the text processing pipeline.
constructor
constructor(str: string, metadata: {});
Parameter str
The string token being wrapped.
Parameter metadata
Metadata associated with this token.
method clone
clone: (fn?: Token.UpdateFunction) => Token;
Creates a clone of this token. Optionally a function can be applied to the cloned token.
Parameter fn
An optional function to apply to the cloned token.
method toString
toString: () => string;
Returns the token string that is being wrapped by this object.
method update
update: (fn: Token.UpdateFunction) => Token;
Applies the given function to the wrapped string token.
Parameter fn
A function to apply to the token string.
Example 1
token.update(function (str, metadata) { return str.toUpperCase() })
class TokenSet
class TokenSet {}
A token set is used to store the unique list of all tokens within an index. Token sets are also used to represent an incoming query to the index, this query token set and index token set are then intersected to find which tokens to look up in the inverted index.
A token set can hold multiple tokens, as in the case of the index token set, or it can hold a single token as in the case of a simple query token set.
Additionally token sets are used to perform wildcard matching. Leading, contained and trailing wildcards are supported, and from this edit distance matching can also be provided.
Token sets are implemented as a minimal finite state automata, where both common prefixes and suffixes are shared between tokens. This helps to reduce the space used for storing the token set.
constructor
constructor();
method fromArray
fromArray: (arr: string[]) => TokenSet;
Creates a TokenSet instance from the given sorted array of words.
Parameter arr
A sorted array of strings to create the set from.
Throws
Will throw an error if the input array is not sorted.
method fromFuzzyString
fromFuzzyString: (str: string, editDistance: number) => Vector;
Creates a token set representing a single string with a specified edit distance.
Insertions, deletions, substitutions and transpositions are each treated as an edit distance of 1.
Increasing the allowed edit distance will have a dramatic impact on the performance of both creating and intersecting these TokenSets. It is advised to keep the edit distance less than 3.
Parameter str
The string to create the token set from.
Parameter editDistance
The allowed edit distance to match.
method fromString
fromString: (str: string) => TokenSet;
Creates a TokenSet from a string.
The string may contain one or more wildcard characters (*) that will allow wildcard matching when intersecting with another TokenSet.
Parameter str
The string to create a TokenSet from.
method intersect
intersect: (b: TokenSet) => TokenSet;
Returns a new TokenSet that is the intersection of this TokenSet and the passed TokenSet.
This intersection will take into account any wildcards contained within the TokenSet.
Parameter b
An other TokenSet to intersect with.
method toArray
toArray: () => string[];
Converts this TokenSet into an array of strings contained within the TokenSet.
method toString
toString: () => string;
Generates a string representation of a TokenSet.
This is intended to allow TokenSets to be used as keys in objects, largely to aid the construction and minimisation of a TokenSet. As such it is not designed to be a human friendly representation of the TokenSet.
class Vector
class Vector {}
A vector is used to construct the vector space of documents and queries. These vectors support operations to determine the similarity between two documents or a document and a query.
Normally no parameters are required for initializing a vector, but in the case of loading a previously dumped vector the raw elements can be provided to the constructor.
For performance reasons vectors are implemented with a flat array, where an elements index is immediately followed by its value. E.g. [index, value, index, value]. This allows the underlying array to be as sparse as possible and still offer decent performance when being used for vector calculations.
constructor
constructor(elements: number[]);
Parameter elements
The flat list of element index and element value pairs.
method dot
dot: (otherVector: Vector) => number;
Calculates the dot product of this vector and another vector.
Parameter otherVector
The vector to compute the dot product with.
method insert
insert: (insertIdx: number, val: number) => void;
Inserts an element at an index within the vector.
Does not allow duplicates, will throw an error if there is already an entry for this index.
Parameter insertIdx
The index at which the element should be inserted.
Parameter val
The value to be inserted into the vector.
method magnitude
magnitude: () => number;
Calculates the magnitude of this vector.
method positionForIndex
positionForIndex: (index: number) => number;
Calculates the position within the vector to insert a given index.
This is used internally by insert and upsert. If there are duplicate indexes then the position is returned as if the value for that index were to be updated, but it is the callers responsibility to check whether there is a duplicate at that index
Parameter insertIdx
The index at which the element should be inserted.
method similarity
similarity: (otherVector: Vector) => number;
Calculates the cosine similarity between this vector and another vector.
Parameter otherVector
The other vector to calculate the similarity with.
method toArray
toArray: () => number[];
Converts the vector to an array of the elements within the vector.
method toJSON
toJSON: () => number[];
A JSON serializable representation of the vector.
method upsert
upsert: ( insertIdx: number, val: number, fn: (existingVal: number, val: number) => number) => void;
Inserts or updates an existing index within the vector.
Parameter insertIdx
The index at which the element should be inserted.
Parameter val
The value to be inserted into the vector.
Parameter fn
A function that is called for updates, the existing value and the requested value are passed as arguments
Type Aliases
type ConfigFunction
type ConfigFunction = (this: Builder, builder: Builder) => void;
type PipelineFunction
type PipelineFunction = ( token: Token, i: number, tokens: Token[]) => null | Token | Token[];
A pipeline function maps lunr.Token to lunr.Token. A lunr.Token contains the token string as well as all known metadata. A pipeline function can mutate the token string or mutate (or add) metadata for a given token.
A pipeline function can indicate that the passed token should be discarded by returning null. This token will not be passed to any downstream pipeline functions and will not be added to the index.
Multiple tokens can be returned by returning an array of tokens. Each token will be passed to any downstream pipeline functions and all will returned tokens will be added to the index.
Any number of pipeline functions may be chained together using a lunr.Pipeline.
Parameter token
A token from the document being processed.
Parameter i
The index of this token in the complete list of tokens for this document/field.
Parameter tokens
All tokens for this document/field.
Namespaces
namespace Builder
namespace Builder {}
type Plugin
type Plugin = (this: Builder, ...args: any[]) => void;
A plugin is a function that is called with the index builder as its context. Plugins can be used to customise or extend the behaviour of the index in some way. A plugin is just a function, that encapsulated the custom behaviour that should be applied when building the index.
The plugin function will be called with the index builder as its argument, additional arguments can also be passed when calling use. The function will be called with the index builder as its context.
namespace Index
namespace Index {}
interface Attributes
interface Attributes {}
property documentVectors
documentVectors: { [docRef: string]: Vector };
Document vectors keyed by document reference.
property fields
fields: string[];
The names of indexed document fields.
property invertedIndex
invertedIndex: object;
An index of term/field to document reference.
property pipeline
pipeline: Pipeline;
The pipeline to use for search terms.
property tokenSet
tokenSet: TokenSet;
An set of all corpus tokens.
interface Result
interface Result {}
A result contains details of a document matching a search query.
property matchData
matchData: MatchData;
Contains metadata about this match including which term(s) caused the match.
property ref
ref: string;
The reference of the document this result represents.
property score
score: number;
A number between 0 and 1 representing how similar this document is to the query.
type QueryBuilder
type QueryBuilder = (this: Query, query: Query) => void;
A query builder callback provides a query object to be used to express the query to perform on the index.
Parameter query
The query object to build up.
type QueryString
type QueryString = string;
Although lunr provides the ability to create queries using lunr.Query, it also provides a simple query language which itself is parsed into an instance of lunr.Query.
For programmatically building queries it is advised to directly use lunr.Query, the query language is best used for human entered text rather than program generated text.
At its simplest queries can just be a single term, e.g.
hello
, multiple terms are also supported and will be combined with OR, e.ghello world
will match documents that contain either 'hello' or 'world', though those that contain both will rank higher in the results.Wildcards can be included in terms to match one or more unspecified characters, these wildcards can be inserted anywhere within the term, and more than one wildcard can exist in a single term. Adding wildcards will increase the number of documents that will be found but can also have a negative impact on query performance, especially with wildcards at the beginning of a term.
Terms can be restricted to specific fields, e.g.
title:hello
, only documents with the term hello in the title field will match this query. Using a field not present in the index will lead to an error being thrown.Modifiers can also be added to terms, lunr supports edit distance and boost modifiers on terms. A term boost will make documents matching that term score higher, e.g.
foo^5
. Edit distance is also supported to provide fuzzy matching, e.g. 'hello~2' will match documents with hello with an edit distance of 2. Avoid large values for edit distance to improve query performance.To escape special characters the backslash character '' can be used, this allows searches to include characters that would normally be considered modifiers, e.g.
foo\~2
will search for a term "foo~2" instead of attempting to apply a boost of 2 to the search term "foo".Example 1
Simple single term query hello
Example 2
Multiple term query hello world
Example 3
term scoped to a field title:hello
Example 4
term with a boost of 10 hello^10
Example 5
term with an edit distance of 2 hello~2
namespace Query
namespace Query {}
interface Clause
interface Clause {}
A single clause in a lunr.Query contains a term and details on how to match that term against a lunr.Index.
property boost
boost: number;
Any boost that should be applied when matching this clause.
property editDistance
editDistance: number;
Whether the term should have fuzzy matching applied, and how fuzzy the match should be.
property fields
fields: string[];
The fields in an index this clause should be matched against.
property term
term: string;
property usePipeline
usePipeline: boolean;
Whether the term should be passed through the search pipeline.
property wildcard
wildcard: number;
Whether the term should have wildcards appended or prepended.
enum presence
enum presence { OPTIONAL = 1, REQUIRED = 2, PROHIBITED = 3,}
Constants for indicating what kind of presence a term must have in matching documents.
member OPTIONAL
OPTIONAL = 1
Term's presence in a document is optional, this is the default value.
member PROHIBITED
PROHIBITED = 3
Term's presence in a document is prohibited, documents that do contain this term will not be returned.
member REQUIRED
REQUIRED = 2
Term's presence in a document is required, documents that do not contain this term will not be returned.
namespace Token
namespace Token {}
type UpdateFunction
type UpdateFunction = (str: string, metadata: object) => void;
A token update function is used when updating or optionally when cloning a token.
Parameter str
The string representation of the token.
Parameter metadata
All metadata associated with this token.
namespace tokenizer
namespace tokenizer {}
variable separator
let separator: RegExp;
The separator used to split a string into tokens. Override this property to change the behaviour of
lunr.tokenizer
behaviour when tokenizing strings. By default this splits on whitespace and hyphens.See Also
lunr.tokenizer
namespace utils
namespace utils {}
A namespace containing utils for the rest of the lunr library
function asString
asString: (obj: any) => string;
Convert an object to a string.
In the case of
null
andundefined
the function returns the empty string, in all other cases the result of callingtoString
on the passed object is returned.Parameter obj
The object to convert to a string. string representation of the passed object.
function warn
warn: (message: string) => void;
Print a warning message to the console.
Parameter message
The message to be printed.
Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/lunr
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/lunr)
- HTML<a href="https://www.jsdocs.io/package/@types/lunr"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 5044 ms. - Missing or incorrect documentation? Open an issue for this package.