浏览代码

Improve ranking of search results when multiple exact matches are found

Thibaut 11 年之前
父节点
当前提交
115ffa8fc8
共有 1 个文件被更改,包括 9 次插入0 次删除
  1. 9 0
      assets/javascripts/app/searcher.coffee

+ 9 - 0
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)