Convert Figma logo to code with AI

olivernn logolunr.js

A bit like Solr, but much smaller and not as bright

8,902
546
8,902
121

Top Related Projects

JS Search is an efficient, client-side search library for JavaScript and JSON objects

Tiny and powerful JavaScript full-text search engine for browser and Node

Next-Generation full text search library for Browser and Node.js

18,051

Lightweight fuzzy-search, in JavaScript

Based on lunr.js, but more flexible and customized.

A persistent, network resilient, full text search library for the browser and Node.js

Quick Overview

Lunr.js is a lightweight, full-text search library for use in the browser. It provides a simple search solution for small to medium-sized websites and applications without the need for external services or server-side search engines.

Pros

  • Pure JavaScript implementation, requiring no external dependencies
  • Works entirely in the browser, eliminating the need for server-side search infrastructure
  • Supports advanced search features like boosting, wildcard searches, and field-specific searches
  • Small file size, making it ideal for client-side applications

Cons

  • Not suitable for large-scale applications with extensive data sets
  • Performance can degrade with very large indexes
  • Limited language support compared to more comprehensive search solutions
  • Requires all search data to be loaded into the browser's memory

Code Examples

Creating an index and performing a search:

const idx = lunr(function () {
  this.field('title')
  this.field('body')

  this.add({
    "title": "Lunr",
    "body": "Like Solr, but much smaller, and not as bright.",
    "id": 1
  })
})

const results = idx.search("bright")

Boosting fields:

const idx = lunr(function () {
  this.field('title', { boost: 10 })
  this.field('body')
})

Using wildcard searches:

const results = idx.search("lun*")

Getting Started

  1. Install Lunr.js via npm:
npm install lunr
  1. Import and use Lunr in your JavaScript file:
import lunr from 'lunr'

const idx = lunr(function () {
  this.field('title')
  this.field('body')

  this.add({
    "title": "Example",
    "body": "This is an example document",
    "id": 1
  })
})

const results = idx.search("example")
console.log(results)
  1. For browser usage, include the Lunr.js script in your HTML:
<script src="https://unpkg.com/lunr/lunr.js"></script>

Competitor Comparisons

JS Search is an efficient, client-side search library for JavaScript and JSON objects

Pros of js-search

  • Faster indexing and search performance, especially for large datasets
  • More flexible search options, including prefix matching and custom tokenization
  • Smaller bundle size, which can be beneficial for web applications

Cons of js-search

  • Less mature and less widely adopted compared to Lunr.js
  • Fewer built-in features, such as stemming and stop word filtering
  • Limited documentation and community support

Code Comparison

js-search:

const search = new JsSearch.Search('id');
search.addIndex('title');
search.addIndex('author');
search.addDocuments(books);
const results = search.search('javascript');

Lunr.js:

const idx = lunr(function () {
  this.field('title');
  this.field('author');
  this.ref('id');
  books.forEach(book => this.add(book));
});
const results = idx.search('javascript');

Both libraries offer simple APIs for creating search indexes and performing searches. js-search provides a more object-oriented approach, while Lunr.js uses a function-based configuration. js-search allows for easier addition of multiple indexes, while Lunr.js offers a more declarative syntax for defining the index structure.

Tiny and powerful JavaScript full-text search engine for browser and Node

Pros of MiniSearch

  • Faster indexing and searching performance, especially for large datasets
  • Smaller bundle size, making it more suitable for browser-based applications
  • More flexible configuration options for fine-tuning search behavior

Cons of MiniSearch

  • Less mature and less widely adopted compared to Lunr.js
  • Fewer built-in features, requiring more custom implementation for advanced use cases

Code Comparison

MiniSearch:

const miniSearch = new MiniSearch({
  fields: ['title', 'text'],
  storeFields: ['title', 'category']
})
miniSearch.addAll(documents)
const results = miniSearch.search('query')

Lunr.js:

const idx = lunr(function () {
  this.field('title')
  this.field('text')
  documents.forEach(doc => this.add(doc))
})
const results = idx.search('query')

Both libraries offer simple APIs for creating search indexes and performing searches. MiniSearch provides more granular control over field storage and search options, while Lunr.js has a more declarative setup process. MiniSearch's approach may be more intuitive for developers familiar with modern JavaScript practices.

Next-Generation full text search library for Browser and Node.js

Pros of FlexSearch

  • Significantly faster performance, especially for large datasets
  • More flexible configuration options, allowing fine-tuning for specific use cases
  • Supports multiple languages and custom tokenization

Cons of FlexSearch

  • Larger file size, which may impact load times for smaller projects
  • Steeper learning curve due to more complex API and configuration options
  • Less established community and ecosystem compared to Lunr.js

Code Comparison

Lunr.js:

var idx = lunr(function () {
  this.field('title')
  this.field('body')
  this.add({ id: 1, title: 'Foo', body: 'Bar' })
})
var results = idx.search('foo')

FlexSearch:

var index = new FlexSearch({
  encode: "icase",
  tokenize: "forward",
  threshold: 0,
  resolution: 9
});
index.add(1, "Foo Bar");
var results = index.search("foo");

Both libraries offer full-text search capabilities, but FlexSearch provides more granular control over indexing and searching behavior. Lunr.js has a simpler API, making it easier to get started for basic use cases. FlexSearch's advanced features and performance make it more suitable for larger-scale applications or those with specific search requirements.

18,051

Lightweight fuzzy-search, in JavaScript

Pros of Fuse

  • Lightweight and has no dependencies
  • Supports fuzzy searching with customizable threshold
  • Can search through nested objects and arrays

Cons of Fuse

  • Slower performance for large datasets compared to Lunr.js
  • Lacks advanced features like field boosting and wildcard searches

Code Comparison

Fuse:

const fuse = new Fuse(list, {
  keys: ['title', 'author.firstName']
});
const result = fuse.search('old man');

Lunr.js:

const idx = lunr(function () {
  this.field('title')
  this.field('body')
  this.add(documents)
});
const results = idx.search('old man');

Key Differences

  • Fuse performs searches directly on the original data, while Lunr.js builds an index for faster searching
  • Lunr.js offers more advanced query syntax and stemming support
  • Fuse provides more flexibility in fuzzy matching and result ranking

Use Cases

  • Fuse: Ideal for smaller datasets and when fuzzy searching is a priority
  • Lunr.js: Better for larger datasets and applications requiring more complex search functionality

Based on lunr.js, but more flexible and customized.

Pros of elasticlunr.js

  • Supports incremental indexing, allowing for dynamic updates to the search index
  • Offers field-specific boosting for more precise search result ranking
  • Provides a more memory-efficient index structure

Cons of elasticlunr.js

  • Less actively maintained compared to lunr.js
  • Smaller community and fewer resources available
  • May have slightly slower search performance in some scenarios

Code Comparison

lunr.js:

var idx = lunr(function () {
  this.field('title', { boost: 10 })
  this.field('body')
  this.ref('id')
})

elasticlunr.js:

var index = elasticlunr(function () {
  this.addField('title')
  this.addField('body')
  this.setRef('id')
})

Both libraries offer similar APIs for creating and configuring search indexes. However, elasticlunr.js provides more granular control over field-specific settings and allows for dynamic index updates, while lunr.js focuses on simplicity and performance for static indexes.

When choosing between the two, consider factors such as the need for dynamic indexing, specific boosting requirements, and the importance of community support and ongoing maintenance.

A persistent, network resilient, full text search library for the browser and Node.js

Pros of search-index

  • Supports more advanced search features like faceted search and range queries
  • Better performance for larger datasets and complex queries
  • Offers both in-memory and persistent storage options

Cons of search-index

  • Steeper learning curve and more complex setup
  • Larger file size and potentially higher memory usage
  • Less suitable for simple, client-side search scenarios

Code Comparison

search-index example:

const si = require('search-index')
const { index, search } = await si({ name: 'my-index' })
await index([
  { id: '1', text: 'Hello world' },
  { id: '2', text: 'Goodbye world' }
])
const results = await search('world')

lunr.js example:

var idx = lunr(function () {
  this.field('text')
  this.add({ id: '1', text: 'Hello world' })
  this.add({ id: '2', text: 'Goodbye world' })
})
var results = idx.search('world')

Both libraries provide full-text search capabilities for JavaScript applications, but they cater to different use cases. lunr.js is lightweight and easy to use for simple client-side search, while search-index offers more advanced features and better performance for larger datasets and server-side applications. The choice between them depends on the specific requirements of your project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Lunr.js

Join the chat at https://gitter.im/olivernn/lunr.js

Build Status

A bit like Solr, but much smaller and not as bright.

Example

A very simple search index can be created using the following:

var idx = lunr(function () {
  this.field('title')
  this.field('body')

  this.add({
    "title": "Twelfth-Night",
    "body": "If music be the food of love, play on: Give me excess of it…",
    "author": "William Shakespeare",
    "id": "1"
  })
})

Then searching is as simple as:

idx.search("love")

This returns a list of matching documents with a score of how closely they match the search query as well as any associated metadata about the match:

[
  {
    "ref": "1",
    "score": 0.3535533905932737,
    "matchData": {
      "metadata": {
        "love": {
          "body": {}
        }
      }
    }
  }
]

API documentation is available, as well as a full working example.

Description

Lunr.js is a small, full-text search library for use in the browser. It indexes JSON documents and provides a simple search interface for retrieving documents that best match text queries.

Why

For web applications with all their data already sitting in the client, it makes sense to be able to search that data on the client too. It saves adding extra, compacted services on the server. A local search index will be quicker, there is no network overhead, and will remain available and usable even without a network connection.

Installation

Simply include the lunr.js source file in the page that you want to use it. Lunr.js is supported in all modern browsers.

Alternatively an npm package is also available npm install lunr.

Browsers that do not support ES5 will require a JavaScript shim for Lunr to work. You can either use Augment.js, ES5-Shim or any library that patches old browsers to provide an ES5 compatible JavaScript environment.

Features

  • Full text search support for 14 languages
  • Boost terms at query time or boost entire documents at index time
  • Scope searches to specific fields
  • Fuzzy term matching with wildcards or edit distance

Contributing

See the CONTRIBUTING.md file.

NPM DownloadsLast 30 Days