class Mongoid::FTS::Index

Public Class Methods

add(*args, &block) click to toggle source
# File lib/mongoid-fts/index.rb, line 195
def Index.add(*args, &block)
  add!(*args, &block)
end
add!(model) click to toggle source
# File lib/mongoid-fts/index.rb, line 136
def Index.add!(model)
  to_search = Index.to_search(model)

  literals         = to_search.has_key?(:literals) ?  Coerce.list_of_strings(to_search[:literals]) : nil

  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

  fuzzy            = to_search.has_key?(:fuzzy) ?  Coerce.list_of_strings(to_search[:fuzzy]) : 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 = {
    :literals         => literals,
    :title            => title,
    :keywords         => keywords,
    :fuzzy            => fuzzy,
    :fulltext         => fulltext
  }

  index = nil
  n = 42

  n.times do |i|
    index = where(conditions).first
    break if index

    begin
      index = create!(conditions)
      break if index
    rescue Object
      nil
    end

    sleep(rand) if i < (n - 1)
  end

  if index
    begin
      index.update_attributes!(attributes)
    rescue Object
      raise Error.new("failed to update index for #{ conditions.inspect }")
    end
  else
    raise Error.new("failed to create index for #{ conditions.inspect }")
  end

  index
end
rebuild!() click to toggle source
# File lib/mongoid-fts/index.rb, line 120
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(*args, &block) click to toggle source
# File lib/mongoid-fts/index.rb, line 218
def Index.remove(*args, &block)
  remove!(*args, &block)
end
remove!(*args, &block) click to toggle source
# File lib/mongoid-fts/index.rb, line 199
def Index.remove!(*args, &block)
  options = args.extract_options!.to_options!
  models = args.flatten.compact

  model_ids = {}

  models.each do |model|
    model_name = model.class.name.to_s
    model_ids[model_name] ||= []
    model_ids[model_name].push(model.id)
  end

  conditions = model_ids.map do |model_name, model_ids|
    {:context_type => model_name, :context_id.in => model_ids}
  end

  any_of(conditions).destroy_all
end
reset!() click to toggle source
# File lib/mongoid-fts/index.rb, line 115
def Index.reset!
  teardown!
  setup!
end
setup!() click to toggle source
# File lib/mongoid-fts/index.rb, line 111
def Index.setup!
  Index.create_indexes
end
teardown!() click to toggle source
# File lib/mongoid-fts/index.rb, line 106
def Index.teardown!
  Index.remove_indexes
  Index.destroy_all
end

Public Instance Methods

inspect(*args, &block) click to toggle source
# File lib/mongoid-fts/index.rb, line 102
def inspect(*args, &block)
  Map.for(as_document).inspect(*args, &block)
end
normalize() click to toggle source
# File lib/mongoid-fts/index.rb, line 86
def normalize
  if !defined?(@normalized) or !@normalized
    normalize!
  end
end
normalize!() click to toggle source
# File lib/mongoid-fts/index.rb, line 92
def normalize!
  index = self

  %w( literals title keywords fulltext ).each do |attr|
    index[attr] = FTS.list_of_strings(index[attr])
  end
ensure
  @normalized = true
end