From 7eab4a23f044dd5b25ea66ecc4ca144a0b53b87d Mon Sep 17 00:00:00 2001 From: Jonathan Bouzekri Date: Sat, 3 Sep 2016 13:26:53 +0200 Subject: [PATCH] separate search and score sorting operation --- lib/index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 8e574ef1..4cad8697 100644 --- a/lib/index.js +++ b/lib/index.js @@ -322,7 +322,7 @@ lunr.Index.prototype.idf = function (term) { * @see Index.prototype.documentVector * @memberOf Index */ -lunr.Index.prototype.search = function (query) { +lunr.Index.prototype.baseSearch = function (query) { var queryTokens = this.pipeline.run(this.tokenizerFn(query)), queryVector = new lunr.Vector, documentSets = [], @@ -382,6 +382,19 @@ lunr.Index.prototype.search = function (query) { .map(function (ref) { return { ref: ref, score: queryVector.similarity(this.documentVector(ref)) } }, this) +} + +/** + * Searches the index using the passed query and returns result sorted by score. + * + * @param {String} query The query to search the index with. + * @returns {Object} + * @see Index.prototype.baseSearch + * @memberOf Index + */ +lunr.Index.prototype.search = function (query) { + return this + .search(query) .sort(function (a, b) { return b.score - a.score })