0

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?

Sam Dutton
  • 14,775
  • 6
  • 54
  • 64
  • Are you running this code on the server side or the client side? Do you have any sort of REST API, WebSockets or other communication medium setup so you can send messages between the client and server? – Mike Cluck Sep 27 '17 at 18:05
  • Hi Mike — I'm running this on the server side. I want to be able to create an index on the server then cache and use that on the client. (See my comment on Brian's answer below.) – Sam Dutton Sep 29 '17 at 09:48

1 Answers1

1

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.
bvaughn
  • 13,300
  • 45
  • 46
  • Thanks Brian! My aim is to try out offline-enabled search like I've done here: https://simpl.info/search/lunr, https://simpl.info/search/pouchdb. I'll try out creating the index in memory on the client as you suggest (while caching the data). – Sam Dutton Sep 29 '17 at 09:46