Class: Tire::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/goodyear/index.rb

Instance Method Summary collapse

Instance Method Details

#get_index_from_document(document) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/goodyear/index.rb', line 37

def get_index_from_document(document)
  ind = case
    when document.is_a?(Hash)
      document[:_index] || document['_index']
    when document.respond_to?(:_index)
      document._index rescue nil
  end

  return ind if ind.to_s != @name.to_s
end

#store(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/goodyear/index.rb', line 3

def store(*args)
  document, options = args

  id       = get_id_from_document(document)
  type     = get_type_from_document(document)
  index    = get_index_from_document(document)
  document = convert_document_to_json(document)

  options ||= {}
  params    = {}

  if options[:percolate]
    params[:percolate] = options[:percolate]
    params[:percolate] = "*" if params[:percolate] === true
  end

  params[:parent]  = options[:parent]  if options[:parent]
  params[:routing] = options[:routing] if options[:routing]
  params[:replication] = options[:replication] if options[:replication]
  params[:version] = options[:version] if options[:version]

  params_encoded = params.empty? ? '' : "?#{params.to_param}"

  url_with_index = index.blank? ? self.url : self.url.gsub(name, index)
  url  = id ? "#{url_with_index}/#{type}/#{Utils.escape(id)}#{params_encoded}" : "#{url_with_index}/#{type}/#{params_encoded}"

  @response = Configuration.client.post url, document
  MultiJson.decode(@response.body)
ensure
  curl = %Q|curl -X POST "#{url}" -d '#{document}'|
  logged([type, id].join('/'), curl)
end