javascriptlunrjsjs-search

Build an index in a Node app using Js Search


I want to create a Js Search index in a Node app, then use this index in a client-side JavaScript app.

It's not clear to me from the README or the benchmark code how to do this.

I've tried this:

  // docs is an array of objects, each with a name, title and description
  var search = new jsSearch.Search('name');
  search.addIndex('title');
  search.addIndex('description');
  search.addDocuments(docs);

...and this:

  var search = new jsSearch.Search('name');
  search.searchIndex = new jsSearch.TfIdfSearchIndex('name');
  search.addIndex('title');
  search.addIndex('description');
  search.addDocuments(docs);

...but how can I access the index at that point?


Solution

  • use this index in a client-side JavaScript app

    There's no way to pass the in-memory index from Node to a browser to access in the way you normally would (if you just built the index in the browser). js-search doesn't [currently] support serialization. (Early testing suggested that it's not significantly faster to restore from a serialized format vs just recreating from scratch.)

    You could expose the search from Node via an API but I don't think that's what you want or what you're looking for.

    So I'd suggest a couple of possibilities: