Class: Mongoid::FTS::Index

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/mongoid-fts.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(model) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/mongoid-fts.rb', line 341

def Index.add(model)
  to_search = Index.to_search(model)

  title    = to_search.has_key?(:title) ?  Coerce.string(to_search[:title]) : nil
  keywords = to_search.has_key?(:keywords) ?  Coerce.list_of_strings(to_search[:keywords]) : nil
  fulltext = to_search.has_key?(:fulltext) ?  Coerce.string(to_search[:fulltext]) : nil

  context_type = model.class.name.to_s
  context_id   = model.id

  conditions = {
    :context_type => context_type,
    :context_id   => context_id
  }

  attributes = {
    :title        => title,
    :keywords     => keywords,
    :fulltext     => fulltext
  }

  new(conditions).upsert

  where(conditions).first.tap do |index|
    if index
      index.update_attributes(attributes)
    end
  end
end

.rebuild!Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/mongoid-fts.rb', line 325

def Index.rebuild!
  batches = Hash.new{|h,k| h[k] = []}

  each do |index|
    context_type, context_id = index.context_type, index.context_id
    next unless context_type && context_id
    (batches[context_type] ||= []).push(context_id)
  end

  models = FTS.find_in_batches(batches)

  reset!

  models.each{|model| add(model)}
end

.remove(model) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/mongoid-fts.rb', line 371

def Index.remove(model)
  context_type = model.class.name.to_s
  context_id = model.id

  conditions = {
    :context_type => context_type,
    :context_id   => context_id
  }

  where(conditions).first.tap do |index|
    if index
      index.destroy rescue nil
    end
  end
end

.reset!Object



320
321
322
323
# File 'lib/mongoid-fts.rb', line 320

def Index.reset!
  teardown!
  setup!
end

.setup!Object



316
317
318
# File 'lib/mongoid-fts.rb', line 316

def Index.setup!
  Index.create_indexes
end

.teardown!Object



311
312
313
314
# File 'lib/mongoid-fts.rb', line 311

def Index.teardown!
  Index.remove_indexes
  Index.destroy_all
end

.to_search(model) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/mongoid-fts.rb', line 387

def Index.to_search(model)
  to_search = nil

  if model.respond_to?(:to_search)
    to_search = Map.for(model.to_search)
  else
    to_search = Map.new

    to_search[:title] =
      %w( title ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end.compact.join(' ')

    to_search[:keywords] =
      %w( keywords tags ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end.compact

    to_search[:fulltext] =
      %w( fulltext text content body description ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end.compact.join(' ')
  end

  unless %w( title keywords fulltext ).detect{|key| to_search.has_key?(key)}
    raise ArgumentError, "you need to define #{ model }#to_search"
  end

  to_search
end

Instance Method Details

#normalizeObject



282
283
284
285
286
# File 'lib/mongoid-fts.rb', line 282

def normalize
  if !defined?(@normalized) or !@normalized
    normalize!
  end
end

#normalize!Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/mongoid-fts.rb', line 288

def normalize!
  index = self

  unless [index.keywords].join.strip.empty?
    index.keywords = FTS.list_of_strings(index.keywords)
  end

  unless [index.title].join.strip.empty?
    index.title = index.title.to_s.strip
  end

  unless [index.keywords].join.strip.empty?
    index.keywords = index.keywords.map{|keyword| keyword.strip}
  end

  unless [index.fulltext].join.strip.empty?
    index.fulltext = index.fulltext.to_s.strip
  end

ensure
  @normalized = true
end