Module ActsAsSolr::InstanceMethods
In: lib/instance_methods.rb

Methods

Public Instance methods

[Source]

# File lib/instance_methods.rb, line 64
    def field(name, value)
      field = REXML::Element.new("field")
      field.add_attribute("name", name)
      field.add_text(value)
      field
    end

[Source]

# File lib/instance_methods.rb, line 27
    def solr_commit
      ActsAsSolr::Post.execute('<optimize waitFlush="false" waitSearcher="false"/>', :update)
    end

remove from index

[Source]

# File lib/instance_methods.rb, line 20
    def solr_destroy
      logger.debug "solr_destroy: #{self.class.name} : #{self.id}"
      ActsAsSolr::Post.execute("<delete><id>#{solr_id}</id></delete>", :update)
      solr_commit
      true
    end

[Source]

# File lib/instance_methods.rb, line 5
    def solr_id
      "#{self.class.name}:#{self.id}"
    end

saves to the Solr index

[Source]

# File lib/instance_methods.rb, line 10
    def solr_save
      logger.debug "solr_save: #{self.class.name} : #{self.id}"
      xml = REXML::Element.new('add')
      xml.add_element to_solr_doc
      response = ActsAsSolr::Post.execute(xml.to_s, :update)
      solr_commit
      true
    end

convert instance to Solr document

[Source]

# File lib/instance_methods.rb, line 32
    def to_solr_doc
      logger.debug "to_doc: creating doc for class: #{self.class.name}, id: #{self.id}"
      doc = REXML::Element.new('doc')

      # Solr id is <classname>:<id> to be unique across all models
      doc.add_element field("id", solr_id)
      doc.add_element field(solr_configuration[:type_field], self.class.name)
      doc.add_element field(solr_configuration[:primary_key_field], self.id.to_s)

      # iterate through the fields and add them to the document,
      configuration[:solr_fields].each do |field|
        field_name = field
        field_type = !configuration[:facets].nil? && configuration[:facets].include?(field.is_a?(String) ? field.to_sym : field) ? "facet" : "t"
        if field.is_a?(Hash)
          field_name = field.keys[0]
          field_type = get_solr_field_type(field.values[0])
        end
        value = self.send("#{field_name}_for_solr")
        
        # 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.to_s != "id") and (field.to_s != "type")
          doc.add_element field("#{field_name}_#{field_type}", value.to_s)
        end
      end
      
      add_includes(doc) unless configuration[:include].nil?
      logger.debug doc
      return doc
    end

[Validate]