What is lunr?
The lunr npm package is a small, full-text search library for use in a web browser or Node.js environment. It provides a simple search interface for retrieving documents based on a search query. Lunr is designed to be easy to set up and use, without the need for a dedicated backend search server.
What are lunr's main functionalities?
Creating an index
This code sample demonstrates how to create a search index with lunr. Fields to be indexed are specified, and documents are added to the index.
const lunr = require('lunr');
const idx = lunr(function () {
this.field('title');
this.field('body');
this.add({
'title': 'Example',
'body': 'This is an example.'
});
});
Searching the index
Once an index has been created, you can search it using a query string. This code sample searches for the term 'example' in the index.
const results = idx.search('example');
Serializing and loading an index
Lunr allows you to serialize an index to JSON and load it back. This is useful for saving the index to disk or sending it over the network.
const serializedIndex = JSON.stringify(idx);
const loadedIndex = lunr.Index.load(JSON.parse(serializedIndex));
Other packages similar to lunr
elasticlunr
Elasticlunr is a lightweight full-text search engine in JavaScript. It is based on lunr.js but provides more flexibility and is faster than lunr.js. It allows for configuring similarity tuning, custom scoring, and has a chainable API.
fuse.js
Fuse.js is a powerful, lightweight fuzzy-search library with a rich set of options. It is different from lunr in that it performs 'fuzzy' searches, which can find matches even when the search terms are not exactly the same as the indexed terms.
js-search
Js-search is a library that enables efficient search in JavaScript and JSON objects. It supports various search strategies and is more customizable than lunr, allowing for indexing and searching in multiple languages.
algoliasearch
Algolia is a hosted search API that provides a full suite of search features. It is more feature-rich and scalable than lunr, offering real-time search, typo tolerance, and geo-search out of the box. Unlike lunr, it requires an external service and is not a purely client-side solution.