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.



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

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.



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

def tokens
  @tokens
end

Instance Method Details

#result_countObject

Returns the total result count



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

def result_count
  result_ids.size
end

#result_count_by_typeObject

Returns the result count by type



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

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



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

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



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

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.filter_map do |scope, id|
    results_by_scope[scope].detect {|record| record.id == id}
  end
end