Class: Redmine::Search::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/redmine/search.rb

Overview

Since:

  • 3.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question, user, scope, projects, options = {}) ⇒ Fetcher

Returns a new instance of Fetcher



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redmine/search.rb', line 51

def initialize(question, user, scope, projects, options={})
  @user = user
  @question = question.strip
  @scope = scope
  @projects = projects
  @cache = options.delete(:cache)
  @options = options

  # extract tokens from the question
  # eg. hello "bye bye" => ["hello", "bye bye"]
  @tokens = @question.scan(%r{((\s|^)"[^"]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
  # tokens must be at least 2 characters long
  @tokens = @tokens.uniq.select {|w| w.length > 1 }
  # no more than 5 tokens to search for
  @tokens.slice! 5..-1
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens



49
50
51
# File 'lib/redmine/search.rb', line 49

def tokens
  @tokens
end

Instance Method Details

#result_countObject

Returns the total result count



69
70
71
# File 'lib/redmine/search.rb', line 69

def result_count
  result_ids.size
end

#result_count_by_typeObject

Returns the result count by type



74
75
76
77
78
79
80
# File 'lib/redmine/search.rb', line 74

def result_count_by_type
  ret = Hash.new {|h,k| h[k] = 0}
  result_ids.group_by(&:first).each do |scope, ids|
    ret[scope] += ids.size
  end
  ret
end

#result_idsObject

Returns the results ids, sorted by rank



98
99
100
# File 'lib/redmine/search.rb', line 98

def result_ids
  @ranks_and_ids ||= load_result_ids_from_cache
end

#results(offset, limit) ⇒ Object

Returns the results for the given offset and limit



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/redmine/search.rb', line 83

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}
  end.compact
end