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

Results aren't accurate for search #364

Closed sosharma1403 closed 6 years ago

sosharma1403 commented 6 years ago
var lunr = require('lunr');
var documents = [{
  "name": "Lunr",
  "text": "this Like Solr, but much smaller, and not as bright."
}, {
  "name": "React",
  "text": "A JavaScript library for building user interfaces."
}, {
  "name": "Lodash",
  "text": "A modern JavaScript utility library delivering modularity, performance & extras."
}]

var idx = lunr(function () {
  this.ref('name')
  this.field('text')

  documents.forEach(function (doc) {
    this.add(doc)
  }, this)
})

console.log(idx.search("this"));

Should return match for this

yeraydiazdiaz commented 6 years ago

The reason Lunr returns no results for your query is because this is one of the words filtered by the stopWordFilter, which skips it during the indexing of the document as it's considered an non-content word in english.

Searching for other more meaningful words like user or modular does return results.

If you want full control over the index building process you skip using the lunr function and create a lunr.Builder manually removing the stopWordsFilter from the pipeline similar to what the lunr function does.

olivernn commented 6 years ago

Just to add one thing, if you need a different set of words to be considered stop words you can use lunr.generateStopWordFilter to create a custom stop word filter. This is exactly how the default stop word filter is created.