# Lunr.js — Full-Text Search Engine for the Browser > A small full-text search library for client-side applications, providing stemming, field boosting, and wildcard queries in an index you build once and query instantly. ## Install Save in your project root: # Lunr.js — Full-Text Search Engine for the Browser ## Quick Use ```bash npm install lunr ``` ```js const lunr = require('lunr'); const idx = lunr(function () { this.field('title'); this.field('body'); this.add({id: 1, title: 'Intro', body: 'Welcome to Lunr'}); }); console.log(idx.search('lunr')); ``` ## Introduction Lunr.js is a small, full-text search library for use in the browser and Node.js. Inspired by Solr, it provides a simple way to build a search index over your documents and query it with features like stemming, boosting, and wildcards, all without external services. ## What Lunr.js Does - Builds a full-text search index from JavaScript objects with configurable fields - Supports stemming, stop word removal, and language-specific text processing - Enables field boosting to prioritize matches in specific document fields - Provides wildcard, fuzzy matching, and term presence/absence operators - Serializes indexes to JSON for pre-building at compile time and loading at runtime ## Architecture Overview Lunr.js constructs an inverted index during the build phase, tokenizing and stemming each document field into terms that map to document references. The index uses a trie-based structure for efficient prefix and wildcard lookups. At query time, the search engine resolves each query clause against the index, scores matches using TF-IDF with optional field boosts, and returns sorted results. ## Self-Hosting & Configuration - Install via npm or include via a script tag from a CDN - Define an index inside the lunr callback, specifying fields and adding documents - Set field boosts with this.field(name, {boost: N}) to weight certain fields higher - Pre-build the index server-side and serialize with JSON.stringify for faster client-side loading - Add language support via lunr-languages plugin packs for stemming and stop words ## Key Features - Zero dependencies and works identically in Node.js and browsers - Pre-built index serialization to JSON for static sites and documentation - Language support for 14+ languages via community stemmer plugins - Query syntax with wildcards, fuzzy matches, boosts, and required/prohibited terms - Pipeline-based text processing that is customizable with user-defined functions ## Comparison with Similar Tools - **Fuse.js** — fuzzy search without indexing; better for small datasets, no stemming or field boosting - **FlexSearch** — faster for very large datasets with context-based scoring; less standard query syntax - **MiniSearch** — similar API with auto-suggest and prefix search; actively maintained alternative - **Algolia** — hosted search-as-a-service with dashboards and analytics; requires a server and API key ## FAQ **Q: Can I update the index after building it?** A: No, Lunr indexes are immutable once built. Rebuild the index when your data changes. **Q: How large a dataset can Lunr handle?** A: It works well for up to tens of thousands of documents. Larger datasets may cause slow build times or high memory use. **Q: Can I use Lunr with TypeScript?** A: Yes, type definitions are available via @types/lunr on npm. **Q: How do I search across multiple fields at once?** A: All indexed fields are searched by default. Use field-prefixed queries like title:keyword to restrict to a specific field. ## Sources - https://github.com/olivernn/lunr.js - https://lunrjs.com/ --- Source: https://tokrepo.com/en/workflows/asset-2e5b7aff Author: AI Open Source