Class: Solr::Request::AddDocument

Inherits:
JsonUpdate show all
Defined in:
lib/solr/request/add_document.rb

Instance Method Summary collapse

Methods inherited from JsonUpdate

#handler, #response_format

Methods inherited from Base

#content_type, #handler, #response_format

Constructor Details

#initialize(doc = {}) ⇒ AddDocument

create the request, optionally passing in a Solr::Document

request = Solr::Request::AddDocument.new(doc)

as a short cut you can pass in a Hash instead:

request = Solr::Request::AddDocument.new(:creator => 'Jorge Luis Borges')

or an array, to add multiple documents at the same time:

request = Solr::Request::AddDocument.new([doc1, doc2, doc3]))

29
30
31
32
33
34
35
36
# File 'lib/solr/request/add_document.rb', line 29

def initialize(doc={})
  @docs = []
  if doc.is_a?(Array)
    doc.each { |d| add_doc(d) }
  else
    add_doc(doc)
  end
end

Instance Method Details

#to_jsonObject


38
39
40
41
42
# File 'lib/solr/request/add_document.rb', line 38

def to_json
  '{' + 
  @docs.map{ |doc| "\"add\": #{doc.to_jsonhash.to_json}" }.join(',') +
  '}'
end

#to_sObject


52
53
54
# File 'lib/solr/request/add_document.rb', line 52

def to_s
  to_json
end

#to_xmlObject


44
45
46
47
48
49
50
# File 'lib/solr/request/add_document.rb', line 44

def to_xml
  e = Solr::XML::Element.new 'add'
  for doc in @docs
    e.add_element doc.to_xml
  end
  return e.to_s
end