Getting Started

This guide will walk you through setting up your first search index with Lunr. It assumes you have some familiarity with JavaScript. After finishing this guide you will have a script that will be able to perform a search on a collection of documents.

Installation

Install Lunr with npm:

$ npm install lunr

Lunr is also available as a single file for use in browsers using script tags. It can be included from the unpkg CDN like this:

<script src="https://unpkg.com/lunr/lunr.js"></script>

The following examples will use node.js for simplicity; the same code will work in any JavaScript environment.

Creating an Index

We will create a simple index on a collection of documents and then perform searches on those documents.

First, we need a collection of documents. A document is a JavaScript object. It should have an identifier field that Lunr will use to tell us which documents in the collection matched a search, as well as any other fields that we want to search on.

vardocuments=[{"name":"Lunr","text":"Like Solr, but much smaller, and not as bright."},{"name":"React","text":"A JavaScript library for building user interfaces."},{"name":"Lodash","text":"A modern JavaScript utility library delivering modularity, performance & extras."}]

We will use the above array of documents to build our index. We want to search the text field, and the name field will be our identifier. Let’s define our index and add these documents to it.