elastomer-client 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +12 -0
  4. data/CHANGELOG.md +15 -0
  5. data/README.md +6 -7
  6. data/Rakefile +21 -0
  7. data/docs/README.md +44 -0
  8. data/docs/bulk_indexing.md +3 -0
  9. data/docs/client.md +240 -0
  10. data/docs/cluster.md +148 -0
  11. data/docs/docs.md +254 -0
  12. data/docs/index.md +161 -0
  13. data/docs/multi_search.md +3 -0
  14. data/docs/notifications.md +24 -11
  15. data/docs/scan_scroll.md +3 -0
  16. data/docs/snapshots.md +3 -0
  17. data/docs/templates.md +3 -0
  18. data/docs/warmers.md +3 -0
  19. data/elastomer-client.gemspec +2 -2
  20. data/lib/elastomer/client.rb +70 -43
  21. data/lib/elastomer/client/bulk.rb +2 -2
  22. data/lib/elastomer/client/cluster.rb +2 -2
  23. data/lib/elastomer/client/docs.rb +190 -54
  24. data/lib/elastomer/client/errors.rb +4 -2
  25. data/lib/elastomer/client/index.rb +111 -43
  26. data/lib/elastomer/client/multi_search.rb +1 -1
  27. data/lib/elastomer/client/nodes.rb +9 -4
  28. data/lib/elastomer/client/repository.rb +2 -2
  29. data/lib/elastomer/client/scroller.rb +235 -0
  30. data/lib/elastomer/client/snapshot.rb +1 -1
  31. data/lib/elastomer/client/template.rb +1 -1
  32. data/lib/elastomer/client/warmer.rb +1 -1
  33. data/lib/elastomer/notifications.rb +1 -1
  34. data/lib/elastomer/version.rb +1 -1
  35. data/script/bootstrap +0 -7
  36. data/script/cibuild +8 -3
  37. data/script/test +6 -0
  38. data/test/client/bulk_test.rb +2 -2
  39. data/test/client/cluster_test.rb +23 -2
  40. data/test/client/docs_test.rb +137 -6
  41. data/test/client/errors_test.rb +12 -8
  42. data/test/client/index_test.rb +88 -5
  43. data/test/client/multi_search_test.rb +29 -0
  44. data/test/client/repository_test.rb +36 -37
  45. data/test/client/{scan_test.rb → scroller_test.rb} +25 -6
  46. data/test/client/snapshot_test.rb +53 -43
  47. data/test/client/stubbed_client_test.rb +1 -1
  48. data/test/client_test.rb +60 -0
  49. data/test/notifications_test.rb +69 -0
  50. data/test/test_helper.rb +54 -11
  51. metadata +36 -23
  52. data/.ruby-version +0 -1
  53. data/lib/elastomer/client/scan.rb +0 -161
  54. data/script/testsuite +0 -10
@@ -26,8 +26,10 @@ module Elastomer
26
26
 
27
27
  when Faraday::Response
28
28
  response = args.shift
29
- message = Hash === response.body && response.body['error'] || response.body.to_s
30
29
  @status = response.status
30
+ body = response.body
31
+
32
+ message = body.is_a?(Hash) && body['error'] || body.to_s
31
33
  super message
32
34
 
33
35
  else
@@ -59,7 +61,7 @@ module Elastomer
59
61
  @fatal = true
60
62
  end
61
63
  attr_writer :fatal
62
- alias :fatal? :fatal
64
+ alias_method :fatal?, :fatal
63
65
  end
64
66
 
65
67
  end # Error
@@ -1,12 +1,17 @@
1
1
  module Elastomer
2
2
  class Client
3
3
 
4
- # Provides access to index-level API commands.
4
+ # Provides access to index-level API commands. An index name is required for
5
+ # these API calls. If you want to operate on all inidces - flushing all
6
+ # indices, for example - then you will need to use the "_all" index name.
7
+ #
8
+ # You can override the index name for one-off calls by passing in the
9
+ # desired index name via the `:index` option.
5
10
  #
6
11
  # name - The name of the index as a String or an Array of names
7
12
  #
8
13
  # Returns an Index instance.
9
- def index( name )
14
+ def index( name = nil )
10
15
  Index.new self, name
11
16
  end
12
17
 
@@ -19,32 +24,34 @@ module Elastomer
19
24
  #
20
25
  def initialize( client, name )
21
26
  @client = client
22
- @name = @client.assert_param_presence(name, 'index name')
27
+ @name = @client.assert_param_presence(name, 'index name') unless name.nil?
23
28
  end
24
29
 
25
30
  attr_reader :client, :name
26
31
 
27
- # Check for the existence of the index. If a :type option is given, then
32
+ # Check for the existence of the index. If a `:type` option is given, then
28
33
  # we will check for the existence of the document type in the index.
29
34
  #
30
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists/
31
- # and http://www.elasticsearch.org/guide/reference/api/admin-indices-types-exists/
32
- #
33
35
  # params - Parameters Hash
36
+ # :type - optional type mapping as a String
37
+ #
38
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html
39
+ # and http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-types-exists.html
34
40
  #
35
41
  # Returns true if the index (or type) exists
36
42
  def exists?( params = {} )
37
43
  response = client.head '/{index}{/type}', update_params(params, :action => 'index.exists')
38
44
  response.success?
39
45
  end
40
- alias :exist? :exists?
46
+ alias_method :exist?, :exists?
41
47
 
42
48
  # Create the index.
43
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index/
44
49
  #
45
50
  # body - The index settings and mappings as a Hash or a JSON encoded String
46
51
  # params - Parameters Hash
47
52
  #
53
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html
54
+ #
48
55
  # Returns the response body as a Hash
49
56
  def create( body, params = {} )
50
57
  response = client.post '/{index}', update_params(params, :body => body, :action => 'index.create')
@@ -52,10 +59,11 @@ module Elastomer
52
59
  end
53
60
 
54
61
  # Delete the index.
55
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index/
56
62
  #
57
63
  # params - Parameters Hash
58
64
  #
65
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
66
+ #
59
67
  # Returns the response body as a Hash
60
68
  def delete( params = {} )
61
69
  response = client.delete '/{index}', update_params(params, :action => 'index.delete')
@@ -63,10 +71,11 @@ module Elastomer
63
71
  end
64
72
 
65
73
  # Open the index.
66
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close/
67
74
  #
68
75
  # params - Parameters Hash
69
76
  #
77
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
78
+ #
70
79
  # Returns the response body as a Hash
71
80
  def open( params = {} )
72
81
  response = client.post '/{index}/_open', update_params(params, :action => 'index.open')
@@ -74,10 +83,11 @@ module Elastomer
74
83
  end
75
84
 
76
85
  # Close the index.
77
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close/
78
86
  #
79
87
  # params - Parameters Hash
80
88
  #
89
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
90
+ #
81
91
  # Returns the response body as a Hash
82
92
  def close( params = {} )
83
93
  response = client.post '/{index}/_close', update_params(params, :action => 'index.close')
@@ -85,64 +95,69 @@ module Elastomer
85
95
  end
86
96
 
87
97
  # Retrieve the settings for the index.
88
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-get-settings/
89
98
  #
90
99
  # params - Parameters Hash
91
100
  #
101
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-settings.html
102
+ #
92
103
  # Returns the response body as a Hash
93
104
  def get_settings( params = {} )
94
105
  response = client.get '{/index}/_settings', update_params(params, :action => 'index.get_settings')
95
106
  response.body
96
107
  end
97
- alias :settings :get_settings
108
+ alias_method :settings, :get_settings
98
109
 
99
110
  # Change specific index level settings in real time.
100
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/
101
111
  #
102
112
  # body - The index settings as a Hash or a JSON encoded String
103
113
  # params - Parameters Hash
104
114
  #
115
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
116
+ #
105
117
  # Returns the response body as a Hash
106
118
  def update_settings( body, params = {} )
107
119
  response = client.put '{/index}/_settings', update_params(params, :body => body, :action => 'index.update_settings')
108
120
  response.body
109
121
  end
110
122
 
111
- # Retrive one or more mappings from the index. To retrieve a specific
112
- # mapping provide the name as the :type parameter.
113
- #
114
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping/
123
+ # Retrieve one or more mappings from the index. To retrieve a specific
124
+ # mapping provide the name as the `:type` parameter.
115
125
  #
116
126
  # params - Parameters Hash
127
+ # :type - specific document type as a String or Array of Strings
128
+ #
129
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html
117
130
  #
118
131
  # Returns the response body as a Hash
119
- def mapping( params = {} )
120
- response = client.get '/{index}{/type}/_mapping', update_params(params, :action => 'index.mapping')
132
+ def get_mapping( params = {} )
133
+ response = client.get '/{index}{/type}/_mapping', update_params(params, :action => 'index.get_mapping')
121
134
  response.body
122
135
  end
136
+ alias_method :mapping, :get_mapping
123
137
 
124
138
  # Register specific mapping definition for a specific type.
125
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/
126
139
  #
127
140
  # type - Name of the mapping to update as a String
128
141
  # body - The mapping values to update as a Hash or a JSON encoded String
129
142
  # params - Parameters Hash
130
143
  #
144
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
145
+ #
131
146
  # Returns the response body as a Hash
132
147
  def update_mapping( type, body, params = {} )
133
148
  response = client.put '/{index}/{type}/_mapping', update_params(params, :body => body, :type => type, :action => 'index.update_mapping')
134
149
  response.body
135
150
  end
136
- alias :put_mapping :update_mapping
151
+ alias_method :put_mapping, :update_mapping
137
152
 
138
153
  # Delete the mapping identified by `type`. This deletes all documents of
139
154
  # that type from the index.
140
155
  #
141
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping/
142
- #
143
156
  # type - Name of the mapping to update as a String
144
157
  # params - Parameters Hash
145
158
  #
159
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-mapping.html
160
+ #
146
161
  # Returns the response body as a Hash
147
162
  def delete_mapping( type, params = {} )
148
163
  response = client.delete '/{index}/{type}', update_params(params, :type => type, :action => 'index.delete_mapping')
@@ -160,7 +175,7 @@ module Elastomer
160
175
  response = client.get '/{index}/_aliases', update_params(:action => 'index.get_aliases')
161
176
  response.body
162
177
  end
163
- alias :aliases :get_aliases
178
+ alias_method :aliases, :get_aliases
164
179
 
165
180
  # Return the named aliases associated with this index.
166
181
  #
@@ -220,11 +235,12 @@ module Elastomer
220
235
  end
221
236
 
222
237
  # Performs the analysis process on a text and return the tokens breakdown of the text.
223
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/
224
238
  #
225
239
  # text - The text to analyze as a String
226
240
  # params - Parameters Hash
227
241
  #
242
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
243
+ #
228
244
  # Returns the response body as a Hash
229
245
  def analyze( text, params = {} )
230
246
  response = client.get '{/index}/_analyze', update_params(params, :body => text.to_s, :action => 'index.analyze')
@@ -234,9 +250,10 @@ module Elastomer
234
250
  # Explicitly refresh one or more index, making all operations performed
235
251
  # since the last refresh available for search.
236
252
  #
237
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh/
238
- #
239
253
  # params - Parameters Hash
254
+ # :index - set to "_all" to refresh all indices
255
+ #
256
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
240
257
  #
241
258
  # Returns the response body as a Hash
242
259
  def refresh( params = {} )
@@ -245,9 +262,11 @@ module Elastomer
245
262
  end
246
263
 
247
264
  # Flush one or more indices to the index storage.
248
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-flush/
249
265
  #
250
266
  # params - Parameters Hash
267
+ # :index - set to "_all" to flush all indices
268
+ #
269
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
251
270
  #
252
271
  # Returns the response body as a Hash
253
272
  def flush( params = {} )
@@ -257,9 +276,11 @@ module Elastomer
257
276
 
258
277
  # Optimize one or more indices. Optimizing an index allows for faster
259
278
  # search operations but can be resource intensive.
260
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-optimize/
261
279
  #
262
280
  # params - Parameters Hash
281
+ # :index - set to "_all" to optimize all indices
282
+ #
283
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html
263
284
  #
264
285
  # Returns the response body as a Hash
265
286
  def optimize( params = {} )
@@ -270,12 +291,13 @@ module Elastomer
270
291
  # Deprecated: Explicitly snapshot (backup) one or more indices to the
271
292
  # gateway. By default this happens periodically (every 1 second) but the
272
293
  # period can be changed or disabled completely.
273
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-gateway-snapshot/
274
294
  #
275
295
  # This API was removed in ES 1.2.
276
296
  #
277
297
  # params - Parameters Hash
278
298
  #
299
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/indices-gateway-snapshot.html
300
+ #
279
301
  # Returns the response body as a Hash
280
302
  def snapshot( params = {} )
281
303
  response = client.post '{/index}/_gateway/snapshot', update_params(params, :action => 'index.snapshot')
@@ -285,10 +307,10 @@ module Elastomer
285
307
  # Provides insight into on-going index shard recoveries. Recovery status
286
308
  # may be reported for specific indices, or cluster-wide.
287
309
  #
288
- # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-recovery.html
289
- #
290
310
  # params - Parameters Hash
291
311
  #
312
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-recovery.html
313
+ #
292
314
  # Returns the response body as a Hash
293
315
  def recovery( params = {} )
294
316
  response = client.get '{/index}/_recovery', update_params(params, :action => 'index.recovery')
@@ -297,9 +319,11 @@ module Elastomer
297
319
 
298
320
  # Clear caches for one or more indices. Individual caches can be
299
321
  # specified with parameters.
300
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache/
301
322
  #
302
323
  # params - Parameters Hash
324
+ # :index - set to "_all" to clear all index caches
325
+ #
326
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-clearcache.html
303
327
  #
304
328
  # Returns the response body as a Hash
305
329
  def clear_cache( params = {} )
@@ -309,22 +333,26 @@ module Elastomer
309
333
 
310
334
  # Retrieve statistics about one or more indices. Specific statistics
311
335
  # can be retrieved with parameters.
312
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-stats/
313
336
  #
314
337
  # params - Parameters Hash
315
338
  #
339
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-stats.html
340
+ #
316
341
  # Returns the response body as a Hash
317
342
  def stats( params = {} )
318
343
  response = client.get '{/index}/_stats', update_params(params, :action => 'index.stats')
319
344
  response.body
320
345
  end
321
346
 
322
- # Retrieve the status of one or more indices. Recovery and snapshot
323
- # status can be retrieved with parameters.
324
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-status/
347
+ # Deprecated: Retrieve the status of one or more indices. Recovery and
348
+ # snapshot status can be retrieved with parameters.
349
+ #
350
+ # This API was deprecated in 1.2 and is slated for removal.
325
351
  #
326
352
  # params - Parameters Hash
327
353
  #
354
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
355
+ #
328
356
  # Returns the response body as a Hash
329
357
  def status( params = {} )
330
358
  response = client.get '{/index}/_status', update_params(params, :action => 'index.status')
@@ -333,10 +361,11 @@ module Elastomer
333
361
 
334
362
  # Retrieve low level Lucene segments information for shards of one
335
363
  # or more indices.
336
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-segments/
337
364
  #
338
365
  # params - Parameters Hash
339
366
  #
367
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-segments.html
368
+ #
340
369
  # Returns the response body as a Hash
341
370
  def segments( params = {} )
342
371
  response = client.get '{/index}/_segments', update_params(params, :action => 'index.segments')
@@ -348,6 +377,8 @@ module Elastomer
348
377
  #
349
378
  # type - The document type as a String
350
379
  #
380
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs.html
381
+ #
351
382
  # Returns a Docs instance.
352
383
  def docs( type = nil )
353
384
  client.docs name, type
@@ -372,6 +403,8 @@ module Elastomer
372
403
  # ...
373
404
  # end
374
405
  #
406
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
407
+ #
375
408
  # Returns the response body as a Hash
376
409
  def bulk( params = {}, &block )
377
410
  raise 'a block is required' if block.nil?
@@ -380,8 +413,37 @@ module Elastomer
380
413
  client.bulk params, &block
381
414
  end
382
415
 
383
- # Create a new Scan instance for scrolling all results from a `query`.
384
- # The Scan will be scoped to the current index.
416
+ # Create a new Scroller instance for scrolling all results from a `query`.
417
+ # The Scroller will be scoped to the current index.
418
+ #
419
+ # query - The query to scroll as a Hash or a JSON encoded String
420
+ # opts - Options Hash
421
+ # :index - the name of the index to search
422
+ # :type - the document type to search
423
+ # :scroll - the keep alive time of the scrolling request (5 minutes by default)
424
+ # :size - the number of documents per shard to fetch per scroll
425
+ #
426
+ # Examples
427
+ #
428
+ # scroll = index.scroll('{"query":{"match_all":{}},"sort":{"date":"desc"}}')
429
+ # scroll.each_document do |document|
430
+ # document['_id']
431
+ # document['_source']
432
+ # end
433
+ #
434
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html
435
+ #
436
+ # Returns a new Scroller instance
437
+ def scroll( query, opts = {} )
438
+ opts = {:index => name}.merge opts
439
+ client.scroll query, opts
440
+ end
441
+
442
+ # Create a new Scroller instance for scanning all results from a `query`.
443
+ # The Scroller will be scoped to the current index. The Scroller is
444
+ # configured to use `scan` semantics which are more efficient than a
445
+ # standard scroll query; the caveat is that the returned documents cannot
446
+ # be sorted.
385
447
  #
386
448
  # query - The query to scan as a Hash or a JSON encoded String
387
449
  # opts - Options Hash
@@ -398,7 +460,9 @@ module Elastomer
398
460
  # document['_source']
399
461
  # end
400
462
  #
401
- # Returns a new Scan instance
463
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html
464
+ #
465
+ # Returns a new Scroller instance
402
466
  def scan( query, opts = {} )
403
467
  opts = {:index => name}.merge opts
404
468
  client.scan query, opts
@@ -427,6 +491,8 @@ module Elastomer
427
491
  # ...
428
492
  # end
429
493
  #
494
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html
495
+ #
430
496
  # Returns the response body as a Hash
431
497
  def multi_search( params = {}, &block )
432
498
  raise 'a block is required' if block.nil?
@@ -450,6 +516,8 @@ module Elastomer
450
516
  # index.warmer('warmer1').get
451
517
  # index.warmer('warmer1').delete
452
518
  #
519
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
520
+ #
453
521
  # Returns a new Warmer instance
454
522
  def warmer( warmer_name )
455
523
  client.warmer(name, warmer_name)
@@ -44,7 +44,7 @@ module Elastomer
44
44
  response.body
45
45
  end
46
46
  end
47
- alias :msearch :multi_search
47
+ alias_method :msearch, :multi_search
48
48
 
49
49
  # The MultiSearch class is a helper for accumulating and submitting
50
50
  # multi_search API requests. Instances of the MultiSearch class
@@ -2,12 +2,15 @@
2
2
  module Elastomer
3
3
  class Client
4
4
 
5
- # Provides access to node-level API commands.
5
+ # Provides access to node-level API commands. The default node is set to
6
+ # nil which target all nodes. You can pass in "_all" (to get the
7
+ # same effect) or "_local" to target only the current node the client is
8
+ # connected to. And you can specify an individual node or multiple nodes.
6
9
  #
7
10
  # node_id - The node ID as a String or an Array of node IDs
8
11
  #
9
12
  # Returns a Nodes instance.
10
- def nodes( node_id = '_all' )
13
+ def nodes( node_id = nil )
11
14
  Nodes.new self, node_id
12
15
  end
13
16
 
@@ -70,7 +73,9 @@ module Elastomer
70
73
  response.body
71
74
  end
72
75
 
73
- # Get the current hot threads on each node in the cluster.
76
+ # Get the current hot threads on each node in the cluster. The return
77
+ # value is a human formatted String - i.e. a String with newlines and
78
+ # other formatting characters suitable for display in a terminal window.
74
79
  #
75
80
  # params - Parameters Hash
76
81
  # :node_id - a single node ID or Array of node IDs
@@ -80,7 +85,7 @@ module Elastomer
80
85
  #
81
86
  # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-nodes-hot-threads.html
82
87
  #
83
- # Returns the response as a Hash
88
+ # Returns the response as a String
84
89
  def hot_threads( params = {} )
85
90
  response = client.get '/_nodes{/node_id}/hot_threads', update_params(params, :action => 'nodes.hot_threads')
86
91
  response.body