Class: Redmine::Search::Fetcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Fetcher.



53
54
55
56
57
58
59
60
61
# File 'lib/redmine/search.rb', line 53

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

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



51
52
53
# File 'lib/redmine/search.rb', line 51

def tokens
  @tokens
end

Instance Method Details

#result_countObject

Returns the total result count



64
65
66
# File 'lib/redmine/search.rb', line 64

def result_count
  result_ids.size
end

#result_count_by_typeObject

Returns the result count by type



69
70
71
72
73
74
75
# File 'lib/redmine/search.rb', line 69

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



91
92
93
# File 'lib/redmine/search.rb', line 91

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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/redmine/search.rb', line 78

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