Brain Phrye

code cooking diy fiction personal photos politics reviews tools 


Search

I’ve mentioned before this is a static site so how is it there’s now a search function. The magic of javascript!

I’d seen a search box on hugo sites so I looked around and found these two articles 1 2 on adding search and they each built on each other using lunr.

I tweaked the index building to try and squash it down a bit. The javascript I use to build the lunr prebuilt indexes (search/index.json and search/results.json) is currently as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function uniq(text) {
  return text.split(/\W+/).sort()
           .filter( function(v,i,o){return v!==o[i-1];})
           .filter(word => word.length > 0).join(' ');
}

var lunr = require('../static/lunr.js'),
    fs = require('fs'),
    results = []

fs.readFile('public/full-index.json', (err, data) => {
  if (err) throw err;

  var idx = lunr(function () {
    this.ref('ref');
    this.field('t');

    JSON.parse(data).forEach(function (doc) {
      this.add({
        'ref': doc.ref,
        't': uniq(doc.title + ' ' + doc.body),
      });
      results.push({
        'ref': doc.ref,
        'title': doc.title,
        'excerpt': doc.excerpt,
      });

    }, this)
  })

  fs.writeFile('public/search/index.json', JSON.stringify(idx));
  fs.writeFile('public/search/results.json', JSON.stringify(results));
});

The uniq bit, which came up yesterday in shell, did shrink down the index files, but they’re still pretty large. However in reading the lunr docs the win in pre-built indexes is that it reduces computation. In addition lunr has a pipeline system where work should be done to index the files.

So I suspect in future iterations I’ll be removing the uniq function and instead using lunr’s pipeline system to filter and fine tune the words being indexed.