elastomer-client 2.2.0 → 2.3.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.
@@ -28,11 +28,11 @@ module Elastomer
28
28
  def exists?(params = {})
29
29
  response = client.get "/_snapshot{/repository}", update_params(params, :action => "repository.exists")
30
30
  response.success?
31
- rescue Elastomer::Client::Error => exception
32
- if exception.message =~ /RepositoryMissingException/
31
+ rescue Elastomer::Client::Error => err
32
+ if err.error && err.error.dig("root_cause", 0, "type") == "repository_missing_exception"
33
33
  false
34
34
  else
35
- raise exception
35
+ raise err
36
36
  end
37
37
  end
38
38
  alias_method :exist?, :exists?
@@ -102,8 +102,7 @@ module Elastomer
102
102
  response = get "/_search/scroll", :body => scroll_id, :scroll => scroll, :action => "search.scroll"
103
103
  response.body
104
104
  rescue RequestError => err
105
- if err.error && err.error["caused_by"]["type"] == "search_context_missing_exception" \
106
- || err.message =~ /SearchContextMissingException/
105
+ if err.error && err.error["caused_by"]["type"] == "search_context_missing_exception"
107
106
  raise SearchContextMissing, "No search context found for scroll ID #{scroll_id.inspect}"
108
107
  else
109
108
  raise err
@@ -37,11 +37,11 @@ module Elastomer
37
37
  def exists?(params = {})
38
38
  response = client.get "/_snapshot/{repository}/{snapshot}", update_params(params, :action => "snapshot.exists")
39
39
  response.success?
40
- rescue Elastomer::Client::Error => exception
41
- if exception.message =~ /SnapshotMissingException/
40
+ rescue Elastomer::Client::Error => err
41
+ if err.error && err.error.dig("root_cause", 0, "type") == "snapshot_missing_exception"
42
42
  false
43
43
  else
44
- raise exception
44
+ raise err
45
45
  end
46
46
  end
47
47
  alias_method :exist?, :exists?
@@ -23,8 +23,8 @@ module Elastomer
23
23
  end
24
24
 
25
25
  def match_content_type(env)
26
+ add_content_type!(env)
26
27
  if process_request?(env)
27
- env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
28
28
  yield env[:body] unless env[:body].respond_to?(:to_str)
29
29
  end
30
30
  end
@@ -43,6 +43,13 @@ module Elastomer
43
43
  type = type.split(";", 2).first if type.index(";")
44
44
  type
45
45
  end
46
+
47
+ def add_content_type!(env)
48
+ method = env[:method]
49
+ if method == :put || method == :post || has_body?(env)
50
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
51
+ end
52
+ end
46
53
  end
47
54
  end
48
55
  end
@@ -1,5 +1,5 @@
1
1
  module Elastomer
2
- VERSION = "2.2.0"
2
+ VERSION = "2.3.0"
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -14,10 +14,14 @@ echo
14
14
 
15
15
  result=0
16
16
 
17
- export RBENV_VERSION="2.1.1-github"
17
+ export RBENV_VERSION="2.3.3"
18
+ if [ -d /usr/share/rbenv/shims ]; then
19
+ export PATH=/usr/share/rbenv/shims:$PATH
20
+ fi
18
21
  ruby -v
22
+ rm -f Gemfile.lock
19
23
  script/bootstrap
20
- script/test || result=$?
24
+ bundle exec rake test || result=$?
21
25
 
22
26
  if [ $result -ne 0 ]; then
23
27
  exit $result
@@ -87,10 +87,20 @@ describe Elastomer::Client::Bulk do
87
87
 
88
88
  assert_kind_of Integer, h["took"]
89
89
 
90
- assert_bulk_index h["items"].first
91
- assert_bulk_create h["items"].last
90
+ assert_equal 2, h["items"].length
91
+
92
+ if es_version_2_x?
93
+ assert_bulk_index h["items"].first
94
+ assert_bulk_create h["items"].last
95
+ book_id = items.last["create"]["_id"]
96
+ elsif es_version_5_x?
97
+ assert_bulk_index h["items"].first
98
+ assert_bulk_index h["items"].last
99
+ book_id = items.last["index"]["_id"]
100
+ else
101
+ fail "Unknown ES version!"
102
+ end
92
103
 
93
- book_id = items.last["create"]["_id"]
94
104
  assert_match %r/^\S{20,22}$/, book_id
95
105
 
96
106
  @index.refresh
@@ -108,10 +118,22 @@ describe Elastomer::Client::Bulk do
108
118
  end
109
119
  items = h["items"]
110
120
 
111
- assert_bulk_create h["items"].first, "expected to create a book"
112
- assert_bulk_delete h["items"].last, "expected to delete a book"
121
+ assert_equal 2, h["items"].length
122
+
123
+ if es_version_2_x?
124
+ assert_bulk_create h["items"].first, "expected to create a book"
125
+ assert_bulk_delete h["items"].last, "expected to delete a book"
126
+
127
+ book_id2 = items.first["create"]["_id"]
128
+ elsif es_version_5_x?
129
+ assert_bulk_index h["items"].first, "expected to create a book"
130
+ assert_bulk_delete h["items"].last, "expected to delete a book"
131
+
132
+ book_id2 = items.first["index"]["_id"]
133
+ else
134
+ fail "Unknown ES version!"
135
+ end
113
136
 
114
- book_id2 = items.first["create"]["_id"]
115
137
  assert_match %r/^\S{20,22}$/, book_id2
116
138
 
117
139
  @index.refresh
@@ -27,19 +27,17 @@ describe Elastomer::Client::Cluster do
27
27
  assert_instance_of Hash, h["metadata"], "the metadata are returned"
28
28
  end
29
29
 
30
- if es_version_1_x?
31
- it "filters cluster state by metrics" do
32
- h = @cluster.state(:metrics => "nodes")
33
- refute h.key("metadata"), "expected only nodes state"
34
- h = @cluster.state(:metrics => "metadata")
35
- refute h.key("nodes"), "expected only metadata state"
36
- end
30
+ it "filters cluster state by metrics" do
31
+ h = @cluster.state(metrics: "nodes")
32
+ refute h.key("metadata"), "expected only nodes state"
33
+ h = @cluster.state(metrics: "metadata")
34
+ refute h.key("nodes"), "expected only metadata state"
35
+ end
37
36
 
38
- it "filters cluster state by indices" do
39
- @index.create({}) unless @index.exists?
40
- h = @cluster.state(:metrics => "metadata", :indices => @name)
41
- assert [@name], h["metadata"]["indices"].keys
42
- end
37
+ it "filters cluster state by indices" do
38
+ @index.create(default_index_settings) unless @index.exists?
39
+ h = @cluster.state(metrics: "metadata", indices: @name)
40
+ assert [@name], h["metadata"]["indices"].keys
43
41
  end
44
42
 
45
43
  it "gets the cluster settings" do
@@ -55,13 +53,13 @@ describe Elastomer::Client::Cluster do
55
53
  end
56
54
 
57
55
  it "updates the cluster settings" do
58
- @cluster.update_settings :transient => { "indices.ttl.interval" => "30m" }
56
+ @cluster.update_settings transient: { "indices.ttl.interval" => "30m" }
59
57
  h = @cluster.settings
60
58
 
61
59
  value = h["transient"]["indices"]["ttl"]["interval"]
62
60
  assert_equal "30m", value
63
61
 
64
- @cluster.update_settings :transient => { "indices.ttl.interval" => "60m" }
62
+ @cluster.update_settings transient: { "indices.ttl.interval" => "60m" }
65
63
  h = @cluster.settings
66
64
 
67
65
  value = h["transient"]["indices"]["ttl"]["interval"]
@@ -70,7 +68,9 @@ describe Elastomer::Client::Cluster do
70
68
 
71
69
  it "returns cluster stats" do
72
70
  h = @cluster.stats
73
- assert_equal %w[cluster_name indices nodes status timestamp], h.keys.sort
71
+ expected = %w[cluster_name indices nodes status timestamp]
72
+ expected.unshift("_nodes") if es_version_5_x?
73
+ assert_equal expected, h.keys.sort
74
74
  end
75
75
 
76
76
  it "returns a list of pending tasks" do
@@ -80,7 +80,7 @@ describe Elastomer::Client::Cluster do
80
80
  end
81
81
 
82
82
  it "returns the list of indices in the cluster" do
83
- @index.create({}) unless @index.exists?
83
+ @index.create(default_index_settings) unless @index.exists?
84
84
  indices = @cluster.indices
85
85
  assert !indices.empty?, "expected to see an index"
86
86
  end
@@ -94,7 +94,7 @@ describe Elastomer::Client::Cluster do
94
94
  before do
95
95
  @name = "elastomer-cluster-test"
96
96
  @index = $client.index @name
97
- @index.create({}) unless @index.exists?
97
+ @index.create(default_index_settings) unless @index.exists?
98
98
  wait_for_index(@name)
99
99
  end
100
100
 
@@ -104,12 +104,10 @@ describe Elastomer::Client::Cluster do
104
104
 
105
105
  it "adds and gets an alias" do
106
106
  hash = @cluster.get_aliases
107
- if es_version_always_returns_aliases?
108
- assert_empty hash[@name]["aliases"]
109
- end
107
+ assert_empty hash[@name]["aliases"]
110
108
 
111
109
  @cluster.update_aliases \
112
- :add => {:index => @name, :alias => "elastomer-test-unikitty"}
110
+ add: {index: @name, alias: "elastomer-test-unikitty"}
113
111
 
114
112
  hash = @cluster.get_aliases
115
113
  assert_equal ["elastomer-test-unikitty"], hash[@name]["aliases"].keys
@@ -117,12 +115,10 @@ describe Elastomer::Client::Cluster do
117
115
 
118
116
  it "adds and gets an alias with .aliases" do
119
117
  hash = @cluster.aliases
120
- if es_version_always_returns_aliases?
121
- assert_empty hash[@name]["aliases"]
122
- end
118
+ assert_empty hash[@name]["aliases"]
123
119
 
124
120
  @cluster.update_aliases \
125
- :add => {:index => @name, :alias => "elastomer-test-unikitty"}
121
+ add: {index: @name, alias: "elastomer-test-unikitty"}
126
122
 
127
123
  hash = @cluster.aliases
128
124
  assert_equal ["elastomer-test-unikitty"], hash[@name]["aliases"].keys
@@ -130,14 +126,14 @@ describe Elastomer::Client::Cluster do
130
126
 
131
127
  it "removes an alias" do
132
128
  @cluster.update_aliases \
133
- :add => {:index => @name, :alias => "elastomer-test-unikitty"}
129
+ add: {index: @name, alias: "elastomer-test-unikitty"}
134
130
 
135
131
  hash = @cluster.get_aliases
136
132
  assert_equal ["elastomer-test-unikitty"], hash[@name]["aliases"].keys
137
133
 
138
134
  @cluster.update_aliases([
139
- {:add => {:index => @name, :alias => "elastomer-test-SpongeBob-SquarePants"}},
140
- {:remove => {:index => @name, :alias => "elastomer-test-unikitty"}}
135
+ {add: {index: @name, alias: "elastomer-test-SpongeBob-SquarePants"}},
136
+ {remove: {index: @name, alias: "elastomer-test-unikitty"}}
141
137
  ])
142
138
 
143
139
  hash = @cluster.get_aliases
@@ -145,12 +141,12 @@ describe Elastomer::Client::Cluster do
145
141
  end
146
142
 
147
143
  it "accepts the full aliases actions hash" do
148
- @cluster.update_aliases :actions => [
149
- {:add => {:index => @name, :alias => "elastomer-test-He-Man"}},
150
- {:add => {:index => @name, :alias => "elastomer-test-Skeletor"}}
144
+ @cluster.update_aliases actions: [
145
+ {add: {index: @name, alias: "elastomer-test-He-Man"}},
146
+ {add: {index: @name, alias: "elastomer-test-Skeletor"}}
151
147
  ]
152
148
 
153
- hash = @cluster.get_aliases(:index => @name)
149
+ hash = @cluster.get_aliases(index: @name)
154
150
  assert_equal %w[elastomer-test-He-Man elastomer-test-Skeletor], hash[@name]["aliases"].keys.sort
155
151
  end
156
152
  end
@@ -270,13 +270,11 @@ describe Elastomer::Client::Docs do
270
270
  end
271
271
 
272
272
  it "supports the shards search API" do
273
- if es_version_supports_search_shards?
274
- h = @docs.search_shards(:type => "docs1")
273
+ h = @docs.search_shards(:type => "docs1")
275
274
 
276
- assert h.key?("nodes"), "response contains \"nodes\" information"
277
- assert h.key?("shards"), "response contains \"shards\" information"
278
- assert h["shards"].is_a?(Array), "\"shards\" is an array"
279
- end
275
+ assert h.key?("nodes"), "response contains \"nodes\" information"
276
+ assert h.key?("shards"), "response contains \"shards\" information"
277
+ assert h["shards"].is_a?(Array), "\"shards\" is an array"
280
278
  end
281
279
 
282
280
  it "generates QueryParsingError exceptions on bad input when searching" do
@@ -313,48 +311,6 @@ describe Elastomer::Client::Docs do
313
311
  assert_equal 1, h["count"]
314
312
  end
315
313
 
316
- # The /_mlt endpoint has been removed from ES 2.X
317
- if es_version_1_x?
318
- it "searches for more like this" do
319
- populate!
320
-
321
- # for some reason, if there's no document indexed here all the mlt
322
- # queries return zero results
323
- @docs.index \
324
- :_id => 3,
325
- :_type => "doc1",
326
- :title => "the author of faraday",
327
- :author => "technoweenie"
328
-
329
- @index.refresh
330
-
331
- h = @docs.more_like_this({
332
- :type => "doc1",
333
- :id => 1,
334
- :mlt_fields => "title",
335
- :min_term_freq => 1
336
- })
337
- assert_equal 2, h["hits"]["total"]
338
-
339
- h = @docs.more_like_this({
340
- :facets => {
341
- "author" => {
342
- :terms => {
343
- :field => "author"
344
- }
345
- }
346
- }
347
- }, {
348
- :type => "doc1",
349
- :id => 1,
350
- :mlt_fields => "title,author",
351
- :min_term_freq => 1
352
- })
353
- assert_equal 2, h["hits"]["total"]
354
- assert_equal 2, h["facets"]["author"]["total"]
355
- end
356
- end
357
-
358
314
  it "explains scoring" do
359
315
  populate!
360
316
 
@@ -427,7 +383,7 @@ describe Elastomer::Client::Docs do
427
383
  populate!(b)
428
384
  end
429
385
 
430
- assert_instance_of Fixnum, response["took"]
386
+ assert_kind_of Integer, response["took"]
431
387
 
432
388
  response = @docs.get(:id => 1, :type => "doc1")
433
389
  assert_found response
@@ -105,10 +105,6 @@ describe Elastomer::Client::Index do
105
105
  end
106
106
 
107
107
  it "updates document mappings" do
108
- unless es_version_supports_update_mapping_with__all_disabled?
109
- skip "Mapping Update API is broken in this ES version."
110
- end
111
-
112
108
  @index.create(
113
109
  :mappings => {
114
110
  :doco => {
@@ -136,10 +132,6 @@ describe Elastomer::Client::Index do
136
132
  end
137
133
 
138
134
  it "updates document mappings with .put_mapping" do
139
- unless es_version_supports_update_mapping_with__all_disabled?
140
- skip "Mapping Update API is broken in this ES version."
141
- end
142
-
143
135
  @index.create(
144
136
  :mappings => {
145
137
  :doco => {
@@ -166,71 +158,31 @@ describe Elastomer::Client::Index do
166
158
  assert_property_exists @index.mapping[@name], "mux_mool", "song"
167
159
  end
168
160
 
169
- it "deletes document mappings" do
170
- @index.create(
171
- :mappings => {
172
- :doco => {
173
- :_source => { :enabled => false },
174
- :_all => { :enabled => false },
175
- :properties => {:title => { :type => "string", :analyzer => "standard" }}
176
- }
177
- }
178
- )
179
- assert_mapping_exists @index.mapping[@name], "doco"
180
-
181
- response = @index.delete_mapping "doco"
182
-
183
- if es_version_1_x?
184
- assert_acknowledged response
185
-
186
- mapping = @index.get_mapping
187
- mapping = mapping[@name] if mapping.key? @name
188
- mapping = mapping["mappings"] if mapping.key? "mappings"
189
-
190
- assert_empty mapping, "no mappings are present"
191
-
192
- elsif es_version_2_x?
193
- assert_equal "No handler found for uri [/elastomer-index-test/doco] and method [DELETE]", response
194
-
195
- else
196
- assert false, "Unsupported Elasticsearch version #{$client.semantic_version}"
197
- end
198
- end
199
-
200
161
  it "lists all aliases to the index" do
201
162
  @index.create(nil)
202
-
203
- if es_version_always_returns_aliases?
204
- assert_equal({@name => {"aliases" => {}}}, @index.get_aliases)
205
- else
206
- assert_equal({@name => {}}, @index.get_aliases)
207
- end
163
+ assert_equal({@name => {"aliases" => {}}}, @index.get_aliases)
208
164
 
209
165
  $client.cluster.update_aliases :add => {:index => @name, :alias => "foofaloo"}
210
166
  assert_equal({@name => {"aliases" => {"foofaloo" => {}}}}, @index.get_aliases)
211
167
 
212
- if es_version_1_x?
213
- assert_equal({@name => {"aliases" => {"foofaloo" => {}}}}, @index.get_alias("f*"))
214
- assert_equal({}, @index.get_alias("r*"))
215
- end
168
+ assert_equal({@name => {"aliases" => {"foofaloo" => {}}}}, @index.get_alias("f*"))
169
+ assert_equal({}, @index.get_alias("r*"))
216
170
  end
217
171
 
218
- if es_version_1_x?
219
- it "adds and deletes aliases to the index" do
220
- @index.create(nil)
221
- assert_empty @index.get_alias("*")
172
+ it "adds and deletes aliases to the index" do
173
+ @index.create(nil)
174
+ assert_empty @index.get_alias("*")
222
175
 
223
- @index.add_alias "gondolin"
224
- aliases = @index.get_alias("*")
225
- assert_equal %w[gondolin], aliases[@name]["aliases"].keys.sort
176
+ @index.add_alias "gondolin"
177
+ aliases = @index.get_alias("*")
178
+ assert_equal %w[gondolin], aliases[@name]["aliases"].keys.sort
226
179
 
227
- @index.add_alias "gondor"
228
- aliases = @index.get_alias("*")
229
- assert_equal %w[gondolin gondor], aliases[@name]["aliases"].keys.sort
180
+ @index.add_alias "gondor"
181
+ aliases = @index.get_alias("*")
182
+ assert_equal %w[gondolin gondor], aliases[@name]["aliases"].keys.sort
230
183
 
231
- @index.delete_alias "gon*"
232
- assert_empty @index.get_alias("*")
233
- end
184
+ @index.delete_alias "gon*"
185
+ assert_empty @index.get_alias("*")
234
186
  end
235
187
 
236
188
  it "analyzes text and returns tokens" do
@@ -270,15 +222,11 @@ describe Elastomer::Client::Index do
270
222
  before do
271
223
  suggest = {
272
224
  :type => "completion",
273
- :index_analyzer => "simple",
225
+ :analyzer => "simple",
274
226
  :search_analyzer => "simple",
275
227
  :payloads => false
276
228
  }
277
229
 
278
- if es_version_2_x?
279
- suggest[:analyzer] = suggest.delete(:index_analyzer)
280
- end
281
-
282
230
  @index.create(
283
231
  :settings => { :number_of_shards => 1, :number_of_replicas => 0 },
284
232
  :mappings => {
@@ -327,14 +275,6 @@ describe Elastomer::Client::Index do
327
275
  assert_equal 0, response["_shards"]["failed"]
328
276
  end
329
277
 
330
- # COMPATIBILITY ES 1.2 removed support for the gateway snapshot API.
331
- if es_version_supports_gateway_snapshots?
332
- it "snapshots" do
333
- response = @index.snapshot
334
- assert_equal 0, response["_shards"]["failed"]
335
- end
336
- end
337
-
338
278
  it "recovery" do
339
279
  response = @index.recovery
340
280
  assert_includes response, "elastomer-index-test"
@@ -354,14 +294,6 @@ describe Elastomer::Client::Index do
354
294
  end
355
295
  end
356
296
 
357
- # The /<index>/_status endpoint has been removed in ES 2.X
358
- if es_version_1_x?
359
- it "gets status" do
360
- response = @index.status
361
- assert_includes response["indices"], "elastomer-index-test"
362
- end
363
- end
364
-
365
297
  it "gets segments" do
366
298
  @index.docs("foo").index("foo" => "bar")
367
299
  response = @index.segments