You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. module Redmine
  18. module Search
  19. mattr_accessor :available_search_types
  20. @@available_search_types = []
  21. class << self
  22. def map(&block)
  23. yield self
  24. end
  25. # Registers a search provider
  26. def register(search_type, options={})
  27. search_type = search_type.to_s
  28. @@available_search_types << search_type unless @@available_search_types.include?(search_type)
  29. end
  30. # Returns the cache store for search results
  31. # Can be configured with config.redmine_search_cache_store= in config/application.rb
  32. def cache_store
  33. @@cache_store ||= begin
  34. # if config.search_cache_store was not previously set, a no method error would be raised
  35. config = Rails.application.config.redmine_search_cache_store rescue :memory_store
  36. if config
  37. ActiveSupport::Cache.lookup_store config
  38. end
  39. end
  40. end
  41. end
  42. class Fetcher
  43. attr_reader :tokens
  44. def initialize(question, user, scope, projects, options={})
  45. @user = user
  46. @question = question.strip
  47. @scope = scope
  48. @projects = projects
  49. @cache = options.delete(:cache)
  50. @options = options
  51. # extract tokens from the question
  52. # eg. hello "bye bye" => ["hello", "bye bye"]
  53. @tokens = @question.scan(%r{((\s|^)"[^"]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
  54. # tokens must be at least 2 characters long
  55. # but for Chinese characters (汉字/漢字), tokens can be one character
  56. @tokens = @tokens.uniq.select {|w| w.length > 1 || w =~ /\p{Han}/ }
  57. # no more than 5 tokens to search for
  58. @tokens.slice! 5..-1
  59. end
  60. # Returns the total result count
  61. def result_count
  62. result_ids.size
  63. end
  64. # Returns the result count by type
  65. def result_count_by_type
  66. ret = Hash.new {|h,k| h[k] = 0}
  67. result_ids.group_by(&:first).each do |scope, ids|
  68. ret[scope] += ids.size
  69. end
  70. ret
  71. end
  72. # Returns the results for the given offset and limit
  73. def results(offset, limit)
  74. result_ids_to_load = result_ids[offset, limit] || []
  75. results_by_scope = Hash.new {|h,k| h[k] = []}
  76. result_ids_to_load.group_by(&:first).each do |scope, scope_and_ids|
  77. klass = scope.singularize.camelcase.constantize
  78. results_by_scope[scope] += klass.search_results_from_ids(scope_and_ids.map(&:last))
  79. end
  80. result_ids_to_load.map do |scope, id|
  81. results_by_scope[scope].detect {|record| record.id == id}
  82. end.compact
  83. end
  84. # Returns the results ids, sorted by rank
  85. def result_ids
  86. @ranks_and_ids ||= load_result_ids_from_cache
  87. end
  88. private
  89. def project_ids
  90. Array.wrap(@projects).map(&:id)
  91. end
  92. def load_result_ids_from_cache
  93. if Redmine::Search.cache_store
  94. cache_key = ActiveSupport::Cache.expand_cache_key(
  95. [@question, @user.id, @scope.sort, @options, project_ids.sort]
  96. )
  97. Redmine::Search.cache_store.fetch(cache_key, :force => !@cache) do
  98. load_result_ids
  99. end
  100. else
  101. load_result_ids
  102. end
  103. end
  104. def load_result_ids
  105. ret = []
  106. # get all the results ranks and ids
  107. @scope.each do |scope|
  108. klass = scope.singularize.camelcase.constantize
  109. ranks_and_ids_in_scope = klass.search_result_ranks_and_ids(@tokens, User.current, @projects, @options)
  110. ret += ranks_and_ids_in_scope.map {|rs| [scope, rs]}
  111. end
  112. # sort results, higher rank and id first
  113. ret.sort! {|a,b| b.last <=> a.last}
  114. # only keep ids now that results are sorted
  115. ret.map! {|scope, r| [scope, r.last]}
  116. ret
  117. end
  118. end
  119. module Controller
  120. def self.included(base)
  121. base.extend(ClassMethods)
  122. end
  123. module ClassMethods
  124. @@default_search_scopes = Hash.new {|hash, key| hash[key] = {:default => nil, :actions => {}}}
  125. mattr_accessor :default_search_scopes
  126. # Set the default search scope for a controller or specific actions
  127. # Examples:
  128. # * search_scope :issues # => sets the search scope to :issues for the whole controller
  129. # * search_scope :issues, :only => :index
  130. # * search_scope :issues, :only => [:index, :show]
  131. def default_search_scope(id, options = {})
  132. if actions = options[:only]
  133. actions = [] << actions unless actions.is_a?(Array)
  134. actions.each {|a| default_search_scopes[controller_name.to_sym][:actions][a.to_sym] = id.to_s}
  135. else
  136. default_search_scopes[controller_name.to_sym][:default] = id.to_s
  137. end
  138. end
  139. end
  140. def default_search_scopes
  141. self.class.default_search_scopes
  142. end
  143. # Returns the default search scope according to the current action
  144. def default_search_scope
  145. @default_search_scope ||= default_search_scopes[controller_name.to_sym][:actions][action_name.to_sym] ||
  146. default_search_scopes[controller_name.to_sym][:default]
  147. end
  148. end
  149. end
  150. end