The type of the documents being indexed.
const documents = [
{
id: 1,
title: 'Moby Dick',
text: 'Call me Ishmael. Some years ago...',
category: 'fiction'
},
{
id: 2,
title: 'Zen and the Art of Motorcycle Maintenance',
text: 'I can see by my watch...',
category: 'fiction'
},
{
id: 3,
title: 'Neuromancer',
text: 'The sky above the port was...',
category: 'fiction'
},
{
id: 4,
title: 'Zen and the Art of Archery',
text: 'At first sight it must seem...',
category: 'non-fiction'
},
// ...and more
]
// Create a search engine that indexes the 'title' and 'text' fields for
// full-text search. Search results will include 'title' and 'category' (plus the
// id field, that is always stored and returned)
const miniSearch = new MiniSearch({
fields: ['title', 'text'],
storeFields: ['title', 'category']
})
// Add documents to the index
miniSearch.addAll(documents)
// Search for documents:
let results = miniSearch.search('zen art motorcycle')
// => [
// { id: 2, title: 'Zen and the Art of Motorcycle Maintenance', category: 'fiction', score: 2.77258 },
// { id: 4, title: 'Zen and the Art of Archery', category: 'non-fiction', score: 1.38629 }
// ]
Configuration options
// Create a search engine that indexes the 'title' and 'text' fields of your
// documents:
const miniSearch = new MiniSearch({ 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 = new MiniSearch({ idField: 'key', fields: ['title', 'text'] })
// The full set of options (here with their default value) is:
const miniSearch = new MiniSearch({
// 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: []
})
Number of documents in the index
Adds a document to the index
The document to be indexed
Adds all the given documents to the index
An array of documents to be indexed
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.
An array of documents to be indexed
Configuration options
A promise resolving to undefined
when the indexing is done
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.
// 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 }
// ]
Query string to be expanded into suggestions
Search options. The supported options and default values
are the same as for the search
method, except that by default prefix
search is performed on the last term in the query.
A sorted array of suggestions sorted by relevance score.
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:
The document to be removed
Removes all the given documents from the index. If called with no arguments, it removes all documents from the index.
The documents to be removed. If this argument is omitted, all documents are removed. Note that, for removing all documents, it is more efficient to call this method with no arguments than to pass all documents.
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.
// 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'
})
Query string to search for
Allows serialization of the index to JSON, to possibly store it and later
deserialize it with MiniSearch.loadJSON
.
Normally one does not directly call this method, but rather call the
standard JavaScript JSON.stringify()
passing the MiniSearch
instance,
and JavaScript will internally call this method. Upon deserialization, one
must pass to loadJSON
the same options used to create the original
instance that was serialized.
// Serialize the index:
let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
miniSearch.addAll(documents)
const json = JSON.stringify(miniSearch)
// Later, to deserialize it:
miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
A plain-object serializeable representation of the search index.
Returns the default value of an option. It will throw an error if no option with the given name exists.
Name of the option
The default value of the given option
// 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"'
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.
// If the index was serialized with:
let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
miniSearch.addAll(documents)
const json = JSON.stringify(miniSearch)
// It can later be deserialized like this:
miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
JSON-serialized index
configuration options, same as the constructor
An instance of MiniSearch deserialized from the given JSON.
Generated using TypeDoc
MiniSearch is the main entrypoint class, implementing a full-text search engine in memory.