Module: ActsAsSolr::CommonMethods

Included in:
ClassMethods
Defined in:
lib/acts_as_solr/common_methods.rb

Constant Summary collapse

TypeMapping =
{
  :double => "do",
  :float => "f",
  :decimal => "f",
  :integer => "i",
  :boolean => "b",
  :string => "s",
  :date => "d",
  :range_float => "rf",
  :range_integer => "ri",
  :facet => "facet",
  :text => "t",
}

Instance Method Summary collapse

Instance Method Details

#get_solr_field_type(field_type) ⇒ Object

Converts field types into Solr types


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/acts_as_solr/common_methods.rb', line 20

def get_solr_field_type(field_type)
  if field_type.is_a?(Symbol)
    t = TypeMapping[field_type]
    raise "Unknown field_type symbol: #{field_type}" if t.nil?
    t
  elsif field_type.is_a?(String)
    return field_type
  else
    raise "Unknown field_type class: #{field_type.class}: #{field_type}"
  end
end

#record_id(object) ⇒ Object

Returns the id for the given instance


73
74
75
# File 'lib/acts_as_solr/common_methods.rb', line 73

def record_id(object)
  eval "object.#{object.class.primary_key}"
end

#solr_add(add_xml) ⇒ Object

Sends an add command to Solr


44
45
46
# File 'lib/acts_as_solr/common_methods.rb', line 44

def solr_add(add_xml)
  ActsAsSolr::Post.execute(Solr::Request::AddDocument.new(add_xml))
end

#solr_batch_add(objects) ⇒ Object


32
33
34
35
# File 'lib/acts_as_solr/common_methods.rb', line 32

def solr_batch_add(objects)
  solr_add objects.map{ |a| a.to_solr_doc }
  solr_commit if defined?(configuration) and configuration[:auto_commit]
end

#solr_batch_add_association(ar, association) ⇒ Object


37
38
39
40
41
# File 'lib/acts_as_solr/common_methods.rb', line 37

def solr_batch_add_association(ar, association)
  result = ar.send(association)
  result = [result] unless result.is_a?(Array)
  solr_batch_add result
end

#solr_commitObject

Sends the commit command to Solr


54
55
56
# File 'lib/acts_as_solr/common_methods.rb', line 54

def solr_commit
  ActsAsSolr::Post.execute(Solr::Request::Commit.new)
end

#solr_delete(solr_ids) ⇒ Object

Sends the delete command to Solr


49
50
51
# File 'lib/acts_as_solr/common_methods.rb', line 49

def solr_delete(solr_ids)
  ActsAsSolr::Post.execute(Solr::Request::Delete.new(:id => solr_ids))
end

#solr_optimizeObject

Optimizes the Solr index. Solr says:

Optimizations can take nearly ten minutes to run. We are presuming optimizations should be run once following large batch-like updates to the collection and/or once a day.

One of the solutions for this would be to create a cron job that runs every day at midnight and optmizes the index:

0 0 * * * /your_rails_dir/script/runner -e production "Model.solr_optimize"

68
69
70
# File 'lib/acts_as_solr/common_methods.rb', line 68

def solr_optimize
  ActsAsSolr::Post.execute(Solr::Request::Optimize.new)
end