olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.89k stars 545 forks source link

unable to load searilized index #380

Closed KunalSaini closed 5 years ago

KunalSaini commented 5 years ago

Hi I am getting following error when trying to load back a serialized index.

/mnt/c/proj/node_modules/lunr/lunr.js:2291
      serializedVectors = serializedIndex.fieldVectors,
                                          ^

TypeError: Cannot read property 'fieldVectors' of null
    at Function.lunr.Index.load (/mnt/c/proj/node_modules/lunr/lunr.js:2291:43)
    at fs.readFile (/mnt/c/proj/scripts/search.js:12:28)
    at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)

here is the code for reading the index

const lunr = require('lunr');
const fs = require('fs');
const path = require('path');

fs.readFile(path.join(__dirname, 'index.json'),
  {
    encoding: 'utf-8',
  },
  (data) => {
    const idx = lunr.Index.load(JSON.parse(data));
    const searchTerm = ('test');
    console.log(idx.search(searchTerm));
  });

hear is how i am serializing the index in first place.

const normalizeUrl = (builder) => {
  const urlFunction = (token) => {
    const turl = url.parse(token.str);
    if (turl.hash) {
      const value = turl.hash.slice(1);
      token.update(() => value);
    }
    return token;
  };

  // Register the pipeline function so the index can be serialised
  lunr.Pipeline.registerFunction(urlFunction, 'normalizeUrl');
  // Add the pipeline function to both the indexing pipeline and the
  // searching pipeline
  builder.pipeline.before(lunr.stemmer, urlFunction);
  builder.searchPipeline.before(lunr.stemmer, urlFunction);
};

function config(builder) {
  builder.use(normalizeUrl);
  builder.ref('newId');
  builder.field('newId');
  builder.field('@type');
  statents.forEach((doc) => {
    builder.add(doc);
  });
}

const idx = lunr(config);
fs.writeFileSync(path.join(__dirname, 'index.json'), JSON.stringify(idx));
hoelzro commented 5 years ago

@KunalSaini Thanks for the report! Looking at your example, I see that you're calling the first parameter to readFile's callback function data - I believe Node passes in any error object as the first parameter to this callback, and the data loaded from the filesystem as the second parameter. This is consistent with your error message, which seems to indicate null is getting passed to lunr.Index.load. Does changing the function signature from (data) to (err, data) make your code work?

KunalSaini commented 5 years ago

thanks @hoelzro, you are right, silly mistake on my part.