Constructor
+ + +new MiniSearch(options)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 138 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Examples
+ +// Create a search engine that indexes the 'title' and 'text' fields of your
+// documents:
+const miniSearch = MiniSearch.new({ fields: ['title', 'text'] })
+
+ // Your documents are assumed to include a unique 'id' field, but if you want
+// to use a different field for document identification, you can set the
+// 'idField' option:
+const miniSearch = MiniSearch.new({ idField: 'key', fields: ['title', 'text'] })
+
+ // The full set of options (here with their default value) is:
+const miniSearch = MiniSearch.new({
+ // idField: field that uniquely identifies a document
+ idField: 'id',
+
+ // extractField: function used to get the value of a field in a document.
+ // By default, it assumes the document is a flat object with field names as
+ // property keys and field values as string property values, but custom logic
+ // can be implemented by setting this option to a custom extractor function.
+ extractField: (document, fieldName) => document[fieldName],
+
+ // tokenize: function used to split fields into individual terms. By
+ // default, it is also used to tokenize search queries, unless a specific
+ // `tokenize` search option is supplied. When tokenizing an indexed field,
+ // the field name is passed as the second argument.
+ tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),
+
+ // processTerm: function used to process each tokenized term before
+ // indexing. It can be used for stemming and normalization. Return a falsy
+ // value in order to discard a term. By default, it is also used to process
+ // search queries, unless a specific `processTerm` option is supplied as a
+ // search option. When processing a term from a indexed field, the field
+ // name is passed as the second argument.
+ processTerm: (term, _fieldName) => term.toLowerCase(),
+
+ // searchOptions: default search options, see the `search` method for
+ // details
+ searchOptions: undefined,
+
+ // fields: document fields to be indexed. Mandatory, but not set by default
+ fields: undefined
+
+ // storeFields: document fields to be stored and returned as part of the
+ // search results.
+ storeFields: []
+})
+
+
+
+
+ Parameters:
+ + +Name | + + +Type | + + + + + +Description | +||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
+
+
+ + + +Object + + + + | + + + + + +Configuration options +Properties+ + +
|
+
Members
+ + + +documentCount :number
+ + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 463 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Number of documents in the index
+Type:
+-
+
- + +number + + + +
Methods
+ + + + + + +(static) getDefault(optionName) → {*}
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 509 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Get the default value of an option. It will throw an error if no option with +the given name exists.
+Examples
+ +// Get default tokenizer
+MiniSearch.getDefault('tokenize')
+
+ // Get default term processor
+MiniSearch.getDefault('processTerm')
+
+ // Unknown options will throw an error
+MiniSearch.getDefault('notExisting')
+// => throws 'MiniSearch: unknown option "notExisting"'
+
+
+
+
+ Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
optionName |
+
+
+ + + +string + + + + | + + + + + +name of the option |
+
Returns:
+ + +the default value of the given option
+-
+
- + Type + +
- + +* + + + +
(static) loadJSON(json, options) → {MiniSearch}
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 482 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Deserializes a JSON index (serialized with miniSearch.toJSON()
) and
+instantiates a MiniSearch instance. It should be given the same options
+originally used when serializing the index.
Warning: JSON (de)serialization of the index is currently tightly +coupled to the index implementation. For this reason, the current +implementation is to be considered a beta feature, subject to breaking +changes changes in future releases. If a breaking change is introduced, +though, it will be properly reported in the changelog.
+Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
json |
+
+
+ + + +string + + + + | + + + + + +JSON-serialized index |
+
options |
+
+
+ + + +Object + + + + | + + + + + +configuration options, same as the constructor |
+
Returns:
+ + +an instance of MiniSearch
+-
+
- + Type + +
- + +MiniSearch + + + +
add(document)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 181 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Adds a document to the index
+Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
document |
+
+
+ + + +Object + + + + | + + + + + +the document to be indexed |
+
addAll(documents)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 206 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Adds all the given documents to the index
+Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
documents |
+
+
+ + + +Array.<Object> + + + + | + + + + + +an array of documents to be indexed |
+
addAllAsync(documents, optionsopt) → {Promise}
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 222 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Adds all the given documents to the index asynchronously.
+Returns a promise that resolves to undefined when the indexing is done. This +method is useful when index many documents, to avoid blocking the main +thread. The indexing is performed asynchronously and in chunks.
+Parameters:
+ + +Name | + + +Type | + + +Attributes | + + + + +Description | +||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
documents |
+
+
+ + + +Array.<Object> + + + + | + + ++ + + + + + | + + + + +an array of documents to be indexed |
+ ||||||||
options |
+
+
+ + + +Object + + + + | + + +
+
+ <optional> + + + + + + |
+
+
+
+
+ Configuration options +Properties+ + +
|
+
Returns:
+ + +A promise resolving to null
when the indexing is done
-
+
- + Type + +
- + +Promise + + + +
autoSuggest(queryString, optionsopt) → {Array.<{suggestion: string, score: number}>}
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 441 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Provide suggestions for the given search query
+The result is a list of suggested modified search queries, derived from the +given search query, each with a relevance score, sorted by descending score.
+Examples
+ +// Get suggestions for 'neuro':
+miniSearch.autoSuggest('neuro')
+// => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
+
+ // Get suggestions for 'zen ar':
+miniSearch.autoSuggest('zen ar')
+// => [
+// { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
+// { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
+// ]
+
+ // Correct spelling mistakes using fuzzy search:
+miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 })
+// => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
+
+ // Get suggestions for 'zen ar', but only within the 'fiction' category
+// (assuming that 'category' is a stored field):
+miniSearch.autoSuggest('zen ar', {
+ filter: (result) => result.category === 'fiction'
+})
+// => [
+// { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
+// { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
+// ]
+
+
+
+
+ Parameters:
+ + +Name | + + +Type | + + +Attributes | + + + + +Description | +
---|---|---|---|
queryString |
+
+
+ + + +string + + + + | + + ++ + + + + + | + + + + +Query string to be expanded into suggestions |
+
options |
+
+
+ + + +Object + + + + | + + +
+
+ <optional> + + + + + + |
+
+
+
+
+ Search options. The supported options and default values are the same as for the |
+
Returns:
+ + +A sorted array of suggestions sorted by relevance score.
+-
+
- + Type + +
- + +Array.<{suggestion: string, score: number}> + + + +
remove(document)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 251 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Removes the given document from the index.
+The document to delete must NOT have changed between indexing and deletion, +otherwise the index will be corrupted. Therefore, when reindexing a document +after a change, the correct order of operations is:
+-
+
- remove old version +
- apply changes +
- index new version +
Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
document |
+
+
+ + + +Object + + + + | + + + + + +the document to be indexed |
+
search(queryString, optionsopt) → {Array.<{id: any, score: number, match: Object}>}
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 374 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Search for documents matching the given search query.
+The result is a list of scored document IDs matching the query, sorted by +descending score, and each including data about which terms were matched and +in which fields.
+Examples
+ +// Search for "zen art motorcycle" with default options: terms have to match
+// exactly, and individual terms are joined with OR
+miniSearch.search('zen art motorcycle')
+// => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
+
+ // Search only in the 'title' field
+miniSearch.search('zen', { fields: ['title'] })
+
+ // Boost a field
+miniSearch.search('zen', { boost: { title: 2 } })
+
+ // Search for "moto" with prefix search (it will match documents
+// containing terms that start with "moto" or "neuro")
+miniSearch.search('moto neuro', { prefix: true })
+
+ // Search for "ismael" with fuzzy search (it will match documents containing
+// terms similar to "ismael", with a maximum edit distance of 0.2 term.length
+// (rounded to nearest integer)
+miniSearch.search('ismael', { fuzzy: 0.2 })
+
+ // Mix of exact match, prefix search, and fuzzy search
+miniSearch.search('ismael mob', {
+ prefix: true,
+ fuzzy: 0.2
+})
+
+ // Perform fuzzy and prefix search depending on the search term. Here
+// performing prefix and fuzzy search only on terms longer than 3 characters
+miniSearch.search('ismael mob', {
+ prefix: term => term.length > 3
+ fuzzy: term => term.length > 3 ? 0.2 : null
+})
+
+ // Combine search terms with AND (to match only documents that contain both
+// "motorcycle" and "art")
+miniSearch.search('motorcycle art', { combineWith: 'AND' })
+
+ // Filter only results in the 'fiction' category (assuming that 'category'
+// is a stored field)
+miniSearch.search('motorcycle art', {
+ filter: (result) => result.category === 'fiction'
+})
+
+
+
+
+ Parameters:
+ + +Name | + + +Type | + + +Attributes | + + + + +Description | +|||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
queryString |
+
+
+ + + +string + + + + | + + ++ + + + + + | + + + + +Query string to search for |
+ |||||||||||||||||||||||||||||||||||||||||||||
options |
+
+
+ + + +Object + + + + | + + +
+
+ <optional> + + + + + + |
+
+
+
+
+ Search options. Each option, if not given, defaults to the corresponding value of Properties+ + +
|
+
Returns:
+ + +A sorted array of scored document IDs matching the search
+-
+
- + Type + +
- + +Array.<{id: any, score: number, match: Object}> + + + +
toJSON() → {Object}
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 605 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Allows serialization of the index to JSON, to possibly store it and later +deserialize it with MiniSearch.loadJSON
+Warning: JSON (de)serialization of the index is currently tightly +coupled to the index implementation. For this reason, the current +implementation is to be considered a beta feature, subject to breaking +changes changes in future releases. If a breaking change is introduced, +though, it will be reported in the changelog.
+Returns:
+ + +the serializeable representation of the search index
+-
+
- + Type + +
- + +Object + + + +
Type Definitions
+ + + + + + +extractField(document, fieldName)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 58 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
document |
+
+
+ + + +Object + + + + | + + + + + +A document object |
+
fieldName |
+
+
+ + + +string + + + + | + + + + + +Name of the field to extract |
+
Returns:
+ + +string - Value of the field
+filter(result)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 297 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
result |
+
+
+ + + +Object + + + + | + + + + + +A search result |
+
Returns:
+ + +boolean - true
to keep the result, false
to filter it out
fuzzyFn(term, i, terms)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 289 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
term |
+
+
+ + + +string + + + + | + + + + + +Search term |
+
i |
+
+
+ + + +number + + + + | + + + + + +Index of the search term in the tokenized search query |
+
terms |
+
+
+ + + +Array.<string> + + + + | + + + + + +Array of all query terms |
+
Returns:
+ + +number|false - Maximum edit distance, or false
to not perform fuzzy search
prefixFn(term, i, terms)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 281 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
term |
+
+
+ + + +string + + + + | + + + + + +Search term |
+
i |
+
+
+ + + +number + + + + | + + + + + +Index of the term in the query terms array |
+
terms |
+
+
+ + + +Array.<string> + + + + | + + + + + +Array of all query terms |
+
Returns:
+ + +boolean - true
to perform prefix search, false
to not perform it
processTerm(text, fieldNamenullable)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 72 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Parameters:
+ + +Name | + + +Type | + + +Attributes | + + + + +Description | +
---|---|---|---|
text |
+
+
+ + + +string + + + + | + + ++ + + + + + | + + + + +The text to tokenize |
+
fieldName |
+
+
+ + + +string + + + + | + + +
+
+
+
+ <nullable> + + + + |
+
+
+
+
+ The name of the field to tokenize |
+
Returns:
+ + +string|null|undefined|false - Processed term, or a falsy value to discard the term
+tokenize(text, fieldNamenullable)
+ + + + + + +-
+
+
+
- Source: +
- + MiniSearch.js, line 65 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Parameters:
+ + +Name | + + +Type | + + +Attributes | + + + + +Description | +
---|---|---|---|
text |
+
+
+ + + +string + + + + | + + ++ + + + + + | + + + + +Text to tokenize |
+
fieldName |
+
+
+ + + +string + + + + | + + +
+
+
+
+ <nullable> + + + + |
+
+
+
+
+ Name of the field to tokenize |
+
Returns:
+ + +string[] - Tokenized terms
+