Class: Tire::Index

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Index

Returns a new instance of Index.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tire/index.rb', line 6

def initialize(*args, &block)
  case args.length
  when 2
    @name = args.last
    self.url = args.first
  when 1
    @name = args.first
    self.url = Configuration.url
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
  end

  block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#add_alias(alias_name, configuration = {}) ⇒ Object



55
56
57
# File 'lib/tire/index.rb', line 55

def add_alias(alias_name, configuration={})
  Alias.create(configuration.merge( :name => alias_name, :index => @name, :url => @base_url ) )
end

#aliases(alias_name = nil) ⇒ Object



63
64
65
# File 'lib/tire/index.rb', line 63

def aliases(alias_name=nil)
  alias_name ? Alias.all(@base_url, @name).select { |a| a.name == alias_name }.first : Alias.all(@base_url, @name)
end

#analyze(text, options = {}) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/tire/index.rb', line 246

def analyze(text, options={})
  options = {:pretty => true}.update(options)
  params  = options.to_param
  @response = Configuration.client.get "#{url}/_analyze?#{params}", text
  @response.success? ? MultiJson.decode(@response.body) : false

ensure
  curl = %Q|curl -X GET "#{url}/_analyze?#{params}" -d '#{text}'|
  logged('_analyze', curl)
end

#bulk_store(documents, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/tire/index.rb', line 101

def bulk_store(documents, options={})
  payload = documents.map do |document|
    type = get_type_from_document(document, :escape => false) # Do not URL-escape the _type
    id   = get_id_from_document(document)

    STDERR.puts "[ERROR] Document #{document.inspect} does not have ID" unless id

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

  tries = 5
  count = 0

  begin
    response = Configuration.client.post("#{url}/_bulk", payload.join("\n"))
    raise RuntimeError, "#{response.code} > #{response.body}" if response.failure?
    response
  rescue StandardError => error
    if count < tries
      count += 1
      STDERR.puts "[ERROR] #{error.message}, retrying (#{count})..."
      retry
    else
      STDERR.puts "[ERROR] Too many exceptions occured, giving up. The HTTP response was: #{error.message}"
      raise if options[:raise]
    end

  ensure
    curl = %Q|curl -X POST "#{url}/_bulk" -d '{... data omitted ...}'|
    logged('BULK', curl)
  end
end

#close(options = {}) ⇒ Object



237
238
239
240
241
242
243
244
# File 'lib/tire/index.rb', line 237

def close(options={})
  @response = Configuration.client.post "#{url}/_close", MultiJson.encode(options)
  MultiJson.decode(@response.body)['ok']

ensure
  curl = %Q|curl -X POST "#{url}/_close"|
  logged('_close', curl)
end

#convert_document_to_json(document) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
# File 'lib/tire/index.rb', line 374

def convert_document_to_json(document)
  document = case
    when document.is_a?(String)
      Tire.warn "Passing the document as JSON string in Index#store has been deprecated, " +
                 "please pass an object which responds to `to_indexed_json` or a plain Hash."
      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," +
                              "'#{document.class}' given."
  end
end

#create(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/tire/index.rb', line 45

def create(options={})
  @options = options
  @response = Configuration.client.post url, MultiJson.encode(options)
  @response.success? ? @response : false

ensure
  curl = %Q|curl -X POST #{url} -d '#{MultiJson.encode(options)}'|
  logged('CREATE', curl)
end

#deleteObject



36
37
38
39
40
41
42
43
# File 'lib/tire/index.rb', line 36

def delete
  @response = Configuration.client.delete url
  @response.success?

ensure
  curl = %Q|curl -X DELETE #{url}|
  logged('DELETE', curl)
end

#exists?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/tire/index.rb', line 27

def exists?
  @response = Configuration.client.head("#{url}")
  @response.success?

ensure
  curl = %Q|curl -I "#{url}"|
  logged('HEAD', curl)
end

#get_id_from_document(document) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
# File 'lib/tire/index.rb', line 340

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

#get_parent_from_document(document) ⇒ Object



363
364
365
366
367
368
369
370
371
372
# File 'lib/tire/index.rb', line 363

def get_parent_from_document(document)
  case
  when document.is_a?(Hash)
    document.delete(:_parent) || document.delete('_parent')
  when document.respond_to?(:parent)
    document.parent
  when document.respond_to?(:_parent)
    document._parent
  end
end

#get_routing_from_document(document) ⇒ Object



352
353
354
355
356
357
358
359
360
361
# File 'lib/tire/index.rb', line 352

def get_routing_from_document(document)
  case
  when document.is_a?(Hash)
    document.delete(:_routing) || document.delete('_routing')
  when document.respond_to?(:routing)
    document.routing
  when document.respond_to?(:_routing)
    document._routing
  end
end

#get_type_from_document(document, options = {}) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/tire/index.rb', line 320

def get_type_from_document(document, options={})
  options = {:escape => true}.merge(options)

  old_verbose, $VERBOSE = $VERBOSE, nil # Silence Object#type deprecation warnings
  type = case
    when document.is_a?(Hash)
      document.delete(:_type) || document.delete('_type') || document[:type] || document['type']
    when document.respond_to?(:document_type)
      document.document_type
    when document.respond_to?(:_type)
      document._type
    when document.respond_to?(:type) && document.type != document.class
      document.type
    end
  $VERBOSE = old_verbose

  type = type ? type.to_s : 'document'
  options[:escape] ? EscapeUtils.escape_url(type) : type
end

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tire/index.rb', line 138

def import(klass_or_collection, options={})
  case
    when method = options.delete(:method)
      options = {:page => 1, :per_page => 1000}.merge options
      while documents = klass_or_collection.send(method.to_sym, options.merge(:page => options[:page])) \
                        and documents.to_a.length > 0

        documents = yield documents if block_given?

        bulk_store documents, options
        options[:page] += 1
      end

    when klass_or_collection.respond_to?(:map)
      documents = block_given? ? yield(klass_or_collection) : klass_or_collection
      bulk_store documents, options

    else
      raise ArgumentError, "Please pass either an Enumerable compatible class, or a collection object" +
                           "with a method for fetching records in batches (such as 'paginate')"
  end
end

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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/tire/index.rb', line 298

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

    Configuration.logger.log_request endpoint, @name, curl

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

    if Configuration.logger.level.to_s == 'debug'
      body = if @response
        defined?(Yajl) ? Yajl::Encoder.encode(@response.body, :pretty => true) : MultiJson.encode(@response.body)
      else
        error.message rescue ''
      end
    else
      body = ''
    end

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

#mappingObject



67
68
69
70
# File 'lib/tire/index.rb', line 67

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

#open(options = {}) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/tire/index.rb', line 227

def open(options={})
  # TODO: Remove the duplication in the execute > rescue > ensure chain
  @response = Configuration.client.post "#{url}/_open", MultiJson.encode(options)
  MultiJson.decode(@response.body)['ok']

ensure
  curl = %Q|curl -X POST "#{url}/_open"|
  logged('_open', curl)
end

#percolate(*args, &block) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/tire/index.rb', line 279

def percolate(*args, &block)
  document = args.shift
  type     = get_type_from_document(document)

  document = MultiJson.decode convert_document_to_json(document)

  query = Search::Query.new(&block).to_hash if block_given?

  payload = { :doc => document }
  payload.update( :query => query ) if query

  @response = Configuration.client.get "#{url}/#{type}/_percolate", MultiJson.encode(payload)
  MultiJson.decode(@response.body)['matches']

ensure
  curl = %Q|curl -X GET "#{url}/#{type}/_percolate?pretty=1" -d '#{payload.to_json}'|
  logged('_percolate', curl)
end

#refreshObject



219
220
221
222
223
224
225
# File 'lib/tire/index.rb', line 219

def refresh
  @response = Configuration.client.post "#{url}/_refresh", ''

ensure
  curl = %Q|curl -X POST "#{url}/_refresh"|
  logged('_refresh', curl)
end

#register_percolator_query(name, options = {}, &block) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/tire/index.rb', line 257

def register_percolator_query(name, options={}, &block)
  options[:query] = Search::Query.new(&block).to_hash if block_given?

  url = "#@percolator_url/#{name}"
  @response = Configuration.client.put url, MultiJson.encode(options)
  MultiJson.decode(@response.body)['ok']

ensure
  curl = %Q|curl -X PUT "#{url}?pretty=true" -d '#{MultiJson.encode(options)}'|
  logged('_percolator', curl)
end

#reindex(name, options = {}, &block) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/tire/index.rb', line 161

def reindex(name, options={}, &block)
  new_index = Index.new(name)
  new_index.create(options) unless new_index.exists?

  Search::Scan.new(self.name, &block).each do |results|
    new_index.bulk_store results.map do |document|
      document.to_hash.except(:type, :_index, :_explanation, :_score, :_version, :highlight, :sort)
    end
  end
end

#remove(*args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/tire/index.rb', line 172

def remove(*args)
  if args.size > 1
    type, document = args
    type           = EscapeUtils.escape_url(type.to_s)
    id             = get_id_from_document(document) || document
  else
    document = args.pop
    type     = get_type_from_document(document)
    id       = get_id_from_document(document) || document
  end
  raise ArgumentError, "Please pass a document ID" unless id

  routing = get_routing_from_document(document)

  url = "#{self.url}/#{type}/#{id}"
  url << "?routing=#{EscapeUtils.escape_url(routing)}" if routing

  result = Configuration.client.delete url
  MultiJson.decode(result.body) if result.success?
ensure
  curl = %Q|curl -X DELETE "#{url}"|
  logged(id, curl)
end

#remove_alias(alias_name) ⇒ Object



59
60
61
# File 'lib/tire/index.rb', line 59

def remove_alias(alias_name)
  Alias.find(@base_url, alias_name) { |a| a.indices.delete @name }.save
end

#retrieve(type, id, params = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/tire/index.rb', line 196

def retrieve(type, id, params = {})
  raise ArgumentError, "Please pass a document ID" unless id

  type = EscapeUtils.escape_url(type.to_s)
  url  = "#{self.url}/#{type}/#{id}"
  url << "?#{params.to_param}" unless params.empty?

  @response = Configuration.client.get url

  h = MultiJson.decode(@response.body)
  if Configuration.wrapper == Hash then h
  else
    return nil if h['exists'] == false
    document = h['_source'] || h['fields'] || {}
    document.update('id' => h['_id'], '_type' => h['_type'], '_index' => h['_index'], '_version' => h['_version'])
    Configuration.wrapper.new(document)
  end

ensure
  curl = %Q|curl -X GET "#{url}"|
  logged(id, curl)
end

#settingsObject



72
73
74
75
# File 'lib/tire/index.rb', line 72

def settings
  @response = Configuration.client.get("#{url}/_settings")
  MultiJson.decode(@response.body)[@name]['settings']
end

#store(*args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tire/index.rb', line 77

def store(*args)
  document, params = args
  id      = get_id_from_document(document)
  type    = get_type_from_document(document)
  routing = get_routing_from_document(document)
  parent  = get_parent_from_document(document)

  params ||= {}
  params[:routing] = routing if routing
  params[:parent]  = parent  if parent
  params[:percolate] = '*' if params[:percolate] === true

  url = id ? "#{self.url}/#{type}/#{id}" : "#{self.url}/#{type}"
  url << "?#{params.to_param}" unless params.empty?

  document = convert_document_to_json(document)

  @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

#unregister_percolator_query(name) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/tire/index.rb', line 269

def unregister_percolator_query(name)
  url = "#@percolator_url/#{name}"
  @response = Configuration.client.delete url
  MultiJson.decode(@response.body)['ok']

ensure
  curl = %Q|curl -X DELETE "#{url}"|
  logged('_percolator', curl)
end