From 115ffa8fc832169009895a227e90f8403aa59b5e Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sun, 15 Jun 2014 11:15:44 -0400 Subject: [PATCH] Improve ranking of search results when multiple exact matches are found --- assets/javascripts/app/searcher.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/assets/javascripts/app/searcher.coffee b/assets/javascripts/app/searcher.coffee index 3b97fae9..bf5f8a5b 100644 --- a/assets/javascripts/app/searcher.coffee +++ b/assets/javascripts/app/searcher.coffee @@ -135,6 +135,7 @@ class app.Searcher # index = # position of the query in the string being matched + lastIndex = # last position of the query in the string being matched match = # regexp match data score = # score for the current match separators = # counter @@ -144,6 +145,14 @@ class app.Searcher index = value.indexOf @query return unless index >= 0 + lastIndex = value.lastIndexOf @query + + if index isnt lastIndex + Math.max(@scoreExactMatch(value, index), @scoreExactMatch(value, lastIndex)) + else + @scoreExactMatch(value, index) + + scoreExactMatch: (value, index) -> # Remove one point for each unmatched character. score = 100 - (value.length - @queryLength)