Module: ActsAsSolr::CommonMethods

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

Instance Method Summary collapse

Instance Method Details

#get_solr_field_type(field_type) ⇒ Object

Converts field types into Solr types



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/common_methods.rb', line 6

def get_solr_field_type(field_type)
  if field_type.is_a?(Symbol)
    case field_type
      when :float
        return "f"
      when :integer
        return "i"
      when :boolean
        return "b"
      when :string
        return "s"
      when :date
        return "d"
      when :range_float
        return "rf"
      when :range_integer
        return "ri"
      when :facet
        return "facet"
      when :text
        return "t"
    else
      raise "Unknown field_type symbol: #{field_type}"
    end
  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



83
84
85
# File 'lib/common_methods.rb', line 83

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

#set_value_if_nil(field_type) ⇒ Object

Sets a default value when value being set is nil.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/common_methods.rb', line 38

def set_value_if_nil(field_type)
  case field_type
    when "b", :boolean
      return "false"
    when "s", "t", "d", :date, :string, :text
      return ""
    when "f", "rf", :float, :range_float
      return 0.00
    when "i", "ri", :integer, :range_integer
      return 0
  else
    return ""
  end
end

#solr_add(add_xml) ⇒ Object

Sends an add command to Solr



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

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

#solr_commitObject

Sends the commit command to Solr



64
65
66
# File 'lib/common_methods.rb', line 64

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

#solr_delete(solr_ids) ⇒ Object

Sends the delete command to Solr



59
60
61
# File 'lib/common_methods.rb', line 59

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"


78
79
80
# File 'lib/common_methods.rb', line 78

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