Search And You Shall Find

Every blog should have a search box. Not because it’s necessary, but because it’s fun to implement. A few weeks ago I ran across a small Javascript library called Lunr.js. It’s basically a small text indexer that can rank search results and it’s written entirely in Javascript, just the way I like it. Setting up an index is really easy: Then you just add some documents to the index: Then you can search for it by simply calling `searchIndex.search(’crummy blog’);` and that will return an array of objects with the properties `ref` and `score`. `ref` is the `id` property of the indexed document and `score` is, well, how well it scored. The array will be sorted with the highest scoring result first in the array. If you want you can supply a word list to the search index with words tha…