How does MiniSearch find a help article?
Give each article a stable ID, choose the fields to search and choose what should come back with the result. You can search both title and body, rank title matches higher and return just the title and link.
It splits text at Unicode spaces and punctuation and lowercases the resulting words by default. It does not reduce words to a shared root or remove accents by default. Test the names and languages visitors actually type. If you add your own word splitting or accent handling, use matching rules for indexing and searching.
Where does the index live?
MiniSearch runs in memory in the browser or in a Node.js server. In a browser, people can search the index without a request for each word once it has loaded. The index can reveal terms from the documents it contains, even if the result displays only a title.
Keep private text out of a public index. For private search, check access on the server before returning records or snippets. Count index download size, memory and time on a representative device before deciding where it runs. The MIT-licensed library has no per-query fee. Your app supplies the hosting and storage it needs.
How can you check a prefix, typo and removal?
In a scratch folder, install MiniSearch 7.2.0. Save this as check.mjs and run node check.mjs. It uses two fictional help records in memory and makes no network requests when it runs.
import MiniSearch from 'minisearch'
const search = new MiniSearch({
fields: ['title', 'body'],
storeFields: ['title'],
})
search.addAll([
{ id: 'reset', title: 'Password reset', body: 'Recover account access.' },
{ id: 'invoice', title: 'Invoice downloads', body: 'Save a receipt.' },
])
const titles = (query, options) =>
search.search(query, options).map(({ title }) => title)
console.log('prefix', titles('pass', { prefix: true }))
console.log('typo', titles('pasword', { fuzzy: 0.2 }))
search.discard('reset')
console.log('removed', titles('pass', { prefix: true }))
console.log('remaining', titles('invoice'))- Try an unfinished word and a typopass should find Password reset through prefix search. pasword should find it through typo tolerance. The return value contains the stored title, not the article body.
- Remove the old help articleAfter discarding record reset, pass should return nothing. invoice should still find Invoice downloads.
How do you update results or rebuild search?
When an article changes, replace the old indexed record using its ID. When it is deleted, remove it from the index. If visitors load a saved index, update the file they load as well. Otherwise a fresh visit could still show the old article.
MiniSearch can save an index as JSON and load it with the original options. Keep the source articles, IDs and index configuration separately. Those are what you can use to rebuild if the file or the library version changes.