Searches the index using the passed query.
Queries should be a string, multiple words are allowed and will lead to an AND based query, e.g. idx.search('foo bar')
will run a search for documents containing both 'foo' and 'bar'.
All query tokens are passed through the same pipeline that document tokens are passed through, so any lanugage processing involved will be run on every query term.
Each query term is expanded, so that the term 'he' might be expanded to 'hello' and 'help' if those terms were already included in the index.
Matching documents are returned as an array of objects, each object contains the matching document ref, as set for this index, and the similarity score for this document against the query.
Source
lunr.Index.prototype.search = function (query) {
var queryTokens = this.pipeline.run(lunr.tokenizer(query)),
queryArr = new Array (this.corpusTokens.length),
documentSets = [],
fieldBoosts = this._fields.reduce(function (memo, f) { return memo + f.boost }, 0)
if (!queryTokens.some(lunr.TokenStore.prototype.has, this.tokenStore)) return []
queryTokens
.forEach(function (token, i, tokens) {
var tf = 1 / tokens.length * this._fields.length * fieldBoosts,
self = this
var set = this.tokenStore.expand(token).reduce(function (memo, key) {
var pos = self.corpusTokens.indexOf(key),
idf = self.idf(key),
set = new lunr.SortedSet
if (pos > -1) queryArr[pos] = tf * idf
Object.keys(self.tokenStore.get(key)).forEach(function (ref) { set.add(ref) })
return memo.union(set)
}, new lunr.SortedSet)
documentSets.push(set)
}, this)
var documentSet = documentSets.reduce(function (memo, set) {
return memo.intersect(set)
})
var queryVector = new lunr.Vector (queryArr)
return documentSet
.map(function (ref) {
return { ref: ref, score: queryVector.similarity(this.documentVector(ref)) }
}, this)
.sort(function (a, b) {
return b.score - a.score
})
}