class Solr4R::Builder

Constants

DEFAULT_OPTIONS
ILLEGAL_CHAR_RE

Attributes

client[R]

Public Class Methods

convert_value(value) click to toggle source
   # File lib/solr4r/builder.rb
49 def convert_value(value)
50   case value
51     when Time     then value.getutc.xmlschema
52     when DateTime then convert_value(value.to_time)
53     when Date     then convert_value(value.to_datetime)
54     else               value
55   end
56 end
new(client = Client, options = nil) click to toggle source
Calls superclass method
   # File lib/solr4r/builder.rb
60 def initialize(client = Client, options = nil)
61   raise ArgumentError,
62     'block argument not supported, use options hash instead' if block_given?
63 
64   @client = client
65 
66   @solr4r_doc = doc = Document.new
67   @solr4r_opt = opt = DEFAULT_OPTIONS.dup
68 
69   options ||= client.respond_to?(:options) ? client.options : {}
70   options.each { |k, v| opt[k.to_sym] = v if doc.respond_to?("#{k}=") }
71 
72   super(@solr4r_opt, @solr4r_doc)
73 end

Public Instance Methods

add(doc, attributes = {}) click to toggle source

See Schema.

Examples:

Single document
add(employeeId: '05991', office: 'Bridgewater', skills: %w[Perl Java])

Result:

<?xml version="1.0" encoding="UTF-8"?>
<add>
  <doc>
    <field name="employeeId">05991</field>
    <field name="office">Bridgewater</field>
    <field name="skills">Perl</field>
    <field name="skills">Java</field>
  </doc>
</add>
Multiple documents
add([{ employeeId: '05992', office: 'Blackwater' }, { employeeId: '05993', skills: 'Ruby' }])

Result:

<?xml version="1.0" encoding="UTF-8"?>
<add>
  <doc>
    <field name="employeeId">05992</field>
    <field name="office">Blackwater</field>
  </doc>
  <doc>
    <field name="employeeId">05993</field>
    <field name="skills">Ruby</field>
  </doc>
</add>
Add attributes
add([id: 42, text: 'blah'], commitWithin: 23)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<add commitWithin="23">
  <doc>
    <field name="id">42</field>
    <field name="text">blah</field>
  </doc>
</add>
Document attributes
add([[{ id: 42, text: 'blah' }, boost: 10.0]])

Result:

<?xml version="1.0" encoding="UTF-8"?>
<add>
  <doc boost="10.0">
    <field name="id">42</field>
    <field name="text">blah</field>
  </doc>
</add>
Field attributes
add(id: 42, text: ['blah', boost: 2.0])

Result:

<?xml version="1.0" encoding="UTF-8"?>
<add>
  <doc>
    <field name="id">42</field>
    <field name="text" boost="2.0">blah</field>
  </doc>
</add>
All attributes together
add([[{ id: 42, text: ['blah', boost: 2.0] }, boost: 10.0]], commitWithin: 23)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<add commitWithin="23">
  <doc boost="10.0">
    <field name="id">42</field>
    <field name="text" boost="2.0">blah</field>
  </doc>
</add>
    # File lib/solr4r/builder.rb
170 def add(doc, attributes = {})
171   to_xml(:add, attributes) { |add_node| _each(doc) { |hash, doc_attributes|
172     add_node.doc_(doc_attributes) { |doc_node| hash.each { |key, values|
173       field_attributes = { name: key }
174 
175       if values.is_a?(Array) && values.last.is_a?(Hash)
176         field_attributes.update((values = values.dup).pop)
177       end
178 
179       _each_value(values) { |value| doc_node.field_(value, field_attributes) }
180     } }
181   } }
182 end
Also aliased as: doc
commit(attributes = {}) click to toggle source

See Schema.

Examples:

Without options
commit

Result:

<?xml version="1.0" encoding="UTF-8"?>
<commit/>
With options
commit(softCommit: true)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<commit softCommit="true"/>
    # File lib/solr4r/builder.rb
207 def commit(attributes = {})
208   to_xml(:commit, attributes)
209 end
delete(hash) click to toggle source

See Schema.

See Query#query_string for handling of query hashes (via client.query_string).

Examples:

Single ID
delete(id: '05991')

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <id>05991</id>
</delete>
Multiple IDs
delete(id: %w[05991 06000])

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <id>05991</id>
  <id>06000</id>
</delete>
Single query
delete(query: 'office:Bridgewater')

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <query>office:Bridgewater</query>
</delete>
Multiple queries
delete(query: %w[office:Bridgewater office:Osaka])

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <query>office:Bridgewater</query>
  <query>office:Osaka</query>
</delete>
Query hash
delete(query: { office: 'Bridgewater', skills: 'Perl' })

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <query>office:Bridgewater skills:Perl</query>
</delete>
Query hash with array
delete(query: { office: %w[Bridgewater Osaka] })

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <query>office:Bridgewater office:Osaka</query>
</delete>
Query hash with LocalParams
delete(query: { office: 'Bridgewater', _: { type: :edismax } })

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <query>{!type=edismax}office:Bridgewater</query>
</delete>
Both IDs and queries
delete(id: %w[05991 06000], query: { office: %w[Bridgewater Osaka] })

Result:

<?xml version="1.0" encoding="UTF-8"?>
<delete>
  <id>05991</id>
  <id>06000</id>
  <query>office:Bridgewater office:Osaka</query>
</delete>
    # File lib/solr4r/builder.rb
347 def delete(hash)
348   to_xml(:delete) { |delete_node| hash.each { |key, values|
349     case key.to_s
350       when 'id'    then _each_value(values) { |value| delete_node.id_(value) }
351       when 'query' then _each_value(values) { |value| delete_node.query_(
352         client.query_string(value, false)) }
353       else raise ArgumentError, "`id' or `query' expected, got %p" % key
354     end
355   } }
356 end
doc(doc, attributes = {})
Alias for: add
inspect() click to toggle source
    # File lib/solr4r/builder.rb
358 def inspect
359   '#<%s:0x%x %p>' % [self.class, object_id, @solr4r_opt]
360 end
optimize(attributes = {}) click to toggle source

See Schema.

Examples:

Without options
optimize

Result:

<?xml version="1.0" encoding="UTF-8"?>
<optimize/>
With options
optimize(maxSegments: 42)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<optimize maxSegments="42"/>
    # File lib/solr4r/builder.rb
232 def optimize(attributes = {})
233   to_xml(:optimize, attributes)
234 end
rollback() click to toggle source

See Schema.

Example:

rollback

Result:

<?xml version="1.0" encoding="UTF-8"?>
<rollback/>
    # File lib/solr4r/builder.rb
246 def rollback
247   to_xml(:rollback)
248 end

Private Instance Methods

_each(values, &block) click to toggle source
    # File lib/solr4r/builder.rb
374 def _each(values, &block)
375   (values.respond_to?(:to_ary) ? values : [values]).each(&block)
376 end
_each_value(values) { |convert_value(value)| ... } click to toggle source
    # File lib/solr4r/builder.rb
378 def _each_value(values)
379   _each(values) { |value| yield convert_value(value) }
380 end
convert_value(value) click to toggle source
    # File lib/solr4r/builder.rb
382 def convert_value(value)
383   self.class.convert_value(value)
384 end
replace_illegal_characters(string) click to toggle source
    # File lib/solr4r/builder.rb
370 def replace_illegal_characters(string)
371   string.gsub(ILLEGAL_CHAR_RE, '')
372 end
to_xml(name, attributes = {}, &block) click to toggle source
Calls superclass method
    # File lib/solr4r/builder.rb
364 def to_xml(name, attributes = {}, &block)
365   self.parent = self.doc = @solr4r_doc.dup
366   method_missing(name, attributes, &block)
367   replace_illegal_characters(super(&nil))
368 end