Class: Slingshot::Index

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Index

Returns a new instance of Index.



6
7
8
9
# File 'lib/slingshot/index.rb', line 6

def initialize(name, &block)
  @name = name
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/slingshot/index.rb', line 4

def name
  @name
end

Instance Method Details

#bulk_store(documents) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/slingshot/index.rb', line 77

def bulk_store documents
  create unless exists?

  payload = documents.map do |document|
    old_verbose, $VERBOSE = $VERBOSE, nil # Silence Object#id deprecation warnings
    id = case
      when document.is_a?(Hash)                                           then document[:id] || document['id']
      when document.respond_to?(:id) && document.id != document.object_id then document.id
      # TODO: Raise error when no id present
    end
    $VERBOSE = old_verbose

    type = case
      when document.is_a?(Hash)                 then document[:type] || document['type']
      when document.respond_to?(:document_type) then document.document_type
    end || 'document'

    output = []
    output << %Q|{"index":{"_index":"#{@name}","_type":"#{type}","_id":"#{id}"}}|
    output << document.to_indexed_json
    output.join("\n")
  end
  payload << ""

  tries = 5
  count = 0

  begin
    # STDERR.puts "Posting payload..."
    # STDERR.puts payload.join("\n")
    Configuration.client.post("#{Configuration.url}/_bulk", payload.join("\n"))
  rescue Exception => error
    if count < tries
      count += 1
      STDERR.puts "[ERROR] #{error.message}:#{error.http_body rescue nil}, retrying (#{count})..."
      retry
    else
      STDERR.puts "[ERROR] Too many exceptions occured, giving up..."
      STDERR.puts "Response: #{error.http_body rescue nil}"
      raise
    end
  ensure
    curl = %Q|curl -X POST "#{Configuration.url}/_bulk" -d '{... data omitted ...}'|
    logged(error, 'BULK', curl)
  end
end

#create(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/slingshot/index.rb', line 28

def create(options={})
  @options = options
  @response = Configuration.client.post "#{Configuration.url}/#{@name}", Yajl::Encoder.encode(options)
rescue Exception => error
  false
ensure
  curl = %Q|curl -X POST "#{Configuration.url}/#{@name}" -d '#{Yajl::Encoder.encode(options, :pretty => true)}'|
  logged(error, 'CREATE', curl)
end

#deleteObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/slingshot/index.rb', line 17

def delete
  # FIXME: RestClient does not return response for DELETE requests?
  @response = Configuration.client.delete "#{Configuration.url}/#{@name}"
  return @response.body =~ /error/ ? false : true
rescue Exception => error
  false
ensure
  curl = %Q|curl -X DELETE "#{Configuration.url}/#{@name}"|
  logged(error, 'DELETE', curl)
end

#exists?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/slingshot/index.rb', line 11

def exists?
  !!Configuration.client.get("#{Configuration.url}/#{@name}/_status")
rescue Exception => error
  false
end

#import(klass_or_collection, method = nil, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/slingshot/index.rb', line 124

def import(klass_or_collection, method=nil, options={})
  # p [klass_or_collection, method, options]

  case

    when method
      options = {:page => 1, :per_page => 1000}.merge options
      while documents = klass_or_collection.send(method.to_sym, options.merge(:page => options[:page])) \
                        and not documents.empty?
        documents = yield documents if block_given?

        bulk_store documents
        options[:page] += 1
      end

    when klass_or_collection.respond_to?(:map)
      documents = block_given? ? yield(klass_or_collection) : klass_or_collection
      bulk_store documents
    else
      raise ArgumentError, "Please pass either a collection of objects, "+
                           "or method for fetching records, or Enumerable compatible class"
  end
end

#logged(error = nil, endpoint = '/', curl = '') ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/slingshot/index.rb', line 190

def logged(error=nil, endpoint='/', curl='')
  if Configuration.logger

    Configuration.logger.log_request endpoint, @name, curl

    code = @response ? @response.code : error.message rescue 200

    if Configuration.logger.level.to_s == 'debug'
      # FIXME: Depends on RestClient implementation
      body = @response ? Yajl::Encoder.encode(@response.body, :pretty => true) : error.http_body rescue ''
    else
      body = ''
    end

    Configuration.logger.log_response code, nil, body
  end
end

#mappingObject



38
39
40
41
# File 'lib/slingshot/index.rb', line 38

def mapping
  @response = Configuration.client.get("#{Configuration.url}/#{@name}/_mapping")
  JSON.parse(@response.body)[@name]
end

#refreshObject



181
182
183
184
185
186
187
188
# File 'lib/slingshot/index.rb', line 181

def refresh
  @response = Configuration.client.post "#{Configuration.url}/#{@name}/_refresh", ''
rescue Exception => error
  raise
ensure
  curl = %Q|curl -X POST "#{Configuration.url}/#{@name}/_refresh"|
  logged(error, '_refresh', curl)
end

#remove(*args) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/slingshot/index.rb', line 148

def remove(*args)
  # TODO: Infer type from the document (hash property, method)

  if args.size > 1
    (type, document = args)
  else
    (document = args.pop; type = :document)
  end

  old_verbose, $VERBOSE = $VERBOSE, nil # Silence Object#id deprecation warnings
  id = case true
    when document.is_a?(Hash)                                           then document[:id] || document['id']
    when document.respond_to?(:id) && document.id != document.object_id then document.id
    else document
  end
  $VERBOSE = old_verbose

  result = Configuration.client.delete "#{Configuration.url}/#{@name}/#{type}/#{id}"
  JSON.parse(result) if result
end

#retrieve(type, id) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/slingshot/index.rb', line 169

def retrieve(type, id)
  @response = Configuration.client.get "#{Configuration.url}/#{@name}/#{type}/#{id}"
  h = JSON.parse(@response.body)
  if Configuration.wrapper == Hash then h
  else
    document = {}
    document = h['_source'] ? document.update( h['_source'] ) : document.update( h['fields'] )
    document.update('id' => h['_id'], '_version' => h['_version'])
    Configuration.wrapper.new(document)
  end
end

#store(*args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/slingshot/index.rb', line 43

def store(*args)
  # TODO: Infer type from the document (hash property, method)

  if args.size > 1
    (type, document = args)
  else
    (document = args.pop; type = :document)
  end

  old_verbose, $VERBOSE = $VERBOSE, nil # Silence Object#id deprecation warnings
  id = case true
    when document.is_a?(Hash)                                           then document[:id] || document['id']
    when document.respond_to?(:id) && document.id != document.object_id then document.id
  end
  $VERBOSE = old_verbose

  document = case true
    when document.is_a?(String) then document
    when document.respond_to?(:to_indexed_json) then document.to_indexed_json
    else raise ArgumentError, "Please pass a JSON string or object with a 'to_indexed_json' method"
  end

  url = id ? "#{Configuration.url}/#{@name}/#{type}/#{id}" : "#{Configuration.url}/#{@name}/#{type}/"

  @response = Configuration.client.post url, document
  JSON.parse(@response.body)

rescue Exception => error
  raise
ensure
  curl = %Q|curl -X POST "#{url}" -d '#{document}'|
  logged(error, "/#{@name}/#{type}/", curl)
end