Class: RSolr::Document
- Inherits:
-
Object
- Object
- RSolr::Document
- Defined in:
- lib/rsolr/document.rb
Constant Summary collapse
- CHILD_DOCUMENT_KEY =
'_childDocuments_'.freeze
Instance Attribute Summary collapse
-
#attrs ⇒ Object
“attrs” is a hash for setting the “doc” xml attributes “fields” is an array of Field objects.
-
#fields ⇒ Object
“attrs” is a hash for setting the “doc” xml attributes “fields” is an array of Field objects.
Instance Method Summary collapse
-
#add_field(name, values, options = {}) ⇒ Object
Add a field value to the document.
- #as_json ⇒ Object
-
#field_by_name(name) ⇒ Object
returns the first field that matches the “name” arg.
-
#fields_by_name(name) ⇒ Object
returns an array of fields that match the “name” arg.
-
#initialize(doc_hash = {}) ⇒ Document
constructor
“doc_hash” must be a Hash/Mash object If a value in the “doc_hash” is an array, a field object is created for each value…
Constructor Details
#initialize(doc_hash = {}) ⇒ Document
“doc_hash” must be a Hash/Mash object If a value in the “doc_hash” is an array, a field object is created for each value…
12 13 14 15 16 17 18 |
# File 'lib/rsolr/document.rb', line 12 def initialize(doc_hash = {}) @fields = [] doc_hash.each_pair do |field, values| add_field(field, values) end @attrs={} end |
Instance Attribute Details
#attrs ⇒ Object
“attrs” is a hash for setting the “doc” xml attributes “fields” is an array of Field objects
7 8 9 |
# File 'lib/rsolr/document.rb', line 7 def attrs @attrs end |
#fields ⇒ Object
“attrs” is a hash for setting the “doc” xml attributes “fields” is an array of Field objects
7 8 9 |
# File 'lib/rsolr/document.rb', line 7 def fields @fields end |
Instance Method Details
#add_field(name, values, options = {}) ⇒ Object
Add a field value to the document. Options map directly to XML attributes in the Solr <field> node. See wiki.apache.org/solr/UpdateXmlMessages#head-8315b8028923d028950ff750a57ee22cbf7977c6
Example:
document.add_field('title', 'A Title', :boost => 2.0)
39 40 41 42 43 44 45 46 |
# File 'lib/rsolr/document.rb', line 39 def add_field(name, values, = {}) RSolr::Array.wrap(values).each do |v| field_attrs = { name: name } field_attrs[:type] = DocumentField if name.to_s == CHILD_DOCUMENT_KEY @fields << RSolr::Field.instance(.merge(field_attrs), v) end end |
#as_json ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rsolr/document.rb', line 48 def as_json @fields.group_by(&:name).each_with_object({}) do |(field, values), result| v = values.map(&:as_json) if v.length > 1 && v.first.is_a?(Hash) && v.first.key?(:value) v = v.first.merge(value: v.map { |single| single[:value] }) end v = v.first if v.length == 1 && field.to_s != CHILD_DOCUMENT_KEY result[field] = v end end |
#field_by_name(name) ⇒ Object
returns the first field that matches the “name” arg
26 27 28 |
# File 'lib/rsolr/document.rb', line 26 def field_by_name(name) @fields.detect{|f|f.name==name} end |
#fields_by_name(name) ⇒ Object
returns an array of fields that match the “name” arg
21 22 23 |
# File 'lib/rsolr/document.rb', line 21 def fields_by_name(name) @fields.select{|f|f.name==name} end |