Matthew Daly's Blog

I'm a web developer in Norfolk. This is my blog...

18th April 2015 3:05 pm

How I Added Search to My Site With Lunr.js

As I mentioned a while back, I recently switched the search on my site from Google’s site-specific search to Lunr.js. Since my site is built with a static site generator, I can’t implement search using database queries, and I was keen to have an integrated search method that would be fast and not require server-side scripting, and Lunr.js seemed to fit the bill.

The first task in implementing it was to generate the index. As I wrote the Grunt task that generates the blog, I amended that task to generate an index at the same time as I generated the posts. I installed Lunr.js with the following command:

npm install lunr --save

I then imported it in the task, and set up the field names:

var lunr = require('lunr');
searchIndex = lunr(function () {
this.field('title', { boost: 10 });

This defined fields for the title, body, and hyperlink, and set the hyperlink as the reference. The variable searchIndex represents the Lunr index.

Next, I looped through the posts, and passed the appropriate details to be added to the index:

for (post in post_items) {
var doc = {
'title': post_items[post].meta.title,
'body': post_items[post].post.rawcontent,
'href': post_items[post].path
store[doc.href] = {
'title': doc.title

At this point, post_items represents an array of objects, with each object representing a blog post. Note that the body field is set to the value of the item’s attribute post.rawcontent, which represents the raw Markdown rather than the compiled HTML.

I then store the title in the store object, so that it can be accessed using the href field as a key.

I then do the same thing when generating the pages:

// Add them to the index
var doc = {
'title': data.meta.title,
'href': permalink + '/'
store[doc.href] = {
'title': data.meta.title

Note that this is already inside the loop that generates the pages, so I don’t include that.

We then write the index to a file:

// Write index
grunt.file.write(options.www.dest + '/lunr.json', JSON.stringify({
index: searchIndex.toJSON(),
store: store

That takes care of generating our index, but we need to implement some client-side code to handle the search. We need to include Lunr.js on the client side as well, (I recommend using Bower to do so), alongside jQuery. If you include both, the following code should do the trick:

$(document).ready(function () {
'use strict';
// Set up search
var index, store;
$.getJSON('/lunr.json', function (response) {
// Create index
index = lunr.Index.load(response.index);
// Create store
store =;
// Handle search
$('input#search').on('keyup', function () {
// Get query
var query = $(this).val();
// Search for it
var result =;
// Output it
var resultdiv = $('ul.searchresults');
if (result.length === 0) {
// Hide results
} else {
// Show results
for (var item in result) {
var ref = result[item].ref;
var searchitem = '<li><a href="' + ref + '">' + store[ref].title + '</a></li>';

This should be easy to understand. On load, we fetch and parse the lunr.json file from the server, and load the index. We then set up an event handler for the keyup event on an input with the ID of search. We get the value of the input, and query our index, and we loop through our results and display them.

I was pleased with how straightforward it was to implement search with Lunr.js, and it works well. It’s also a lot faster than any server-side solution since the index is generated during the build process, and is loaded with the rest of the site, so the only factor in the speed of the response is how quick your browser executes JavaScript. You could probably also use it with a Node.js application by generating the index dynamically, although you’d probably want to cache it to some extent.

Recent Posts

Using Phpiredis With Laravel

Simple Fuzzy Search With Laravel and Postgresql

A Generic PHP SMS Library

Installing Nginx Unit on Ubuntu

Making Internal Requests With Laravel

About me

I'm a web and mobile app developer based in Norfolk. My skillset includes Python, PHP and Javascript, and I have extensive experience working with CodeIgniter, Laravel, Django, Phonegap and Angular.js.