Class: RSolr::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rsolr/message.rb

Overview

The Solr::Message class is the XML generation module for sending updates to Solr.

Defined Under Namespace

Classes: Document, Field

Class Method Summary collapse

Class Method Details

.add(data, add_attrs = {}, &blk) ⇒ 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”.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rsolr/message.rb', line 98

def add(data, add_attrs={}, &blk)
  data = [data] if data.respond_to?(:each_pair)
  xml.add(add_attrs) do |add_node|
    data.each do |item|
      # create doc, passing in fields
      doc = Document.new(item)
      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

.commit(opts = {}) ⇒ Object

generates a <commit/> message



115
116
117
# File 'lib/rsolr/message.rb', line 115

def commit(opts={})
  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



131
132
133
134
135
136
137
138
# File 'lib/rsolr/message.rb', line 131

def delete_by_id(ids)
  ids = [ids] unless ids.is_a?(Array)
  xml.delete do |xml|
    ids.each do |id|
      xml.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



142
143
144
145
146
147
148
149
# File 'lib/rsolr/message.rb', line 142

def delete_by_query(queries)
  queries = [queries] unless queries.is_a?(Array)
  xml.delete do |xml|
    queries.each do |query|
      xml.query(query)
    end
  end
end

.optimize(opts = {}) ⇒ Object

generates a <optimize/> message



120
121
122
# File 'lib/rsolr/message.rb', line 120

def optimize(opts={})
  xml.optimize(opts)
end

.rollbackObject

generates a <rollback/> message



125
126
127
# File 'lib/rsolr/message.rb', line 125

def rollback
  xml.rollback
end

.xmlObject

shortcut method -> xml = RSolr::Message.xml



69
70
71
# File 'lib/rsolr/message.rb', line 69

def xml
  ::Builder::XmlMarkup.new
end