Class: GSolr::Message::Generator
- Inherits:
-
Object
- Object
- GSolr::Message::Generator
- Defined in:
- lib/gsolr/message/generator.rb
Instance Method Summary collapse
-
#add(data, add_attrs = {}, &block) ⇒ Object
generates “add” xml for updating solr “data” can be a hash or an array of hashes.
- #build(&block) ⇒ Object
-
#commit(opts = {}) ⇒ Object
generates a <commit/> message.
-
#delete_by_id(ids) ⇒ Object
generates a <delete><id>ID</id></delete> message “ids” can be a single value or array of values.
-
#delete_by_query(queries) ⇒ Object
generates a <delete><query>ID</query></delete> message “queries” can be a single value or an array of values.
-
#optimize(opts = {}) ⇒ Object
generates a <optimize/> message.
-
#rollback(opts = {}) ⇒ Object
generates a <rollback/> message.
Instance Method Details
#add(data, add_attrs = {}, &block) ⇒ Object
generates “add” xml for updating solr “data” can be a hash or an array of hashes.
-
each hash should be a simple key=>value pair representing a solr doc.
If a value is an array, multiple fields will be created.
“add_attrs” can be a hash for setting the add xml element attributes.
This method can also accept a block. The value yielded to the block is a Message::Document; for each solr doc in “data”. You can set xml element attributes for each “doc” element or individual “field” elements.
For example:
solr.add(:nickname=>‘Tim’, :commitWithin=>1.0) do |doc_msg|
doc_msg.attrs[:boost] = 10.00 # boost the document
nickname = doc_msg.field_by_name(:nickname)
nickname.attrs[:boost] = 20 if nickname.value=='Tim' # boost a field
end
would result in an add element having the attributes boost=“10.0” and a commitWithin=“1.0”. Each doc element would have a boost=“10.0”. The “nickname” field would have a boost=“20.0” if the doc had a “nickname” field with the value of “Tim”.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gsolr/message/generator.rb', line 37 def add(data, add_attrs={}, &block) data = [data] unless data.is_a?(Array) build do |xml| xml.add(add_attrs) do |add_node| data.each do |doc| doc = GSolr::Message::Document.new(doc) if doc.respond_to?(:each_pair) yield doc if block_given? add_node.doc(doc.attrs) do |doc_node| doc.fields.each do |field_obj| doc_node.field field_obj.value, field_obj.attrs end end end end end end |
#build(&block) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/gsolr/message/generator.rb', line 5 def build(&block) require 'builder' b = ::Builder::XmlMarkup.new(:indent=>0, :margin=>0, :encoding => 'UTF-8') b.instruct! block_given? ? yield(b) : b end |
#commit(opts = {}) ⇒ Object
generates a <commit/> message
56 57 58 |
# File 'lib/gsolr/message/generator.rb', line 56 def commit(opts={}) build {|xml| xml.commit opts} end |
#delete_by_id(ids) ⇒ Object
generates a <delete><id>ID</id></delete> message “ids” can be a single value or array of values
72 73 74 75 76 77 78 79 |
# File 'lib/gsolr/message/generator.rb', line 72 def delete_by_id(ids) ids = [ids] unless ids.is_a?(Array) build do |xml| xml.delete do |delete_node| ids.each { |id| delete_node.id(id) } end end end |
#delete_by_query(queries) ⇒ Object
generates a <delete><query>ID</query></delete> message “queries” can be a single value or an array of values
83 84 85 86 87 88 89 90 |
# File 'lib/gsolr/message/generator.rb', line 83 def delete_by_query(queries) queries = [queries] unless queries.is_a?(Array) build do |xml| xml.delete do |delete_node| queries.each { |query| delete_node.query query } end end end |
#optimize(opts = {}) ⇒ Object
generates a <optimize/> message
61 62 63 |
# File 'lib/gsolr/message/generator.rb', line 61 def optimize(opts={}) build {|xml| xml.optimize opts} end |
#rollback(opts = {}) ⇒ Object
generates a <rollback/> message
66 67 68 |
# File 'lib/gsolr/message/generator.rb', line 66 def rollback opts={} build {|xml| xml.rollback opts} end |