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

ref : "undefined" in result for search function #345

Closed ganeshkbhat closed 6 years ago

ganeshkbhat commented 6 years ago

ref : "undefined" in search result for search. Here is my code and example array:

mdArr = [{body:"# Getting started↵↵↵ Texting xxxx text", url:"/assets/mddocs/intro.md"}, {body:"Another body", url:"/assets/mddocs/intro.md"}]

searchString = 'text'

function searchDocs(mdArr, searchString) {
  let result, docIndex = lunr(function () {
    this.field('url');
    this.field('body');
    for (var i = 0; i < mdArr.length; i++) {
      console.log('lunr', mdArr[i]);
      if (!!mdArr[i].body && (mdArr[i].body.indexOf('Error') !== 0 && !mdArr.err)) {
        let a = mdArr[i];
        this.add(a);
      }
    }
  });

  result = docIndex.search(searchString);
  console.log(result);
  return result;
}

ref-undefined

Any help is welcome? Missing something here?

yeraydiazdiaz commented 6 years ago

I believe the problem is that you have not specified a ref field, in that case Lunr attempts to retrieve an id field from your docs, but in your case there is none so ref is always undefined.

Seems you want to use the url field as a reference for the documents, I suggest simply changing this.field('url') for this.ref('url'), as described in the "Getting started" guide.

ganeshkbhat commented 6 years ago

Thank you for your time. this.ref('url') worked and I was able to get the positions as well using this.metadataWhitelist = ['position']; . Kudos for the nice simple and flexible library. ;-)