class Solr::Document

Attributes

boost[RW]

Public Class Methods

new(hash={}) click to toggle source

Create a new Solr::Document, optionally passing in a hash of key/value pairs for the fields

doc = Solr::Document.new(:creator => 'Jorge Luis Borges')
# File lib/solr/document.rb, line 24
def initialize(hash={})
  @fields = []
  self << hash
end

Public Instance Methods

<<(fields) click to toggle source

Append a Solr::Field

doc << Solr::Field.new(:creator => 'Jorge Luis Borges')

If you are truly lazy you can simply pass in a hash:

doc << {:creator => 'Jorge Luis Borges'}
# File lib/solr/document.rb, line 36
def <<(fields)
  case fields
  when Hash
    fields.each_pair do |name,value|
      if value.respond_to?(:each) && !value.is_a?(String)
        value.each {|v| @fields << Solr::Field.new(name => v)}
      else
        @fields << Solr::Field.new(name => value)
      end
    end
  when Solr::Field
    @fields << fields
  else
    raise "must pass in Solr::Field or Hash"
  end
end
[](name) click to toggle source

shorthand to allow hash lookups

doc['name']
# File lib/solr/document.rb, line 55
def [](name)
  field = @fields.find {|f| f.name == name.to_s}
  return field.value if field
  return nil
end
[]=(name,value) click to toggle source

shorthand to assign as a hash

# File lib/solr/document.rb, line 62
def []=(name,value)
  @fields << Solr::Field.new(name => value)
end
to_xml() click to toggle source

convert the Document to a REXML::Element

# File lib/solr/document.rb, line 67
def to_xml
  e = Solr::XML::Element.new 'doc'
  e.attributes['boost'] = @boost.to_s if @boost
  @fields.each {|f| e.add_element(f.to_xml)}
  return e
end