or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-querying.mdindex-building.mdindex.mdsearching.mdtext-processing.mdutilities.md
tile.json

tessl/npm-lunr

Simple full-text search in your browser.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lunr@2.3.x

To install, run

npx @tessl/cli install tessl/npm-lunr@2.3.0

index.mddocs/

Lunr

Lunr is a lightweight, client-side full-text search library designed for browser-based applications that need to search through JSON documents without requiring server-side infrastructure. It provides a simple API for creating search indexes, supports advanced features like field-specific searches, term boosting, fuzzy matching with wildcards, and edit distance calculations.

Package Information

  • Package Name: lunr
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lunr

Core Imports

// ES6/CommonJS - Lunr works in both browser and Node.js
const lunr = require('lunr');

// Browser global
// <script src="path/to/lunr.js"></script>
// lunr is available globally

Basic Usage

const lunr = require('lunr');

// Create a search index
const idx = lunr(function () {
  this.ref('id');
  this.field('title');
  this.field('body');

  this.add({
    id: '1',
    title: 'Getting Started',
    body: 'This is a tutorial about getting started with Lunr.'
  });

  this.add({
    id: '2', 
    title: 'Advanced Features',
    body: 'Learn about advanced search features in Lunr.'
  });
});

// Search the index
const results = idx.search('tutorial');
// Returns: [{ ref: '1', score: 0.6931471805599453 }]

// Search with field-specific queries
const fieldResults = idx.search('title:Advanced');
// Returns: [{ ref: '2', score: 1.0986122886681098 }]

Architecture

Lunr is built around several key components:

  • Index Building: Core lunr() function and Builder class for creating searchable indexes from documents
  • Search Engine: Index class providing query execution with scoring and ranking
  • Text Processing: Configurable pipeline with stemming, stop word filtering, and tokenization
  • Query System: Advanced query parser supporting field restrictions, wildcards, fuzzy matching, and boolean operators
  • Data Structures: Optimized vector spaces, token sets, and finite state automata for efficient search operations

Capabilities

Index Building

Core functionality for creating search indexes from documents. The main entry point for building searchable indexes with customizable fields, reference keys, and text processing pipelines.

/**
 * Create a new search index using the builder pattern
 * @param {Function} config - Configuration function that receives a Builder instance
 * @returns {lunr.Index} - Built search index ready for querying
 */
function lunr(config);

/**
 * Current version of the Lunr library
 * @type {string}
 */
lunr.version; // "2.3.9"

Index Building

Searching

Search execution engine providing query processing, scoring, and result ranking. Supports both simple string queries and advanced programmatic query building.

class Index {
  /**
   * Search the index using query string syntax
   * @param {string} queryString - Query to search for, supports field restrictions, wildcards, etc.
   * @returns {Array<Object>} - Array of search results with ref and score
   */
  search(queryString);

  /**
   * Build queries programmatically for complex search logic
   * @param {Function} builderFunction - Function that receives a Query builder
   * @returns {Array<Object>} - Array of search results
   */
  query(builderFunction);

  /**
   * Serialize the index to JSON for storage/transmission
   * @returns {Object} - Serialized index data
   */
  toJSON();

  /**
   * Load an index from serialized JSON data
   * @param {Object} serializedIndex - Previously serialized index
   * @returns {lunr.Index} - Reconstructed index instance
   */
  static load(serializedIndex);
}

Searching

Text Processing

Configurable text processing pipeline for tokenization, stemming, and filtering. Includes built-in processors and support for custom pipeline functions.

/**
 * Built-in stemmer for reducing words to root forms
 * @param {lunr.Token} token - Token to stem
 * @returns {lunr.Token} - Stemmed token
 */
lunr.stemmer;

/**
 * Built-in stop word filter for removing common words
 * @param {lunr.Token} token - Token to filter
 * @returns {lunr.Token|undefined} - Token if not a stop word, undefined otherwise
 */
lunr.stopWordFilter;

/**
 * Built-in trimmer for removing non-word characters
 * @param {lunr.Token} token - Token to trim
 * @returns {lunr.Token} - Trimmed token
 */
lunr.trimmer;

/**
 * Default tokenizer for converting strings to tokens
 * @param {string|Object} obj - String or object to tokenize
 * @param {Object} metadata - Optional metadata to attach to tokens
 * @returns {Array<lunr.Token>} - Array of tokens
 */
lunr.tokenizer;

Text Processing

Advanced Querying

Advanced query construction and parsing capabilities. Supports field restrictions, wildcards, fuzzy matching, edit distance, boolean operators, and term boosting.

class Query {
  /**
   * Add a clause to the query
   * @param {Object} clause - Query clause with term, field, and options
   * @returns {lunr.Query} - Query instance for chaining
   */
  clause(clause);

  /**
   * Add a term to the query with options
   * @param {string} term - Search term
   * @param {Object} options - Term options (field, boost, wildcard, etc.)
   * @returns {lunr.Query} - Query instance for chaining
   */
  term(term, options);

  /**
   * Check if the query is negated
   * @returns {boolean} - True if query is negated
   */
  isNegated();
}

// Query constants for wildcard insertion
lunr.Query.wildcard = {
  NONE: 0,      // No wildcards
  LEADING: 1,   // Leading wildcard (e.g., "*term")
  TRAILING: 2   // Trailing wildcard (e.g., "term*")
};

// Query constants for term presence
lunr.Query.presence = {
  OPTIONAL: 1,   // Term is optional (default)
  REQUIRED: 2,   // Term must be present (+term)
  PROHIBITED: 3  // Term must not be present (-term)
};

Advanced Querying

Utilities

Utility functions and data structures used throughout the library. Includes helper functions, error handling, and internal data structures.

// Utility namespace
lunr.utils = {
  /**
   * Print warning message to console
   * @param {string} message - Warning message
   */
  warn(message),

  /**
   * Convert object to string, handling null/undefined
   * @param {*} obj - Object to convert
   * @returns {string} - String representation
   */
  asString(obj),

  /**
   * Shallow clone objects and arrays
   * @param {*} obj - Object to clone
   * @returns {*} - Cloned object
   */
  clone(obj)
};

/**
 * Calculate inverse document frequency for scoring
 * @param {Object} posting - Term posting information
 * @param {number} documentCount - Total number of documents
 * @returns {number} - IDF score
 */
lunr.idf(posting, documentCount);

Utilities

Common Patterns

Creating Indexes with Custom Configuration

const idx = lunr(function () {
  // Set document reference field (default: 'id')
  this.ref('documentId');
  
  // Add fields with optional boost values
  this.field('title', { boost: 10 });
  this.field('body');
  this.field('tags', { boost: 5 });
  
  // Configure search parameters
  this.b(0.75);  // Field length normalization (0-1)
  this.k1(1.2);  // Term frequency saturation
  
  // Add documents
  documents.forEach(function (doc) {
    this.add(doc);
  }, this);
});

Advanced Search Queries

// Field-specific search
idx.search('title:advanced');

// Wildcard search
idx.search('run*');      // Terms starting with "run"
idx.search('*ing');      // Terms ending with "ing"

// Fuzzy search with edit distance
idx.search('tutorial~1'); // Allow 1 character difference

// Boolean queries
idx.search('+required -forbidden optional');

// Term boosting
idx.search('important^10 normal');

Index Serialization

// Serialize index for storage
const serialized = idx.toJSON();
localStorage.setItem('searchIndex', JSON.stringify(serialized));

// Load index from storage
const storedIndex = JSON.parse(localStorage.getItem('searchIndex'));
const idx = lunr.Index.load(storedIndex);