Class: RSolr::JSON::Generator

Inherits:
Generator show all
Defined in:
lib/rsolr/json.rb

Constant Summary collapse

CONTENT_TYPE =
'application/json'.freeze

Instance Method Summary collapse

Instance Method Details

#add(data, add_attrs = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rsolr/json.rb', line 11

def add data, add_attrs = {}
  add_attrs ||= {}
  data = RSolr::Array.wrap(data)

  if add_attrs.empty? && data.none? { |doc| doc.is_a?(RSolr::Document) && !doc.attrs.empty? }
    data.map do |doc|
      doc = RSolr::Document.new(doc) if doc.respond_to?(:each_pair)
      yield doc if block_given?
      doc.as_json
    end.to_json
  else
    i = 0
    data.each_with_object({}) do |doc, hash|
      doc = RSolr::Document.new(doc) if doc.respond_to?(:each_pair)
      yield doc if block_given?
      hash["add__UNIQUE_RSOLR_SUFFIX_#{i += 1}"] = add_attrs.merge(doc.attrs).merge(doc: doc.as_json)
    end.to_json.gsub(/__UNIQUE_RSOLR_SUFFIX_\d+/, '')
  end
end

#commit(opts = {}) ⇒ Object

generates a commit message



32
33
34
35
# File 'lib/rsolr/json.rb', line 32

def commit(opts = {})
  opts ||= {}
  { commit: opts }.to_json
end

#content_typeObject



7
8
9
# File 'lib/rsolr/json.rb', line 7

def content_type
  CONTENT_TYPE
end

#delete_by_id(ids) ⇒ Object

generates a delete message “ids” can be a single value or array of values



50
51
52
# File 'lib/rsolr/json.rb', line 50

def delete_by_id(ids)
  { delete: ids }.to_json
end

#delete_by_query(queries) ⇒ Object

generates a delete message “queries” can be a single value or an array of values



56
57
58
# File 'lib/rsolr/json.rb', line 56

def delete_by_query(queries)
  { delete: { query: queries } }.to_json
end

#optimize(opts = {}) ⇒ Object

generates a optimize message



38
39
40
41
# File 'lib/rsolr/json.rb', line 38

def optimize(opts = {})
  opts ||= {}
  { optimize: opts }.to_json
end

#rollbackObject

generates a rollback message



44
45
46
# File 'lib/rsolr/json.rb', line 44

def rollback
  { rollback: {} }.to_json
end