Module: ActsAsSolr::InstanceMethods

Defined in:
lib/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#indexing_disabled?Boolean

Returns:

  • (Boolean)


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

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

#init_solr(data) ⇒ Object



10
11
12
# File 'lib/instance_methods.rb', line 10

def init_solr(data)
  @solr_data = data
end

#method_missing_with_solr_magic(method, *a, &b) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/instance_methods.rb', line 14

def method_missing_with_solr_magic(method, *a, &b)
  if method.to_s =~ /^highlighted_(.*)$/ && a.length == 0
    original_field = $1
    @solr_data && @solr_data[:highlights] && @solr_data[:highlights][id] && 
      @solr_data[:highlights][id][original_field] && 
      @solr_data[:highlights][id][original_field].join(" ") || send(original_field)
  else
    method_missing_without_solr_magic(method, *a, &b)
  end
end

#solr_destroyObject

remove from index



49
50
51
52
53
54
55
56
57
# File 'lib/instance_methods.rb', line 49

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

#solr_idObject

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



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

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

#solr_saveObject

saves to the Solr index



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/instance_methods.rb', line 26

def solr_save
  if self.class.respond_to?(:acts_as_solr_needs_reload?) && self.class.acts_as_solr_needs_reload?
    self.class.acts_as_solr 
  end
  
  return true if indexing_disabled?
  if evaluate_condition(:if, self) 
    logger.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
rescue ConnectionError
  false
end

#to_solr_docObject

convert instance to Solr document



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/instance_methods.rb', line 60

def to_solr_doc
  logger.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|
    #field_type = configuration[:facets] && configuration[:facets].include?(field) ? :facet : :text
    
    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")
    value = set_value_if_nil(field_type) if value.to_s == ""
    
    # add the field to the document, but only if it's not the id field
    # or the type field (from single table inheritance), since these
    # fields have already been added above.
    if field_name.to_s != self.class.primary_key and field_name.to_s != "type"
      suffix = get_solr_field_type(field_type)
      # This next line ensures that e.g. nil dates are excluded from the 
      # document, since they choke Solr. Also ignores e.g. empty strings, 
      # but these can't be searched for anyway: 
      # http://www.mail-archive.com/[email protected]/msg05423.html
      next if value.nil? || value.to_s.strip.empty?
      [value].flatten.each do |v|
        v = set_value_if_nil(suffix) if value.to_s == ""
        field = Solr::Field.new("#{solr_name}_#{suffix}" => ERB::Util.html_escape(v.to_s))
        field.boost = validate_boost(field_boost)
        doc << field
      end
    end
  end
  
  add_includes(doc)
  logger.debug doc.to_xml
  doc
end