elastomer-client 0.7.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.overcommit.yml +5 -0
  3. data/.rubocop.yml +83 -0
  4. data/CHANGELOG.md +8 -0
  5. data/Gemfile +2 -2
  6. data/README.md +1 -1
  7. data/Rakefile +2 -2
  8. data/elastomer-client.gemspec +4 -2
  9. data/lib/elastomer/client.rb +42 -37
  10. data/lib/elastomer/client/bulk.rb +2 -2
  11. data/lib/elastomer/client/cluster.rb +19 -19
  12. data/lib/elastomer/client/delete_by_query.rb +7 -7
  13. data/lib/elastomer/client/docs.rb +81 -24
  14. data/lib/elastomer/client/errors.rb +2 -2
  15. data/lib/elastomer/client/index.rb +65 -29
  16. data/lib/elastomer/client/multi_percolate.rb +127 -0
  17. data/lib/elastomer/client/multi_search.rb +2 -2
  18. data/lib/elastomer/client/nodes.rb +4 -4
  19. data/lib/elastomer/client/percolator.rb +77 -0
  20. data/lib/elastomer/client/repository.rb +7 -7
  21. data/lib/elastomer/client/scroller.rb +14 -14
  22. data/lib/elastomer/client/snapshot.rb +9 -9
  23. data/lib/elastomer/client/template.rb +3 -3
  24. data/lib/elastomer/client/warmer.rb +5 -16
  25. data/lib/elastomer/core_ext/time.rb +1 -1
  26. data/lib/elastomer/middleware/encode_json.rb +5 -5
  27. data/lib/elastomer/middleware/opaque_id.rb +3 -3
  28. data/lib/elastomer/middleware/parse_json.rb +5 -5
  29. data/lib/elastomer/notifications.rb +4 -4
  30. data/lib/elastomer/version.rb +1 -1
  31. data/script/bootstrap +2 -0
  32. data/script/console +5 -5
  33. data/test/assertions.rb +26 -24
  34. data/test/client/bulk_test.rb +111 -111
  35. data/test/client/cluster_test.rb +58 -58
  36. data/test/client/delete_by_query_test.rb +53 -53
  37. data/test/client/docs_test.rb +279 -203
  38. data/test/client/errors_test.rb +1 -1
  39. data/test/client/index_test.rb +143 -109
  40. data/test/client/multi_percolate_test.rb +130 -0
  41. data/test/client/multi_search_test.rb +30 -28
  42. data/test/client/nodes_test.rb +30 -29
  43. data/test/client/percolator_test.rb +52 -0
  44. data/test/client/repository_test.rb +23 -23
  45. data/test/client/scroller_test.rb +40 -40
  46. data/test/client/snapshot_test.rb +15 -15
  47. data/test/client/stubbed_client_test.rb +15 -15
  48. data/test/client/template_test.rb +10 -10
  49. data/test/client/warmer_test.rb +18 -18
  50. data/test/client_test.rb +53 -53
  51. data/test/core_ext/time_test.rb +12 -12
  52. data/test/middleware/encode_json_test.rb +20 -20
  53. data/test/middleware/opaque_id_test.rb +10 -10
  54. data/test/middleware/parse_json_test.rb +14 -14
  55. data/test/notifications_test.rb +32 -32
  56. data/test/test_helper.rb +24 -24
  57. metadata +38 -2
@@ -1,4 +1,4 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  describe Elastomer::Client::Error do
4
4
 
@@ -1,9 +1,9 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  describe Elastomer::Client::Index do
4
4
 
5
5
  before do
6
- @name = 'elastomer-index-test'
6
+ @name = "elastomer-index-test"
7
7
  @index = $client.index @name
8
8
  @index.delete if @index.exists?
9
9
  end
@@ -12,28 +12,28 @@ describe Elastomer::Client::Index do
12
12
  @index.delete if @index.exists?
13
13
  end
14
14
 
15
- it 'does not require an index name' do
15
+ it "does not require an index name" do
16
16
  index = $client.index
17
17
  assert_nil index.name
18
18
  end
19
19
 
20
- it 'determines if an index exists' do
21
- assert !@index.exists?, 'the index should not yet exist'
20
+ it "determines if an index exists" do
21
+ assert !@index.exists?, "the index should not yet exist"
22
22
  end
23
23
 
24
- it 'determines if an index exists with .exist?' do
25
- assert !@index.exist?, 'the index should not yet exist'
24
+ it "determines if an index exists with .exist?" do
25
+ assert !@index.exist?, "the index should not yet exist"
26
26
  end
27
27
 
28
- describe 'when creating an index' do
29
- it 'creates an index' do
28
+ describe "when creating an index" do
29
+ it "creates an index" do
30
30
  @index.create({})
31
- assert @index.exists?, 'the index should now exist'
31
+ assert @index.exists?, "the index should now exist"
32
32
  end
33
33
 
34
- it 'creates an index with settings' do
34
+ it "creates an index with settings" do
35
35
  @index.create :settings => { :number_of_shards => 3, :number_of_replicas => 0 }
36
- settings = @index.get_settings[@name]['settings']
36
+ settings = @index.get_settings[@name]["settings"]
37
37
 
38
38
  # COMPATIBILITY
39
39
  # ES 1.0 changed the default return format of index settings to always
@@ -42,17 +42,17 @@ describe Elastomer::Client::Index do
42
42
  # {"index": {"number_of_replicas":"1"}}
43
43
 
44
44
  # To support both versions, we check for either return format.
45
- value = settings['index.number_of_shards'] ||
46
- settings['index']['number_of_shards']
47
- assert_equal '3', value
48
- value = settings['index.number_of_replicas'] ||
49
- settings['index']['number_of_replicas']
50
- assert_equal '0', value
45
+ value = settings["index.number_of_shards"] ||
46
+ settings["index"]["number_of_shards"]
47
+ assert_equal "3", value
48
+ value = settings["index.number_of_replicas"] ||
49
+ settings["index"]["number_of_replicas"]
50
+ assert_equal "0", value
51
51
  end
52
52
 
53
- it 'creates an index with settings with .settings' do
53
+ it "creates an index with settings with .settings" do
54
54
  @index.create :settings => { :number_of_shards => 3, :number_of_replicas => 0 }
55
- settings = @index.settings[@name]['settings']
55
+ settings = @index.settings[@name]["settings"]
56
56
 
57
57
  # COMPATIBILITY
58
58
  # ES 1.0 changed the default return format of index settings to always
@@ -61,15 +61,15 @@ describe Elastomer::Client::Index do
61
61
  # {"index": {"number_of_replicas":"1"}}
62
62
 
63
63
  # To support both versions, we check for either return format.
64
- value = settings['index.number_of_shards'] ||
65
- settings['index']['number_of_shards']
66
- assert_equal '3', value
67
- value = settings['index.number_of_replicas'] ||
68
- settings['index']['number_of_replicas']
69
- assert_equal '0', value
64
+ value = settings["index.number_of_shards"] ||
65
+ settings["index"]["number_of_shards"]
66
+ assert_equal "3", value
67
+ value = settings["index.number_of_replicas"] ||
68
+ settings["index"]["number_of_replicas"]
69
+ assert_equal "0", value
70
70
  end
71
71
 
72
- it 'adds mappings for document types' do
72
+ it "adds mappings for document types" do
73
73
  @index.create(
74
74
  :settings => { :number_of_shards => 1, :number_of_replicas => 0 },
75
75
  :mappings => {
@@ -77,18 +77,18 @@ describe Elastomer::Client::Index do
77
77
  :_source => { :enabled => false },
78
78
  :_all => { :enabled => false },
79
79
  :properties => {
80
- :title => { :type => 'string', :analyzer => 'standard' },
81
- :author => { :type => 'string', :index => 'not_analyzed' }
80
+ :title => { :type => "string", :analyzer => "standard" },
81
+ :author => { :type => "string", :index => "not_analyzed" }
82
82
  }
83
83
  }
84
84
  }
85
85
  )
86
86
 
87
- assert @index.exists?, 'the index should now exist'
88
- assert_mapping_exists @index.get_mapping[@name], 'doco'
87
+ assert @index.exists?, "the index should now exist"
88
+ assert_mapping_exists @index.get_mapping[@name], "doco"
89
89
  end
90
90
 
91
- it 'adds mappings for document types with .mapping' do
91
+ it "adds mappings for document types with .mapping" do
92
92
  @index.create(
93
93
  :settings => { :number_of_shards => 1, :number_of_replicas => 0 },
94
94
  :mappings => {
@@ -96,23 +96,23 @@ describe Elastomer::Client::Index do
96
96
  :_source => { :enabled => false },
97
97
  :_all => { :enabled => false },
98
98
  :properties => {
99
- :title => { :type => 'string', :analyzer => 'standard' },
100
- :author => { :type => 'string', :index => 'not_analyzed' }
99
+ :title => { :type => "string", :analyzer => "standard" },
100
+ :author => { :type => "string", :index => "not_analyzed" }
101
101
  }
102
102
  }
103
103
  }
104
104
  )
105
105
 
106
- assert @index.exists?, 'the index should now exist'
107
- assert_mapping_exists @index.mapping[@name], 'doco'
106
+ assert @index.exists?, "the index should now exist"
107
+ assert_mapping_exists @index.mapping[@name], "doco"
108
108
  end
109
109
  end
110
110
 
111
- it 'updates index settings' do
111
+ it "updates index settings" do
112
112
  @index.create :settings => { :number_of_shards => 1, :number_of_replicas => 0 }
113
113
 
114
- @index.update_settings 'index.number_of_replicas' => 1
115
- settings = @index.settings[@name]['settings']
114
+ @index.update_settings "index.number_of_replicas" => 1
115
+ settings = @index.settings[@name]["settings"]
116
116
 
117
117
  # COMPATIBILITY
118
118
  # ES 1.0 changed the default return format of index settings to always
@@ -121,12 +121,12 @@ describe Elastomer::Client::Index do
121
121
  # {"index": {"number_of_replicas":"1"}}
122
122
 
123
123
  # To support both versions, we check for either return format.
124
- value = settings['index.number_of_replicas'] ||
125
- settings['index']['number_of_replicas']
126
- assert_equal '1', value
124
+ value = settings["index.number_of_replicas"] ||
125
+ settings["index"]["number_of_replicas"]
126
+ assert_equal "1", value
127
127
  end
128
128
 
129
- it 'updates document mappings' do
129
+ it "updates document mappings" do
130
130
  unless es_version_supports_update_mapping_with__all_disabled?
131
131
  skip "Mapping Update API is broken in this ES version."
132
132
  end
@@ -136,28 +136,28 @@ describe Elastomer::Client::Index do
136
136
  :doco => {
137
137
  :_source => { :enabled => false },
138
138
  :_all => { :enabled => false },
139
- :properties => {:title => { :type => 'string', :analyzer => 'standard' }}
139
+ :properties => {:title => { :type => "string", :analyzer => "standard" }}
140
140
  }
141
141
  }
142
142
  )
143
143
 
144
- assert_property_exists @index.mapping[@name], 'doco', 'title'
144
+ assert_property_exists @index.mapping[@name], "doco", "title"
145
145
 
146
- @index.update_mapping 'doco', { :doco => { :properties => {
147
- :author => { :type => 'string', :index => 'not_analyzed' }
146
+ @index.update_mapping "doco", { :doco => { :properties => {
147
+ :author => { :type => "string", :index => "not_analyzed" }
148
148
  }}}
149
149
 
150
- assert_property_exists @index.mapping[@name], 'doco', 'author'
151
- assert_property_exists @index.mapping[@name], 'doco', 'title'
150
+ assert_property_exists @index.mapping[@name], "doco", "author"
151
+ assert_property_exists @index.mapping[@name], "doco", "title"
152
152
 
153
- @index.update_mapping 'mux_mool', { :mux_mool => { :properties => {
154
- :song => { :type => 'string', :index => 'not_analyzed' }
153
+ @index.update_mapping "mux_mool", { :mux_mool => { :properties => {
154
+ :song => { :type => "string", :index => "not_analyzed" }
155
155
  }}}
156
156
 
157
- assert_property_exists @index.mapping[@name], 'mux_mool', 'song'
157
+ assert_property_exists @index.mapping[@name], "mux_mool", "song"
158
158
  end
159
159
 
160
- it 'updates document mappings with .put_mapping' do
160
+ it "updates document mappings with .put_mapping" do
161
161
  unless es_version_supports_update_mapping_with__all_disabled?
162
162
  skip "Mapping Update API is broken in this ES version."
163
163
  end
@@ -167,40 +167,40 @@ describe Elastomer::Client::Index do
167
167
  :doco => {
168
168
  :_source => { :enabled => false },
169
169
  :_all => { :enabled => false },
170
- :properties => {:title => { :type => 'string', :analyzer => 'standard' }}
170
+ :properties => {:title => { :type => "string", :analyzer => "standard" }}
171
171
  }
172
172
  }
173
173
  )
174
174
 
175
- assert_property_exists @index.mapping[@name], 'doco', 'title'
175
+ assert_property_exists @index.mapping[@name], "doco", "title"
176
176
 
177
- @index.put_mapping 'doco', { :doco => { :properties => {
178
- :author => { :type => 'string', :index => 'not_analyzed' }
177
+ @index.put_mapping "doco", { :doco => { :properties => {
178
+ :author => { :type => "string", :index => "not_analyzed" }
179
179
  }}}
180
180
 
181
- assert_property_exists @index.mapping[@name], 'doco', 'author'
182
- assert_property_exists @index.mapping[@name], 'doco', 'title'
181
+ assert_property_exists @index.mapping[@name], "doco", "author"
182
+ assert_property_exists @index.mapping[@name], "doco", "title"
183
183
 
184
- @index.put_mapping 'mux_mool', { :mux_mool => { :properties => {
185
- :song => { :type => 'string', :index => 'not_analyzed' }
184
+ @index.put_mapping "mux_mool", { :mux_mool => { :properties => {
185
+ :song => { :type => "string", :index => "not_analyzed" }
186
186
  }}}
187
187
 
188
- assert_property_exists @index.mapping[@name], 'mux_mool', 'song'
188
+ assert_property_exists @index.mapping[@name], "mux_mool", "song"
189
189
  end
190
190
 
191
- it 'deletes document mappings' do
191
+ it "deletes document mappings" do
192
192
  @index.create(
193
193
  :mappings => {
194
194
  :doco => {
195
195
  :_source => { :enabled => false },
196
196
  :_all => { :enabled => false },
197
- :properties => {:title => { :type => 'string', :analyzer => 'standard' }}
197
+ :properties => {:title => { :type => "string", :analyzer => "standard" }}
198
198
  }
199
199
  }
200
200
  )
201
- assert_mapping_exists @index.mapping[@name], 'doco'
201
+ assert_mapping_exists @index.mapping[@name], "doco"
202
202
 
203
- response = @index.delete_mapping 'doco'
203
+ response = @index.delete_mapping "doco"
204
204
  assert_acknowledged response
205
205
 
206
206
  mapping = @index.get_mapping
@@ -210,26 +210,26 @@ describe Elastomer::Client::Index do
210
210
  assert_empty mapping, "no mappings are present"
211
211
  end
212
212
 
213
- it 'lists all aliases to the index' do
213
+ it "lists all aliases to the index" do
214
214
  @index.create(nil)
215
215
 
216
216
  if es_version_always_returns_aliases?
217
- assert_equal({@name => {'aliases' => {}}}, @index.get_aliases)
217
+ assert_equal({@name => {"aliases" => {}}}, @index.get_aliases)
218
218
  else
219
219
  assert_equal({@name => {}}, @index.get_aliases)
220
220
  end
221
221
 
222
- $client.cluster.update_aliases :add => {:index => @name, :alias => 'foofaloo'}
223
- assert_equal({@name => {'aliases' => {'foofaloo' => {}}}}, @index.get_aliases)
222
+ $client.cluster.update_aliases :add => {:index => @name, :alias => "foofaloo"}
223
+ assert_equal({@name => {"aliases" => {"foofaloo" => {}}}}, @index.get_aliases)
224
224
 
225
225
  if es_version_1_x?
226
- assert_equal({@name => {'aliases' => {'foofaloo' => {}}}}, @index.get_alias("f*"))
226
+ assert_equal({@name => {"aliases" => {"foofaloo" => {}}}}, @index.get_alias("f*"))
227
227
  assert_equal({}, @index.get_alias("r*"))
228
228
  end
229
229
  end
230
230
 
231
231
  if es_version_1_x?
232
- it 'adds and deletes aliases to the index' do
232
+ it "adds and deletes aliases to the index" do
233
233
  @index.create(nil)
234
234
  assert_empty @index.get_alias("*")
235
235
 
@@ -249,9 +249,9 @@ describe Elastomer::Client::Index do
249
249
  # COMPATIBILITY ES 1.x removed English stopwords from the default analyzers,
250
250
  # so create a custom one with the English stopwords added.
251
251
  if es_version_1_x?
252
- it 'analyzes text and returns tokens' do
253
- tokens = @index.analyze 'Just a few words to analyze.', :analyzer => 'standard', :index => nil
254
- tokens = tokens['tokens'].map { |h| h['token'] }
252
+ it "analyzes text and returns tokens" do
253
+ tokens = @index.analyze "Just a few words to analyze.", :analyzer => "standard", :index => nil
254
+ tokens = tokens["tokens"].map { |h| h["token"] }
255
255
  assert_equal %w[just a few words to analyze], tokens
256
256
 
257
257
  @index.create(
@@ -270,18 +270,18 @@ describe Elastomer::Client::Index do
270
270
  )
271
271
  wait_for_index(@name)
272
272
 
273
- tokens = @index.analyze 'Just a few words to analyze.', :analyzer => 'english_standard'
274
- tokens = tokens['tokens'].map { |h| h['token'] }
273
+ tokens = @index.analyze "Just a few words to analyze.", :analyzer => "english_standard"
274
+ tokens = tokens["tokens"].map { |h| h["token"] }
275
275
  assert_equal %w[just few words analyze], tokens
276
276
  end
277
277
  else
278
- it 'analyzes text and returns tokens' do
279
- tokens = @index.analyze 'Just a few words to analyze.', :index => nil
280
- tokens = tokens['tokens'].map { |h| h['token'] }
278
+ it "analyzes text and returns tokens" do
279
+ tokens = @index.analyze "Just a few words to analyze.", :index => nil
280
+ tokens = tokens["tokens"].map { |h| h["token"] }
281
281
  assert_equal %w[just few words analyze], tokens
282
282
 
283
- tokens = @index.analyze 'Just a few words to analyze.', :analyzer => 'simple', :index => nil
284
- tokens = tokens['tokens'].map { |h| h['token'] }
283
+ tokens = @index.analyze "Just a few words to analyze.", :analyzer => "simple", :index => nil
284
+ tokens = tokens["tokens"].map { |h| h["token"] }
285
285
  assert_equal %w[just a few words to analyze], tokens
286
286
  end
287
287
  end
@@ -293,94 +293,128 @@ describe Elastomer::Client::Index do
293
293
  end
294
294
 
295
295
  #TODO assert this only hits the desired index
296
- it 'deletes' do
296
+ it "deletes" do
297
297
  response = @index.delete
298
298
  assert_acknowledged response
299
299
  end
300
300
 
301
- it 'opens' do
301
+ it "opens" do
302
302
  response = @index.open
303
303
  assert_acknowledged response
304
304
  end
305
305
 
306
- it 'closes' do
306
+ it "closes" do
307
307
  response = @index.close
308
308
  assert_acknowledged response
309
309
  end
310
310
 
311
- it 'refreshes' do
311
+ it "refreshes" do
312
312
  response = @index.refresh
313
313
  assert_equal 0, response["_shards"]["failed"]
314
314
  end
315
315
 
316
- it 'flushes' do
316
+ it "flushes" do
317
317
  response = @index.flush
318
318
  assert_equal 0, response["_shards"]["failed"]
319
319
  end
320
320
 
321
- it 'optimizes' do
321
+ it "optimizes" do
322
322
  response = @index.optimize
323
323
  assert_equal 0, response["_shards"]["failed"]
324
324
  end
325
325
 
326
326
  # COMPATIBILITY ES 1.2 removed support for the gateway snapshot API.
327
327
  if es_version_supports_gateway_snapshots?
328
- it 'snapshots' do
328
+ it "snapshots" do
329
329
  response = @index.snapshot
330
330
  assert_equal 0, response["_shards"]["failed"]
331
331
  end
332
332
  end
333
333
 
334
334
  if es_version_1_x?
335
- it 'recovery' do
335
+ it "recovery" do
336
336
  response = @index.recovery
337
337
  assert_includes response, "elastomer-index-test"
338
338
  end
339
339
  end
340
340
 
341
- it 'clears caches' do
341
+ it "clears caches" do
342
342
  response = @index.clear_cache
343
343
  assert_equal 0, response["_shards"]["failed"]
344
344
  end
345
345
 
346
- it 'gets stats' do
346
+ it "gets stats" do
347
347
  response = @index.stats
348
- if response.key? 'indices'
348
+ if response.key? "indices"
349
349
  assert_includes response["indices"], "elastomer-index-test"
350
350
  else
351
351
  assert_includes response["_all"]["indices"], "elastomer-index-test"
352
352
  end
353
353
  end
354
354
 
355
- it 'gets status' do
355
+ it "gets status" do
356
356
  response = @index.status
357
357
  assert_includes response["indices"], "elastomer-index-test"
358
358
  end
359
359
 
360
- it 'gets segments' do
361
- @index.docs('foo').index("foo" => "bar")
360
+ it "gets segments" do
361
+ @index.docs("foo").index("foo" => "bar")
362
362
  response = @index.segments
363
363
  assert_includes response["indices"], "elastomer-index-test"
364
364
  end
365
365
 
366
- it 'deletes by query' do
367
- @index.docs('foo').index("foo" => "bar")
366
+ it "deletes by query" do
367
+ @index.docs("foo").index("foo" => "bar")
368
368
  @index.refresh
369
- r = @index.delete_by_query(:q => '*')
369
+ r = @index.delete_by_query(:q => "*")
370
370
  assert_equal({
371
- '_all' => {
372
- 'found' => 1,
373
- 'deleted' => 1,
374
- 'missing' => 0,
375
- 'failed' => 0,
371
+ "_all" => {
372
+ "found" => 1,
373
+ "deleted" => 1,
374
+ "missing" => 0,
375
+ "failed" => 0,
376
376
  },
377
377
  @name => {
378
- 'found' => 1,
379
- 'deleted' => 1,
380
- 'missing' => 0,
381
- 'failed' => 0,
378
+ "found" => 1,
379
+ "deleted" => 1,
380
+ "missing" => 0,
381
+ "failed" => 0,
382
382
  }
383
- }, r['_indices'])
383
+ }, r["_indices"])
384
+ end
385
+
386
+ it "creates a Percolator" do
387
+ id = "1"
388
+ percolator = @index.percolator id
389
+ assert_equal id, percolator.id
390
+ end
391
+
392
+ it "performs multi percolate queries" do
393
+ @index.docs.index \
394
+ :_id => 1,
395
+ :_type => "doc2",
396
+ :title => "the author of logging",
397
+ :author => "pea53"
398
+
399
+ @index.docs.index \
400
+ :_id => 2,
401
+ :_type => "doc2",
402
+ :title => "the author of rubber-band",
403
+ :author => "grantr"
404
+
405
+ @index.percolator("1").create :query => { :match_all => { } }
406
+ @index.percolator("2").create :query => { :match => { :author => "pea53" } }
407
+
408
+ h = @index.multi_percolate(:type => "doc2") do |m|
409
+ m.percolate :author => "pea53"
410
+ m.percolate :author => "grantr"
411
+ m.count({}, { :author => "grantr" })
412
+ end
413
+
414
+ response1, response2, response3 = h["responses"]
415
+ assert_equal ["1", "2"], response1["matches"].map { |match| match["_id"] }.sort
416
+ assert_equal ["1"], response2["matches"].map { |match| match["_id"] }.sort
417
+ assert_equal 1, response3["total"]
384
418
  end
385
419
  end
386
420
  end