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

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