diff --git a/app/views/search/index.api.rsb b/app/views/search/index.api.rsb
index 6619c00e3..b226f970f 100644
--- a/app/views/search/index.api.rsb
+++ b/app/views/search/index.api.rsb
@@ -1,5 +1,5 @@
api.array :results, api_meta(:total_count => @result_count, :offset => @offset, :limit => @limit) do
- @results.each do |result|
+ @results.each do |result, rank|
api.result do
api.id result.id
api.title result.event_title
@@ -7,6 +7,7 @@ api.array :results, api_meta(:total_count => @result_count, :offset => @offset,
api.url url_for(result.event_url(:only_path => false))
api.description result.event_description
api.datetime result.event_datetime
+ api.rank rank
end
end
end
diff --git a/app/views/search/index.html.erb b/app/views/search/index.html.erb
index 53ec17a63..bcebe45c0 100644
--- a/app/views/search/index.html.erb
+++ b/app/views/search/index.html.erb
@@ -41,10 +41,10 @@
<%= l(:label_result_plural) %> (<%= @result_count %>)
- <% @results.each do |e| %>
+ <% @results.each do |e, rank| %>
-
<%= content_tag('span', e.project, :class => 'project') unless @project == e.project %>
- <%= link_to(highlight_tokens(e.event_title.truncate(255), @tokens), e.event_url) %>
+ <%= link_to(highlight_tokens(e.event_title.truncate(255), @tokens), e.event_url, :data => { :rank => rank }) %>
- <%= highlight_tokens(e.event_description, @tokens) %>
<%= format_time(e.event_datetime) %>
diff --git a/lib/redmine/search.rb b/lib/redmine/search.rb
index ecc6bb652..e359cf405 100644
--- a/lib/redmine/search.rb
+++ b/lib/redmine/search.rb
@@ -82,15 +82,15 @@ module Redmine
# Returns the results for the given offset and limit
def results(offset, limit)
result_ids_to_load = result_ids[offset, limit] || []
-
+
results_by_scope = Hash.new {|h,k| h[k] = []}
result_ids_to_load.group_by(&:first).each do |scope, scope_and_ids|
klass = scope.singularize.camelcase.constantize
results_by_scope[scope] += klass.search_results_from_ids(scope_and_ids.map(&:last))
end
-
- result_ids_to_load.map do |scope, id|
- results_by_scope[scope].detect {|record| record.id == id}
+
+ result_ids_to_load.map do |scope, rank, id|
+ [results_by_scope[scope].detect {|record| record.id == id}, rank]
end.compact
end
@@ -129,8 +129,8 @@ module Redmine
end
# sort results, higher rank and id first
ret.sort! {|a,b| b.last <=> a.last}
- # only keep ids now that results are sorted
- ret.map! {|scope, r| [scope, r.last]}
+ # [[scope, rank, id], ...]
+ ret.map! {|scope, r| [scope, r].flatten }
ret
end
end