Sam Dutton
Sam Dutton

Reputation: 15269

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?

Upvotes: 0

Views: 834

Answers (1)

bvaughn
bvaughn

Reputation: 13487

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:

  • Build the index on the client-side in the first place. It's pretty fast to do. (If you can't do this b'c the data you're indexing is too large, then consider exposing it via an API as previously mentioned.) Also, if you don't need all of the configurability of js-search (which it seems like you don't, based on your example) then consider using its faster sibling, js-worker-search.
  • Alternately you can look into lunr.js which (I believe) does support serializing and restoring an index.

Upvotes: 1

Related Questions