Class: RSolr::Message::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject

b = Builder.new b.adapter = RSolr::Message::Adapter::LibXML.new b.optimize == ‘<optimize/>’



87
88
89
# File 'lib/rsolr/message.rb', line 87

def adapter
  @adapter ||= RSolr::Message::Adapter::Builder.new
end

Instance Method Details

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



116
117
118
119
120
121
122
123
124
# File 'lib/rsolr/message.rb', line 116

def add(data, add_attrs={})
  data = [data] unless data.is_a?(Array)
  documents = data.map do |doc|
    doc = Document.new(doc) if doc.respond_to?(:each_pair)
    yield doc if block_given?
    doc
  end
  adapter.add(documents, add_attrs)
end

#commit(opts = {}) ⇒ Object

generates a <commit/> message



127
128
129
# File 'lib/rsolr/message.rb', line 127

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



143
144
145
# File 'lib/rsolr/message.rb', line 143

def delete_by_id(ids)
  adapter.delete_by_id(ids)
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



149
150
151
# File 'lib/rsolr/message.rb', line 149

def delete_by_query(queries)
  adapter.delete_by_query(queries)
end

#optimize(opts = {}) ⇒ Object

generates a <optimize/> message



132
133
134
# File 'lib/rsolr/message.rb', line 132

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

#rollbackObject

generates a <rollback/> message



137
138
139
# File 'lib/rsolr/message.rb', line 137

def rollback
  adapter.rollback
end