module ARIndexer::Indexer

Public Class Methods

break_string(value) click to toggle source
# File lib/ar_indexer/indexer.rb, line 3
def self.break_string(value)
  # Remove HTML markup
  value.gsub!(/<[^>]+>/, ' ')
  # Decode HTML entities
  coder = HTMLEntities.new
  value = coder.decode(value)
  # Split words on slashes and dashes
  value.gsub!(/[\/\-]/, ' ')
  # Remove most punctuation
  value.gsub!(/[^a-zA-Z0-9\s]/, '')
  # Move everything to lower case
  value.downcase!
  # Split all words into an array
  forward_index = value.split(' ')
  # Remove stopwords and duplicates
  forward_index = (forward_index - Stopwords::STOPWORDS).uniq
  return forward_index
end
index_string(model_constant, object_id, field_name, value, repair_on_completion) click to toggle source
# File lib/ar_indexer/indexer.rb, line 22
def self.index_string(model_constant, object_id, field_name, value, repair_on_completion)
  forward_index = self.break_string(value)
  forward_index.each do |word|
    if index_record = ReverseIndex.where(:model_constant => model_constant, :field_name => field_name, :word => word).first
      current_id_array = index_record.retrieve_id_array
      unless current_id_array.include? object_id
        new_id_list = (current_id_array << object_id).sort.join(',')
        index_record.update(:id_list => new_id_list)
      end
    else
      ReverseIndex.create(:model_constant => model_constant, :field_name => field_name, :word => word, :id_list => object_id)
    end
  end
  repair_index(model_constant, object_id, field_name, forward_index) if repair_on_completion
end
remove_index_id(model_constant, object_id) click to toggle source
# File lib/ar_indexer/indexer.rb, line 38
def self.remove_index_id(model_constant, object_id)
  index_records = ReverseIndex.where(:model_constant => model_constant)
  if index_records.count > 0
    index_records.each do |record|
      if record.id_list.match(/#{object_id},{0,1}/)
        current_id_array = record.retrieve_id_array
        if current_id_array.delete(object_id)
          if current_id_array.empty?
            record.destroy
          else
            new_id_list = current_id_array.join(',')
            record.update(:id_list => new_id_list)
          end
        end
      end
    end
  end
end
repair_index(model_constant, object_id, field_name, forward_index) click to toggle source
# File lib/ar_indexer/indexer.rb, line 57
def self.repair_index(model_constant, object_id, field_name, forward_index)
  index_records = ReverseIndex.where(:model_constant => model_constant, :field_name => field_name)
  if index_records.count > 0
    index_records.each do |record|
      if record.id_list.match(/#{object_id},{0,1}/)
        unless forward_index.include?(record.word)
          current_id_array = record.retrieve_id_array
          if current_id_array.delete(object_id)
            if current_id_array.empty?
              record.destroy
            else
              new_id_list = current_id_array.join(',')
              record.update(:id_list => new_id_list)
            end
          end
        end
      end
    end
  end
end