Class: Solr::Document

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/solr/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Document

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')


25
26
27
28
# File 'lib/solr/document.rb', line 25

def initialize(hash={})
  @fields = []
  self << hash
end

Instance Attribute Details

#boostObject

Returns the value of attribute boost.



18
19
20
# File 'lib/solr/document.rb', line 18

def boost
  @boost
end

#fieldsObject (readonly)

Returns the value of attribute fields.



19
20
21
# File 'lib/solr/document.rb', line 19

def fields
  @fields
end

Instance Method Details

#<<(fields) ⇒ Object

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'}


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

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) ⇒ Object

shorthand to allow hash lookups

doc['name']


56
57
58
59
60
# File 'lib/solr/document.rb', line 56

def [](name)
  field = @fields.find {|f| f.name == name.to_s}
  return field.value if field
  return nil
end

#[]=(name, value) ⇒ Object

shorthand to assign as a hash



63
64
65
# File 'lib/solr/document.rb', line 63

def []=(name,value)
  @fields << Solr::Field.new(name => value)
end

#each(*args, &blk) ⇒ Object



75
76
77
# File 'lib/solr/document.rb', line 75

def each(*args, &blk)
  fields.each(&blk)
end

#to_xmlObject

convert the Document to a REXML::Element



68
69
70
71
72
73
# File 'lib/solr/document.rb', line 68

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