Class: Mongoid::FTS::Results

Inherits:
Array
  • Object
show all
Defined in:
lib/mongoid-fts/results.rb

Defined Under Namespace

Classes: Promise

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_searches) ⇒ Results

Returns a new instance of Results.



7
8
9
10
11
12
13
14
# File 'lib/mongoid-fts/results.rb', line 7

def initialize(_searches)
  @_searches = _searches
  @_models = []
  _denormalize!
  @page = 1
  @per = size
  @num_pages = 1
end

Instance Attribute Details

#_modelsObject

Returns the value of attribute _models.



5
6
7
# File 'lib/mongoid-fts/results.rb', line 5

def _models
  @_models
end

#_searchesObject

Returns the value of attribute _searches.



4
5
6
# File 'lib/mongoid-fts/results.rb', line 4

def _searches
  @_searches
end

Instance Method Details

#_denormalize!Object

TODO - text sorting more…



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mongoid-fts/results.rb', line 91

def _denormalize!
#
  collection = self

  collection.clear
  @_models = []

  return self if @_searches.empty?

#
  _models = @_searches._models

  _position = proc do |model|
    _models.index(model) or raise("no position for #{ model.inspect }!?")
  end

  results =
    @_searches.map do |_search|
      _search['results'] ||= []

      _search['results'].each do |result|
        result['_model'] = _search._model
        result['_position'] = _position[_search._model]
      end

      _search['results']
    end

  results.flatten!
  results.compact!

=begin
  results.sort! do |a, b|
    score = Float(b['score']) <=> Float(a['score'])

    case score
      when 0
        a['_position'] <=> b['_position']
      else
        score
    end
  end
=end

#
  batches = Hash.new{|h,k| h[k] = []}

  results.each do |entry|
    obj = entry['obj']

    context_type, context_id = obj['context_type'], obj['context_id']

    batches[context_type].push(context_id)
  end

#
  models = FTS.find_in_batches(batches)

#
  result_index = {}

  results.each do |result|
    context_type = result['obj']['context_type'].to_s
    context_id = result['obj']['context_id'].to_s
    key = [context_type, context_id]

    result_index[key] = result
  end

#
  models.each do |model|
    context_type = model.class.name.to_s
    context_id = model.id.to_s
    key = [context_type, context_id]

    result = result_index[key]
    model['_fts_index'] = result
  end

#
  models.sort! do |model_a, model_b|
    a = model_a['_fts_index']
    b = model_b['_fts_index']

    score = Float(b['score']) <=> Float(a['score'])

    case score
      when 0
        a['_position'] <=> b['_position']
      else
        score
    end
  end

#
  limit = @_searches._limit

#
  replace(@_models = models[0 ... limit])

  self
end

#num_pagesObject



81
82
83
# File 'lib/mongoid-fts/results.rb', line 81

def num_pages
  @num_pages
end

#page(*args) ⇒ Object Also known as: current_page



57
58
59
60
61
62
63
64
65
66
# File 'lib/mongoid-fts/results.rb', line 57

def page(*args)
  if args.empty?
    return @page
  else
    options = Map.options_for!(args)
    page = args.shift || options[:page]
    options[:page] = page
    paginate(options)
  end
end

#paginate(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongoid-fts/results.rb', line 16

def paginate(*args)
  results = self
  options = Map.options_for!(args)

  page = Integer(args.shift || options[:page] || @page)
  per = args.shift || options[:per] || options[:size]

  if per.nil?
    return Promise.new(results, page)
  else
    per = Integer(per)
  end

  @page = [page.abs, 1].max
  @per = [per.abs, 1].max
  @num_pages = (size.to_f / @per).ceil

  offset = (@page - 1) * @per
  length = @per 

  slice = Array(@_models[offset, length])

  replace(slice)

  self
end

#per(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/mongoid-fts/results.rb', line 70

def per(*args)
  if args.empty?
    return @per
  else
    options = Map.options_for!(args)
    per = args.shift || options[:per]
    options[:per] = per
    paginate(options)
  end
end

#total_pagesObject



85
86
87
# File 'lib/mongoid-fts/results.rb', line 85

def total_pages
  num_pages
end