UNPKG

38.5 kBTypeScriptView Raw
1// Type definitions for lunr.js 2.3
2// Project: https://github.com/olivernn/lunr.js, http://lunrjs.com
3// Definitions by: Sean Tan <https://github.com/seantanly>, Andrés Pérez <https://github.com/hokiegeek>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.3
6
7export as namespace lunr;
8export = lunr;
9
10/**
11 * lunr - http://lunrjs.com - A bit like Solr, but much smaller and not as bright
12 * Copyright (C) 2014 Oliver Nightingale
13 * MIT Licensed
14 * @license
15 */
16declare namespace lunr {
17 namespace Builder {
18 /**
19 * A plugin is a function that is called with the index builder as its context.
20 * Plugins can be used to customise or extend the behaviour of the index
21 * in some way. A plugin is just a function, that encapsulated the custom
22 * behaviour that should be applied when building the index.
23 *
24 * The plugin function will be called with the index builder as its argument, additional
25 * arguments can also be passed when calling use. The function will be called
26 * with the index builder as its context.
27 */
28 type Plugin = (this: Builder, ...args: any[]) => void;
29 }
30
31 /**
32 * lunr.Builder performs indexing on a set of documents and
33 * returns instances of lunr.Index ready for querying.
34 *
35 * All configuration of the index is done via the builder, the
36 * fields to index, the document reference, the text processing
37 * pipeline and document scoring parameters are all set on the
38 * builder before indexing.
39 */
40 class Builder {
41 /**
42 * Internal reference to the document reference field.
43 */
44 _ref: string;
45
46 /**
47 * Internal reference to the document fields to index.
48 */
49 _fields: string[];
50
51 /**
52 * The inverted index maps terms to document fields.
53 */
54 invertedIndex: object;
55
56 /**
57 * Keeps track of document term frequencies.
58 */
59 documentTermFrequencies: object;
60
61 /**
62 * Keeps track of the length of documents added to the index.
63 */
64 documentLengths: object;
65
66 /**
67 * Function for splitting strings into tokens for indexing.
68 */
69 tokenizer: typeof tokenizer;
70
71 /**
72 * The pipeline performs text processing on tokens before indexing.
73 */
74 pipeline: Pipeline;
75
76 /**
77 * A pipeline for processing search terms before querying the index.
78 */
79 searchPipeline: Pipeline;
80
81 /**
82 * Keeps track of the total number of documents indexed.
83 */
84 documentCount: number;
85
86 /**
87 * A parameter to control field length normalization, setting this to 0 disabled normalization, 1 fully normalizes field lengths, the default value is 0.75.
88 */
89 _b: number;
90
91 /**
92 * A parameter to control how quickly an increase in term frequency results in term frequency saturation, the default value is 1.2.
93 */
94 _k1: number;
95
96 /**
97 * A counter incremented for each unique term, used to identify a terms position in the vector space.
98 */
99 termIndex: number;
100
101 /**
102 * A list of metadata keys that have been whitelisted for entry in the index.
103 */
104 metadataWhitelist: string[];
105
106 constructor();
107
108 /**
109 * Sets the document field used as the document reference. Every document must have this field.
110 * The type of this field in the document should be a string, if it is not a string it will be
111 * coerced into a string by calling toString.
112 *
113 * The default ref is 'id'.
114 *
115 * The ref should _not_ be changed during indexing, it should be set before any documents are
116 * added to the index. Changing it during indexing can lead to inconsistent results.
117 *
118 * @param ref - The name of the reference field in the document.
119 */
120 ref(ref: string): void;
121
122 /**
123 * Adds a field to the list of document fields that will be indexed. Every document being
124 * indexed should have this field. Null values for this field in indexed documents will
125 * not cause errors but will limit the chance of that document being retrieved by searches.
126 *
127 * All fields should be added before adding documents to the index. Adding fields after
128 * a document has been indexed will have no effect on already indexed documents.
129 *
130 * Fields can be boosted at build time. This allows terms within that field to have more
131 * importance when ranking search results. Use a field boost to specify that matches
132 * within one field are more important than other fields.
133 *
134 * @param fieldName - The name of a field to index in all documents.
135 * @param attributes - Optional attributes associated with this field.
136 */
137 field(
138 fieldName: string,
139 attributes?: {
140 boost?: number | undefined;
141 extractor?: ((doc: object) => string | object | object[]) | undefined;
142 },
143 ): void;
144
145 /**
146 * A parameter to tune the amount of field length normalisation that is applied when
147 * calculating relevance scores. A value of 0 will completely disable any normalisation
148 * and a value of 1 will fully normalise field lengths. The default is 0.75. Values of b
149 * will be clamped to the range 0 - 1.
150 *
151 * @param number - The value to set for this tuning parameter.
152 */
153 b(number: number): void;
154
155 /**
156 * A parameter that controls the speed at which a rise in term frequency results in term
157 * frequency saturation. The default value is 1.2. Setting this to a higher value will give
158 * slower saturation levels, a lower value will result in quicker saturation.
159 *
160 * @param number - The value to set for this tuning parameter.
161 */
162 k1(number: number): void;
163
164 /**
165 * Adds a document to the index.
166 *
167 * Before adding fields to the index the index should have been fully setup, with the document
168 * ref and all fields to index already having been specified.
169 *
170 * The document must have a field name as specified by the ref (by default this is 'id') and
171 * it should have all fields defined for indexing, though null or undefined values will not
172 * cause errors.
173 *
174 * Entire documents can be boosted at build time. Applying a boost to a document indicates that
175 * this document should rank higher in search results than other documents.
176 *
177 * @param doc - The document to add to the index.
178 * @param attributes - Optional attributes associated with this document.
179 */
180 add(doc: object, attributes?: { boost?: number | undefined }): void;
181
182 /**
183 * Builds the index, creating an instance of lunr.Index.
184 *
185 * This completes the indexing process and should only be called
186 * once all documents have been added to the index.
187 */
188 build(): Index;
189
190 /**
191 * Applies a plugin to the index builder.
192 *
193 * A plugin is a function that is called with the index builder as its context.
194 * Plugins can be used to customise or extend the behaviour of the index
195 * in some way. A plugin is just a function, that encapsulated the custom
196 * behaviour that should be applied when building the index.
197 *
198 * The plugin function will be called with the index builder as its argument, additional
199 * arguments can also be passed when calling use. The function will be called
200 * with the index builder as its context.
201 *
202 * @param plugin The plugin to apply.
203 */
204 use(plugin: Builder.Plugin, ...args: any[]): void;
205 }
206
207 namespace Index {
208 interface Attributes {
209 /**
210 * An index of term/field to document reference.
211 */
212 invertedIndex: object;
213 /**
214 * Document vectors keyed by document reference.
215 */
216 documentVectors: { [docRef: string]: Vector };
217 /**
218 * An set of all corpus tokens.
219 */
220 tokenSet: TokenSet;
221 /**
222 * The names of indexed document fields.
223 */
224 fields: string[];
225 /**
226 * The pipeline to use for search terms.
227 */
228 pipeline: Pipeline;
229 }
230
231 /**
232 * A result contains details of a document matching a search query.
233 */
234 interface Result {
235 /**
236 * The reference of the document this result represents.
237 */
238 ref: string;
239
240 /**
241 * A number between 0 and 1 representing how similar this document is to the query.
242 */
243 score: number;
244
245 /**
246 * Contains metadata about this match including which term(s) caused the match.
247 */
248 matchData: MatchData;
249 }
250
251 /**
252 * A query builder callback provides a query object to be used to express
253 * the query to perform on the index.
254 *
255 * @param query - The query object to build up.
256 */
257 type QueryBuilder = (this: Query, query: Query) => void;
258
259 /**
260 * Although lunr provides the ability to create queries using lunr.Query, it also provides a simple
261 * query language which itself is parsed into an instance of lunr.Query.
262 *
263 * For programmatically building queries it is advised to directly use lunr.Query, the query language
264 * is best used for human entered text rather than program generated text.
265 *
266 * At its simplest queries can just be a single term, e.g. `hello`, multiple terms are also supported
267 * and will be combined with OR, e.g `hello world` will match documents that contain either 'hello'
268 * or 'world', though those that contain both will rank higher in the results.
269 *
270 * Wildcards can be included in terms to match one or more unspecified characters, these wildcards can
271 * be inserted anywhere within the term, and more than one wildcard can exist in a single term. Adding
272 * wildcards will increase the number of documents that will be found but can also have a negative
273 * impact on query performance, especially with wildcards at the beginning of a term.
274 *
275 * Terms can be restricted to specific fields, e.g. `title:hello`, only documents with the term
276 * hello in the title field will match this query. Using a field not present in the index will lead
277 * to an error being thrown.
278 *
279 * Modifiers can also be added to terms, lunr supports edit distance and boost modifiers on terms. A term
280 * boost will make documents matching that term score higher, e.g. `foo^5`. Edit distance is also supported
281 * to provide fuzzy matching, e.g. 'hello~2' will match documents with hello with an edit distance of 2.
282 * Avoid large values for edit distance to improve query performance.
283 *
284 * To escape special characters the backslash character '\' can be used, this allows searches to include
285 * characters that would normally be considered modifiers, e.g. `foo\~2` will search for a term "foo~2" instead
286 * of attempting to apply a boost of 2 to the search term "foo".
287 *
288 * @example <caption>Simple single term query</caption>
289 * hello
290 * @example <caption>Multiple term query</caption>
291 * hello world
292 * @example <caption>term scoped to a field</caption>
293 * title:hello
294 * @example <caption>term with a boost of 10</caption>
295 * hello^10
296 * @example <caption>term with an edit distance of 2</caption>
297 * hello~2
298 */
299 type QueryString = string;
300 }
301
302 /**
303 * An index contains the built index of all documents and provides a query interface
304 * to the index.
305 *
306 * Usually instances of lunr.Index will not be created using this constructor, instead
307 * lunr.Builder should be used to construct new indexes, or lunr.Index.load should be
308 * used to load previously built and serialized indexes.
309 */
310 class Index {
311 /**
312 * @param attrs The attributes of the built search index.
313 */
314 constructor(attrs: Index.Attributes);
315
316 /**
317 * Performs a search against the index using lunr query syntax.
318 *
319 * Results will be returned sorted by their score, the most relevant results
320 * will be returned first.
321 *
322 * For more programmatic querying use lunr.Index#query.
323 *
324 * @param queryString - A string containing a lunr query.
325 * @throws {lunr.QueryParseError} If the passed query string cannot be parsed.
326 */
327 search(queryString: Index.QueryString): Index.Result[];
328
329 /**
330 * Performs a query against the index using the yielded lunr.Query object.
331 *
332 * If performing programmatic queries against the index, this method is preferred
333 * over lunr.Index#search so as to avoid the additional query parsing overhead.
334 *
335 * A query object is yielded to the supplied function which should be used to
336 * express the query to be run against the index.
337 *
338 * Note that although this function takes a callback parameter it is _not_ an
339 * asynchronous operation, the callback is just yielded a query object to be
340 * customized.
341 *
342 * @param fn - A function that is used to build the query.
343 */
344 query(fn: Index.QueryBuilder): Index.Result[];
345
346 /**
347 * Prepares the index for JSON serialization.
348 *
349 * The schema for this JSON blob will be described in a
350 * separate JSON schema file.
351 */
352 toJSON(): object;
353
354 /**
355 * Loads a previously serialized lunr.Index
356 *
357 * @param serializedIndex - A previously serialized lunr.Index
358 */
359 static load(serializedIndex: object): Index;
360 }
361
362 /**
363 * Contains and collects metadata about a matching document.
364 * A single instance of lunr.MatchData is returned as part of every
365 * lunr.IndexResult.
366 */
367 class MatchData {
368 /**
369 * A cloned collection of metadata associated with this document.
370 */
371 metadata: object;
372
373 /**
374 * @param term - The term this match data is associated with
375 * @param field - The field in which the term was found
376 * @param metadata - The metadata recorded about this term in this field
377 */
378 constructor(term: string, field: string, metadata: object);
379
380 /**
381 * An instance of lunr.MatchData will be created for every term that matches a
382 * document. However only one instance is required in a lunr.Index~Result. This
383 * method combines metadata from another instance of lunr.MatchData with this
384 * objects metadata.
385 *
386 * @param otherMatchData - Another instance of match data to merge with this one.
387 * @see {@link lunr.Index~Result}
388 */
389 combine(otherMatchData: MatchData): void;
390 }
391
392 /**
393 * A pipeline function maps lunr.Token to lunr.Token. A lunr.Token contains the token
394 * string as well as all known metadata. A pipeline function can mutate the token string
395 * or mutate (or add) metadata for a given token.
396 *
397 * A pipeline function can indicate that the passed token should be discarded by returning
398 * null. This token will not be passed to any downstream pipeline functions and will not be
399 * added to the index.
400 *
401 * Multiple tokens can be returned by returning an array of tokens. Each token will be passed
402 * to any downstream pipeline functions and all will returned tokens will be added to the index.
403 *
404 * Any number of pipeline functions may be chained together using a lunr.Pipeline.
405 *
406 * @param token - A token from the document being processed.
407 * @param i - The index of this token in the complete list of tokens for this document/field.
408 * @param tokens - All tokens for this document/field.
409 */
410 type PipelineFunction = (
411 token: Token,
412 i: number,
413 tokens: Token[],
414 ) => null | Token | Token[];
415
416 /**
417 * lunr.Pipelines maintain an ordered list of functions to be applied to all
418 * tokens in documents entering the search index and queries being ran against
419 * the index.
420 *
421 * An instance of lunr.Index created with the lunr shortcut will contain a
422 * pipeline with a stop word filter and an English language stemmer. Extra
423 * functions can be added before or after either of these functions or these
424 * default functions can be removed.
425 *
426 * When run the pipeline will call each function in turn, passing a token, the
427 * index of that token in the original list of all tokens and finally a list of
428 * all the original tokens.
429 *
430 * The output of functions in the pipeline will be passed to the next function
431 * in the pipeline. To exclude a token from entering the index the function
432 * should return undefined, the rest of the pipeline will not be called with
433 * this token.
434 *
435 * For serialisation of pipelines to work, all functions used in an instance of
436 * a pipeline should be registered with lunr.Pipeline. Registered functions can
437 * then be loaded. If trying to load a serialised pipeline that uses functions
438 * that are not registered an error will be thrown.
439 *
440 * If not planning on serialising the pipeline then registering pipeline functions
441 * is not necessary.
442 */
443 class Pipeline {
444 constructor();
445
446 /**
447 * Register a function with the pipeline.
448 *
449 * Functions that are used in the pipeline should be registered if the pipeline
450 * needs to be serialised, or a serialised pipeline needs to be loaded.
451 *
452 * Registering a function does not add it to a pipeline, functions must still be
453 * added to instances of the pipeline for them to be used when running a pipeline.
454 *
455 * @param fn - The function to check for.
456 * @param label - The label to register this function with
457 */
458 static registerFunction(fn: PipelineFunction, label: string): void;
459
460 /**
461 * Loads a previously serialised pipeline.
462 *
463 * All functions to be loaded must already be registered with lunr.Pipeline.
464 * If any function from the serialised data has not been registered then an
465 * error will be thrown.
466 *
467 * @param serialised - The serialised pipeline to load.
468 */
469 static load(serialised: object): Pipeline;
470
471 /**
472 * Adds new functions to the end of the pipeline.
473 *
474 * Logs a warning if the function has not been registered.
475 *
476 * @param functions - Any number of functions to add to the pipeline.
477 */
478 add(...functions: PipelineFunction[]): void;
479
480 /**
481 * Adds a single function after a function that already exists in the
482 * pipeline.
483 *
484 * Logs a warning if the function has not been registered.
485 *
486 * @param existingFn - A function that already exists in the pipeline.
487 * @param newFn - The new function to add to the pipeline.
488 */
489 after(existingFn: PipelineFunction, newFn: PipelineFunction): void;
490
491 /**
492 * Adds a single function before a function that already exists in the
493 * pipeline.
494 *
495 * Logs a warning if the function has not been registered.
496 *
497 * @param existingFn - A function that already exists in the pipeline.
498 * @param newFn - The new function to add to the pipeline.
499 */
500 before(existingFn: PipelineFunction, newFn: PipelineFunction): void;
501
502 /**
503 * Removes a function from the pipeline.
504 *
505 * @param fn The function to remove from the pipeline.
506 */
507 remove(fn: PipelineFunction): void;
508
509 /**
510 * Runs the current list of functions that make up the pipeline against the
511 * passed tokens.
512 *
513 * @param tokens The tokens to run through the pipeline.
514 */
515 run(tokens: Token[]): Token[];
516
517 /**
518 * Convenience method for passing a string through a pipeline and getting
519 * strings out. This method takes care of wrapping the passed string in a
520 * token and mapping the resulting tokens back to strings.
521 *
522 * @param str - The string to pass through the pipeline.
523 */
524 runString(str: string): string[];
525
526 /**
527 * Resets the pipeline by removing any existing processors.
528 */
529 reset(): void;
530
531 /**
532 * Returns a representation of the pipeline ready for serialisation.
533 *
534 * Logs a warning if the function has not been registered.
535 */
536 toJSON(): PipelineFunction[];
537 }
538
539 namespace Query {
540 /**
541 * Constants for indicating what kind of presence a term must have in matching documents.
542 */
543 enum presence {
544 /**
545 * Term's presence in a document is optional, this is the default value.
546 */
547 OPTIONAL = 1,
548 /**
549 * Term's presence in a document is required, documents that do not contain this term will not be returned.
550 */
551 REQUIRED = 2,
552 /**
553 * Term's presence in a document is prohibited, documents that do contain this term will not be returned.
554 */
555 PROHIBITED = 3,
556 }
557
558 enum wildcard {
559 NONE = 0,
560 LEADING = 1 << 0,
561 TRAILING = 1 << 1,
562 }
563
564 /**
565 * A single clause in a {@link lunr.Query} contains a term and details on how to
566 * match that term against a {@link lunr.Index}.
567 */
568 interface Clause {
569 term: string;
570 /** The fields in an index this clause should be matched against. */
571 fields: string[];
572 /** Any boost that should be applied when matching this clause. */
573 boost: number;
574 /** Whether the term should have fuzzy matching applied, and how fuzzy the match should be. */
575 editDistance: number;
576 /** Whether the term should be passed through the search pipeline. */
577 usePipeline: boolean;
578 /** Whether the term should have wildcards appended or prepended. */
579 wildcard: number;
580 }
581 }
582
583 /**
584 * A lunr.Query provides a programmatic way of defining queries to be performed
585 * against a {@link lunr.Index}.
586 *
587 * Prefer constructing a lunr.Query using the {@link lunr.Index#query} method
588 * so the query object is pre-initialized with the right index fields.
589 */
590 class Query {
591 /**
592 * An array of query clauses.
593 */
594 clauses: Query.Clause[];
595
596 /**
597 * An array of all available fields in a lunr.Index.
598 */
599 allFields: string[];
600
601 /**
602 * @param allFields An array of all available fields in a lunr.Index.
603 */
604 constructor(allFields: string[]);
605
606 /**
607 * Adds a {@link lunr.Query~Clause} to this query.
608 *
609 * Unless the clause contains the fields to be matched all fields will be matched. In addition
610 * a default boost of 1 is applied to the clause.
611 *
612 * @param clause - The clause to add to this query.
613 * @see lunr.Query~Clause
614 */
615 clause(clause: Query.Clause): Query;
616
617 /**
618 * Adds a term to the current query, under the covers this will create a {@link lunr.Query~Clause}
619 * to the list of clauses that make up this query.
620 *
621 * The term is used as is, i.e. no tokenization will be performed by this method. Instead conversion
622 * to a token or token-like string should be done before calling this method.
623 *
624 * The term will be converted to a string by calling `toString`. Multiple terms can be passed as an
625 * array, each term in the array will share the same options.
626 *
627 * @param term - The term to add to the query.
628 * @param [options] - Any additional properties to add to the query clause.
629 * @see lunr.Query#clause
630 * @see lunr.Query~Clause
631 * @example <caption>adding a single term to a query</caption>
632 * query.term("foo")
633 * @example <caption>adding a single term to a query and specifying search fields, term boost and automatic trailing wildcard</caption>
634 * query.term("foo", {
635 * fields: ["title"],
636 * boost: 10,
637 * wildcard: lunr.Query.wildcard.TRAILING
638 * })
639 */
640 term(term: string | string[] | Token | Token[], options: object): Query;
641 }
642
643 class QueryParseError extends Error {
644 name: "QueryParseError";
645 message: string;
646 start: number;
647 end: number;
648
649 constructor(message: string, start: string, end: string);
650 }
651
652 /**
653 * lunr.stemmer is an english language stemmer, this is a JavaScript
654 * implementation of the PorterStemmer taken from http://tartarus.org/~martin
655 *
656 * Implements {lunr.PipelineFunction}
657 *
658 * @param token - The string to stem
659 * @see {@link lunr.Pipeline}
660 */
661 function stemmer(token: Token): Token;
662
663 /**
664 * lunr.generateStopWordFilter builds a stopWordFilter function from the provided
665 * list of stop words.
666 *
667 * The built in lunr.stopWordFilter is built using this generator and can be used
668 * to generate custom stopWordFilters for applications or non English languages.
669 *
670 * @param stopWords - The list of stop words
671 * @see lunr.Pipeline
672 * @see lunr.stopWordFilter
673 */
674 function generateStopWordFilter(stopWords: string[]): PipelineFunction;
675
676 /**
677 * lunr.stopWordFilter is an English language stop word list filter, any words
678 * contained in the list will not be passed through the filter.
679 *
680 * This is intended to be used in the Pipeline. If the token does not pass the
681 * filter then undefined will be returned.
682 *
683 * Implements {lunr.PipelineFunction}
684 *
685 * @param token - A token to check for being a stop word.
686 * @see {@link lunr.Pipeline}
687 */
688 function stopWordFilter(token: Token): Token;
689
690 namespace Token {
691 /**
692 * A token update function is used when updating or optionally
693 * when cloning a token.
694 *
695 * @param str - The string representation of the token.
696 * @param metadata - All metadata associated with this token.
697 */
698 type UpdateFunction = (str: string, metadata: object) => void;
699 }
700
701 /**
702 * A token wraps a string representation of a token
703 * as it is passed through the text processing pipeline.
704 */
705 class Token {
706 /**
707 * @param [str=''] - The string token being wrapped.
708 * @param [metadata={}] - Metadata associated with this token.
709 */
710 constructor(str: string, metadata: object);
711
712 /**
713 * Returns the token string that is being wrapped by this object.
714 */
715 toString(): string;
716
717 /**
718 * Applies the given function to the wrapped string token.
719 *
720 * @example
721 * token.update(function (str, metadata) {
722 * return str.toUpperCase()
723 * })
724 *
725 * @param fn - A function to apply to the token string.
726 */
727 update(fn: Token.UpdateFunction): Token;
728
729 /**
730 * Creates a clone of this token. Optionally a function can be
731 * applied to the cloned token.
732 *
733 * @param fn - An optional function to apply to the cloned token.
734 */
735 clone(fn?: Token.UpdateFunction): Token;
736 }
737
738 /**
739 * A token set is used to store the unique list of all tokens
740 * within an index. Token sets are also used to represent an
741 * incoming query to the index, this query token set and index
742 * token set are then intersected to find which tokens to look
743 * up in the inverted index.
744 *
745 * A token set can hold multiple tokens, as in the case of the
746 * index token set, or it can hold a single token as in the
747 * case of a simple query token set.
748 *
749 * Additionally token sets are used to perform wildcard matching.
750 * Leading, contained and trailing wildcards are supported, and
751 * from this edit distance matching can also be provided.
752 *
753 * Token sets are implemented as a minimal finite state automata,
754 * where both common prefixes and suffixes are shared between tokens.
755 * This helps to reduce the space used for storing the token set.
756 */
757 class TokenSet {
758 constructor();
759
760 /**
761 * Creates a TokenSet instance from the given sorted array of words.
762 *
763 * @param arr - A sorted array of strings to create the set from.
764 * @throws Will throw an error if the input array is not sorted.
765 */
766 fromArray(arr: string[]): TokenSet;
767
768 /**
769 * Creates a token set representing a single string with a specified
770 * edit distance.
771 *
772 * Insertions, deletions, substitutions and transpositions are each
773 * treated as an edit distance of 1.
774 *
775 * Increasing the allowed edit distance will have a dramatic impact
776 * on the performance of both creating and intersecting these TokenSets.
777 * It is advised to keep the edit distance less than 3.
778 *
779 * @param str - The string to create the token set from.
780 * @param editDistance - The allowed edit distance to match.
781 */
782 fromFuzzyString(str: string, editDistance: number): Vector;
783
784 /**
785 * Creates a TokenSet from a string.
786 *
787 * The string may contain one or more wildcard characters (*)
788 * that will allow wildcard matching when intersecting with
789 * another TokenSet.
790 *
791 * @param str - The string to create a TokenSet from.
792 */
793 fromString(str: string): TokenSet;
794
795 /**
796 * Converts this TokenSet into an array of strings
797 * contained within the TokenSet.
798 */
799 toArray(): string[];
800
801 /**
802 * Generates a string representation of a TokenSet.
803 *
804 * This is intended to allow TokenSets to be used as keys
805 * in objects, largely to aid the construction and minimisation
806 * of a TokenSet. As such it is not designed to be a human
807 * friendly representation of the TokenSet.
808 */
809 toString(): string;
810
811 /**
812 * Returns a new TokenSet that is the intersection of
813 * this TokenSet and the passed TokenSet.
814 *
815 * This intersection will take into account any wildcards
816 * contained within the TokenSet.
817 *
818 * @param b - An other TokenSet to intersect with.
819 */
820 intersect(b: TokenSet): TokenSet;
821 }
822
823 namespace tokenizer {
824 /**
825 * The separator used to split a string into tokens. Override this property to change the behaviour of
826 * `lunr.tokenizer` behaviour when tokenizing strings. By default this splits on whitespace and hyphens.
827 *
828 * @see lunr.tokenizer
829 */
830 let separator: RegExp;
831 }
832
833 /**
834 * A function for splitting a string into tokens ready to be inserted into
835 * the search index. Uses `lunr.tokenizer.separator` to split strings, change
836 * the value of this property to change how strings are split into tokens.
837 *
838 * This tokenizer will convert its parameter to a string by calling `toString` and
839 * then will split this string on the character in `lunr.tokenizer.separator`.
840 * Arrays will have their elements converted to strings and wrapped in a lunr.Token.
841 *
842 * @param obj - The object to convert into tokens
843 */
844 function tokenizer(obj?: null | string | object | object[]): Token[];
845
846 /**
847 * lunr.trimmer is a pipeline function for trimming non word
848 * characters from the beginning and end of tokens before they
849 * enter the index.
850 *
851 * This implementation may not work correctly for non latin
852 * characters and should either be removed or adapted for use
853 * with languages with non-latin characters.
854 *
855 * Implements {lunr.PipelineFunction}
856 *
857 * @param token The token to pass through the filter
858 * @see lunr.Pipeline
859 */
860 function trimmer(token: Token): Token;
861
862 /**
863 * A namespace containing utils for the rest of the lunr library
864 */
865 namespace utils {
866 /**
867 * Print a warning message to the console.
868 *
869 * @param message The message to be printed.
870 */
871 function warn(message: string): void;
872
873 /**
874 * Convert an object to a string.
875 *
876 * In the case of `null` and `undefined` the function returns
877 * the empty string, in all other cases the result of calling
878 * `toString` on the passed object is returned.
879 *
880 * @param obj The object to convert to a string.
881 * @return string representation of the passed object.
882 */
883 function asString(obj: any): string;
884 }
885
886 /**
887 * A vector is used to construct the vector space of documents and queries. These
888 * vectors support operations to determine the similarity between two documents or
889 * a document and a query.
890 *
891 * Normally no parameters are required for initializing a vector, but in the case of
892 * loading a previously dumped vector the raw elements can be provided to the constructor.
893 *
894 * For performance reasons vectors are implemented with a flat array, where an elements
895 * index is immediately followed by its value. E.g. [index, value, index, value]. This
896 * allows the underlying array to be as sparse as possible and still offer decent
897 * performance when being used for vector calculations.
898 */
899 class Vector {
900 /**
901 * @param [elements] - The flat list of element index and element value pairs.
902 */
903 constructor(elements: number[]);
904
905 /**
906 * Calculates the position within the vector to insert a given index.
907 *
908 * This is used internally by insert and upsert. If there are duplicate indexes then
909 * the position is returned as if the value for that index were to be updated, but it
910 * is the callers responsibility to check whether there is a duplicate at that index
911 *
912 * @param insertIdx - The index at which the element should be inserted.
913 */
914 positionForIndex(index: number): number;
915
916 /**
917 * Inserts an element at an index within the vector.
918 *
919 * Does not allow duplicates, will throw an error if there is already an entry
920 * for this index.
921 *
922 * @param insertIdx - The index at which the element should be inserted.
923 * @param val - The value to be inserted into the vector.
924 */
925 insert(insertIdx: number, val: number): void;
926
927 /**
928 * Inserts or updates an existing index within the vector.
929 *
930 * @param insertIdx - The index at which the element should be inserted.
931 * @param val - The value to be inserted into the vector.
932 * @param fn - A function that is called for updates, the existing value and the
933 * requested value are passed as arguments
934 */
935 upsert(
936 insertIdx: number,
937 val: number,
938 fn: (existingVal: number, val: number) => number,
939 ): void;
940
941 /**
942 * Calculates the magnitude of this vector.
943 */
944 magnitude(): number;
945
946 /**
947 * Calculates the dot product of this vector and another vector.
948 *
949 * @param otherVector - The vector to compute the dot product with.
950 */
951 dot(otherVector: Vector): number;
952
953 /**
954 * Calculates the cosine similarity between this vector and another
955 * vector.
956 *
957 * @param otherVector - The other vector to calculate the
958 * similarity with.
959 */
960 similarity(otherVector: Vector): number;
961
962 /**
963 * Converts the vector to an array of the elements within the vector.
964 */
965 toArray(): number[];
966
967 /**
968 * A JSON serializable representation of the vector.
969 */
970 toJSON(): number[];
971 }
972
973 const version: string;
974 type ConfigFunction = (this: Builder, builder: Builder) => void;
975}
976
977/**
978 * Convenience function for instantiating a new lunr index and configuring it with the default
979 * pipeline functions and the passed config function.
980 *
981 * When using this convenience function a new index will be created with the following functions
982 * already in the pipeline:
983 *
984 * * lunr.StopWordFilter - filters out any stop words before they enter the index
985 *
986 * * lunr.stemmer - stems the tokens before entering the index.
987 *
988 * Example:
989 *
990 * ```javascript
991 * var idx = lunr(function () {
992 * this.field('title', 10);
993 * this.field('tags', 100);
994 * this.field('body');
995 *
996 * this.ref('cid');
997 *
998 * this.pipeline.add(function () {
999 * // some custom pipeline function
1000 * });
1001 * });
1002 * ```
1003 */
1004declare function lunr(config: lunr.ConfigFunction): lunr.Index;