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
62
63
64
65
66
67
68
69
# 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

  # 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
  # but for Chinese characters (Chinese HANZI/Japanese KANJI), tokens can be one character
  @tokens = @tokens.uniq.select {|w| w.length > 1 || w =~ /\p{Han}/ }
  # no more than 5 tokens to search for
  @tokens.slice! 5..-1
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


72
73
74
# File 'lib/redmine/search.rb', line 72

def result_count
  result_ids.size
end

#result_count_by_typeObject

Returns the result count by type


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

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


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

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


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

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