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