olivernn / lunr.js

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

No results in metaData #257

Closed JinxMan25 closed 7 years ago

JinxMan25 commented 7 years ago

I'm using ver. 2.0.1 and I'm having some trouble retrieving results from a search.

var idx = Lunar(function () { this.ref('id'); this.field('text');

    docs.forEach(function (doc) {
      this.add(doc);

});

and docs is just an array of object with key id and text being some gibberish

When run the search, it returns the proper documents but when I look into the metaData and the matches, it has no properties. So I'm not sure if I'm doing wrong or this has been fixed in a more recent update. Thanks

olivernn commented 7 years ago

What metadata are you expecting to be returned with the search results?

Metadata must be whitelisted to be part of the results, this is to reduce the size of an index unless you need the metadata.

Without any plugins, the only available metadata would be term positions, to include the position data in the index you must create the index like this:

var idx = lunr(function () {
  this.ref('id')
  this.field('text')
  this.metadataWhitelist = ['position']

  documents.forEach(function (doc) { this.add(doc) }, this)
})
mayanklahiri commented 7 years ago

I'd be on the side of either making this.metadataWhitelist = ['position'] the default, or having this option added to the example at https://lunrjs.com/docs/index.html.

olivernn commented 7 years ago

@mayanklahiri the reason you have to opt-in to getting the position data is that it inflates the size of the in-memory and serialised index. Lunr doesn't do anything with this data, and so if a user needs it they are likely already having to do something extra anyway, e.g. highlighting matches, that it makes sense for them to opt-in. Otherwise everyone would take the penalty of the larger indexes by default, without getting any benefit from it.

@clanofnoobs I'm closing this issue now, feel free to re-open if my response didn't answer your question.

machow commented 7 years ago

just a note, I spent half an hour trying to figure out why the metadata wasn't appearing. The section on search results don't make clear that you shouldn't expect to see the behavior it describes (unless you ask for it).

It might help to put a small note to the effect of "none of this section applies unless you whitelist it". The behavior makes sense, and putting a note there will save newcomers like me some heavy googling / source code digging.

olivernn commented 7 years ago

@machow good point, the documentation could be improved here. Going to re-open and treat it as an issue with the documentation.

I'm also hoping to put together a guide to highlighting search results swell, I'm waiting for https://github.com/julmot/mark.js/pull/130 to land so that I don't have to re-implement the UI part of the search highlighting.

mike1808 commented 7 years ago

@olivernn it's already merged.

benpolinsky commented 7 years ago

Thanks for the library, very much :)

I'd just like to echo the thoughts in this thread that it would be nice to have the documentation mention whitelisting, especially for the position.

olivernn commented 7 years ago

@benpolinsky good point, I've pushed an edit to the guides that mentions this