olivernn / lunr.js

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

Search (or add) not working. #298

Closed mnemanja closed 6 years ago

mnemanja commented 6 years ago

Hello, I'm following the tutorial from this book https://www.manning.com/books/cross-platform-desktop-applications.

They are using the old API, so I've tried using the new one but it does not work for me.

I cannot confirm if search and add exist from the perspective of API. image I'm not sure how to debug it. Any thoughts?

const lunr = require('lunr');
let index;

function resetIndex() {
  index = lunr(function () {
    this.field('file');
    this.field('type');
    this.ref('path');
  });
}

function addToIndex(file) {
  // The file is an object
  index = lunr(function () {
    this.field('file');
    this.field('type');
    this.ref('path');

    this.add(file);
  });
}

function find(query, cb) {
  if (!index) {
      resetIndex();
  }

  const results = index.search(query);
  // The results returns an empty array [] at this point
  cb(results);
}
olivernn commented 6 years ago

It looks like you are using Lunr 2.x which no longer supports adding documents to an index after it has been built (more info in the upgrading guide).

That said it looks like you are working around this by rebuilding the index every time you want to add a file to it. At a glance it looks like you call resetIndex before doing the search, which doesn't actually add any files to the index. I think the problem then is in how you are creating the index before searching, you would need to ensure that all the files you want to search are being added to the index.

You can help debug the issue by checking if the index is empty in you find function:

index.tokenSet.toArray()

The above will return all the words that are in the index, if this is empty the either you didn't add anything to the index when building it, or the field object you did add to the index had no words.

Hopefully this helps you get to the bottom of the problem, let me know if you manage to come good on this.

P.S. Paul was a colleague of mine when I worked in London, great guy. Hope you are enjoying his book!

mnemanja commented 6 years ago

Thanks for your response. I'll close this issue since I'm no longer working on this.