Class: Mongoid::FTS::Results

Inherits:
Array
  • Object
show all
Defined in:
lib/mongoid-fts.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.



132
133
134
135
136
137
138
139
# File 'lib/mongoid-fts.rb', line 132

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.



130
131
132
# File 'lib/mongoid-fts.rb', line 130

def _models
  @_models
end

#_searchesObject

Returns the value of attribute _searches.



129
130
131
# File 'lib/mongoid-fts.rb', line 129

def _searches
  @_searches
end

Instance Method Details

#_denormalize!Object

TODO - text sorting more…



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/mongoid-fts.rb', line 216

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!

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

    case score
      when 0
        a['_position'] <=> b['_position']
      else
        score
    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)

#
  limit = @_searches._limit

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

  self
end

#num_pagesObject



206
207
208
# File 'lib/mongoid-fts.rb', line 206

def num_pages
  @num_pages
end

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



182
183
184
185
186
187
188
189
190
191
# File 'lib/mongoid-fts.rb', line 182

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



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
# File 'lib/mongoid-fts.rb', line 141

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



195
196
197
198
199
200
201
202
203
204
# File 'lib/mongoid-fts.rb', line 195

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



210
211
212
# File 'lib/mongoid-fts.rb', line 210

def total_pages
  num_pages
end