Module: ActsAsSolr::InstanceMethods

Defined in:
lib/acts_as_solr/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#indexing_disabled?Boolean


23
24
25
# File 'lib/acts_as_solr/instance_methods.rb', line 23

def indexing_disabled?
  evaluate_condition(:offline, self) || !configuration[:if]
end

#solr_destroyObject

remove from index


28
29
30
31
32
33
34
# File 'lib/acts_as_solr/instance_methods.rb', line 28

def solr_destroy
  return true if indexing_disabled?
  debug "solr_destroy: #{self.class.name} : #{record_id(self)}"
  solr_delete solr_id
  solr_commit if configuration[:auto_commit]
  true
end

#solr_idObject

Solr id is <class.name>:<id> to be unique across all models


6
7
8
# File 'lib/acts_as_solr/instance_methods.rb', line 6

def solr_id
  "#{self.class.name}:#{record_id(self)}"
end

#solr_saveObject

saves to the Solr index


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/acts_as_solr/instance_methods.rb', line 11

def solr_save
  return true if indexing_disabled?
  if evaluate_condition(:if, self) 
    debug "solr_save: #{self.class.name} : #{record_id(self)}"
    solr_add to_solr_doc
    solr_commit if configuration[:auto_commit]
    true
  else
    solr_destroy
  end
end

#to_solr_docObject

convert instance to Solr document


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/acts_as_solr/instance_methods.rb', line 37

def to_solr_doc
  debug "to_solr_doc: creating doc for class: #{self.class.name}, id: #{record_id(self)}"
  doc = Solr::Document.new
  doc.boost = validate_boost(configuration[:boost]) if configuration[:boost]
  
  doc << {:id => solr_id,
          solr_configuration[:type_field] => self.class.name,
          solr_configuration[:primary_key_field] => record_id(self).to_s}

  # iterate through the fields and add them to the document,
  configuration[:solr_fields].each do |field_name, options|
    next if [self.class.primary_key, "type"].include?(field_name.to_s)

    field_boost = options[:boost] || solr_configuration[:default_boost]
    field_type = get_solr_field_type(options[:type])
    solr_name = options[:as] || field_name
    
    value = self.send("#{field_name}_for_solr") rescue nil
    next if value.nil?

    suffix = get_solr_field_type(field_type)
    value = Array(value).map{ |v| ERB::Util.html_escape(v) } # escape each value
    value = value.first if value.size == 1

    field = Solr::Field.new(:name => "#{solr_name}_#{suffix}", :value => value)
    processed_boost = validate_boost(field_boost)
    field.boost = processed_boost
    doc << field
  end
  
  add_dynamic_attributes(doc)
  add_includes(doc)
  add_tags(doc)
  add_space(doc)
  
  debug doc.to_json
  doc
end