Class: RSolr::Xml::Generator

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.use_nokogiriObject

Returns the value of attribute use_nokogiri


99
100
101
# File 'lib/rsolr/xml.rb', line 99

def use_nokogiri
  @use_nokogiri
end

Class Method Details

.builder_procObject


101
102
103
104
105
106
107
108
109
# File 'lib/rsolr/xml.rb', line 101

def builder_proc
  if use_nokogiri 
    require 'nokogiri' unless defined?(::Nokogiri::XML::Builder)
    :nokogiri_build
  else
    require 'builder' unless defined?(::Builder::XmlMarkup)
    :builder_build
  end
end

Instance Method Details

#add(data, add_attrs = nil, &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”.


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rsolr/xml.rb', line 157

def add data, add_attrs = nil, &block
  add_attrs ||= {}
  data = [data] unless data.is_a?(Array)
  build do |xml|
    xml.add(add_attrs) do |add_node|
      data.each do |doc|
        doc = RSolr::Xml::Document.new(doc) if doc.respond_to?(:each_pair)
        yield doc if block_given?
        doc_node_builder = lambda do |doc_node|
          doc.fields.each do |field_obj|
            doc_node.field field_obj.value, field_obj.attrs
          end
        end
        self.class.use_nokogiri ? add_node.doc_(doc.attrs,&doc_node_builder) : add_node.doc(doc.attrs,&doc_node_builder)
      end
    end
  end
end

#build(&block) ⇒ Object


128
129
130
# File 'lib/rsolr/xml.rb', line 128

def build &block
  self.send(self.class.builder_proc,&block)
end

#commit(opts = nil) ⇒ Object

generates a <commit/> message


177
178
179
180
# File 'lib/rsolr/xml.rb', line 177

def commit opts = nil
  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


195
196
197
198
199
200
201
202
203
204
# File 'lib/rsolr/xml.rb', line 195

def delete_by_id ids
  ids = [ids] unless ids.is_a?(Array)
  build do |xml|
    xml.delete do |delete_node|
      ids.each do |id| 
        self.class.use_nokogiri ? delete_node.id_(id) : delete_node.id(id)
      end
    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


208
209
210
211
212
213
214
215
# File 'lib/rsolr/xml.rb', line 208

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

generates a <optimize/> message


183
184
185
186
# File 'lib/rsolr/xml.rb', line 183

def optimize opts = nil
  opts ||= {}
  build {|xml| xml.optimize(opts) }
end

#rollbackObject

generates a <rollback/> message


189
190
191
# File 'lib/rsolr/xml.rb', line 189

def rollback
  build {|xml| xml.rollback({}) }
end