elastomer-client 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cd17dd15348e159466cead916479b5bcf68b80b
4
- data.tar.gz: b2f0b0e2455ad13ed6a3a1670cf20a58e6239501
3
+ metadata.gz: e82302d43fced9352f7a34365d970c5c68372244
4
+ data.tar.gz: 5a5125d962c5648390e13d20ff15de07dc63d117
5
5
  SHA512:
6
- metadata.gz: c9c2a2798145fa90c3291459de33dcdf01c1ad806b64e19b719fb6f5679c3b4b165583cb3ee4101a1f3e17879d15da93c350e88703256c1fab9755df80eda187
7
- data.tar.gz: ce53c9838c420f6932d7792a088bdcd6ea7d3735e12f3df51d52459cc849b934cc91ff0703317574ad01ab75d0ca91ad0ef839d55cf7988a7417b499e52fcaed
6
+ metadata.gz: d1359ce4cb196c178ad8015e9b351ac5cdc227dbdd1b68b4ba0eef12fba520e6e12f83b3112dadef8b07f3977736999ada69850e853f34be366d9aa89212ed4f
7
+ data.tar.gz: 9c3973a14dd4a064c15b012cde567ab12bc1692937d38969784ef728b60e61cd7f562d8cf77446beef089f6fa5fa2083ea3647c6628f5e9fc99e0f526896351d
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "bundler", "~> 1.5"
28
28
  spec.add_development_dependency "minitest","~> 4.7"
29
29
  spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "debugger", "~> 1.6.8"
30
31
  end
@@ -18,12 +18,12 @@ module Elastomer
18
18
  #
19
19
  # Examples
20
20
  #
21
- # bulk( request_body, :index => 'default-index' )
21
+ # bulk(request_body, :index => 'default-index')
22
22
  #
23
- # bulk( :index => 'default-index' ) do |b|
24
- # b.index( document1 )
25
- # b.index( document2 )
26
- # b.delete( document3 )
23
+ # bulk(:index => 'default-index') do |b|
24
+ # b.index(document1)
25
+ # b.index(document2, :_type => 'default-type')
26
+ # b.delete(document3)
27
27
  # ...
28
28
  # end
29
29
  #
@@ -118,58 +118,60 @@ module Elastomer
118
118
  end
119
119
 
120
120
  # Add an index action to the list of bulk actions to be performed when
121
- # the bulk API call is made. The `_id` of the document cannot be `nil`
122
- # or empty. If this is the case then we remove the `_id` and allow
123
- # ElasticSearch to generate one.
121
+ # the bulk API call is made. Parameters can be provided in the
122
+ # parameters hash (underscore prefix optional) or in the document
123
+ # hash (underscore prefix required).
124
124
  #
125
125
  # document - The document to index as a Hash or JSON encoded String
126
- # params - Parameters for the index action (as a Hash)
126
+ # params - Parameters for the index action (as a Hash) (optional)
127
+ #
128
+ # Examples
129
+ # index({"foo" => "bar"}, {:_id => 1, :_type => "foo"}
130
+ # index({"foo" => "bar"}, {:id => 1, :type => "foo"}
131
+ # index("foo" => "bar", "_id" => 1, "_type" => "foo")
127
132
  #
128
133
  # Returns the response from the bulk call if one was made or nil.
129
134
  def index( document, params = {} )
130
- unless String === document
131
- overrides = from_document(document)
132
- params = params.merge overrides
133
- end
134
- params.delete(:_id) if params[:_id].nil? || params[:_id].to_s.empty?
135
-
135
+ params = prepare_params(document, params)
136
136
  add_to_actions({:index => params}, document)
137
137
  end
138
138
  alias :add :index
139
139
 
140
140
  # Add a create action to the list of bulk actions to be performed when
141
- # the bulk API call is made. The `_id` of the document cannot be `nil`
142
- # or empty.
141
+ # the bulk API call is made. Parameters can be provided in the
142
+ # parameters hash (underscore prefix optional) or in the document
143
+ # hash (underscore prefix required).
143
144
  #
144
145
  # document - The document to create as a Hash or JSON encoded String
145
- # params - Parameters for the create action (as a Hash)
146
+ # params - Parameters for the create action (as a Hash) (optional)
147
+ #
148
+ # Examples
149
+ # create({"foo" => "bar"}, {:_id => 1}
150
+ # create({"foo" => "bar"}, {:id => 1}
151
+ # create("foo" => "bar", "_id" => 1)
146
152
  #
147
153
  # Returns the response from the bulk call if one was made or nil.
148
154
  def create( document, params )
149
- unless String === document
150
- overrides = from_document(document)
151
- params = params.merge overrides
152
- end
153
- params.delete(:_id) if params[:_id].nil? || params[:_id].to_s.empty?
154
-
155
+ params = prepare_params(document, params)
155
156
  add_to_actions({:create => params}, document)
156
157
  end
157
158
 
158
159
  # Add an update action to the list of bulk actions to be performed when
159
- # the bulk API call is made. The `_id` of the document cannot be `nil`
160
- # or empty.
160
+ # the bulk API call is made. Parameters can be provided in the parameters
161
+ # hash (underscore prefix optional) or in the document hash (underscore
162
+ # prefix required).
161
163
  #
162
164
  # document - The document to update as a Hash or JSON encoded String
163
- # params - Parameters for the update action (as a Hash)
165
+ # params - Parameters for the update action (as a Hash) (optional)
166
+ #
167
+ # Examples
168
+ # update({"foo" => "bar"}, {:_id => 1}
169
+ # update({"foo" => "bar"}, {:id => 1}
170
+ # update("foo" => "bar", "_id" => 1)
164
171
  #
165
172
  # Returns the response from the bulk call if one was made or nil.
166
173
  def update( document, params )
167
- unless String === document
168
- overrides = from_document(document)
169
- params = params.merge overrides
170
- end
171
- params.delete(:_id) if params[:_id].nil? || params[:_id].to_s.empty?
172
-
174
+ params = prepare_params(document, params)
173
175
  add_to_actions({:update => params}, document)
174
176
  end
175
177
 
@@ -177,10 +179,14 @@ module Elastomer
177
179
  # the bulk API call is made.
178
180
  #
179
181
  # params - Parameters for the delete action (as a Hash)
182
+ #
183
+ # Examples
184
+ # delete(:_id => 1, :_type => 'foo')
180
185
  #
181
186
  # Returns the response from the bulk call if one was made or nil.
182
187
  def delete( params )
183
- add_to_actions :delete => params
188
+ params = prepare_params(nil, params)
189
+ add_to_actions({:delete => params})
184
190
  end
185
191
 
186
192
  # Immediately execute a bulk API call with the currently accumulated
@@ -201,6 +207,20 @@ module Elastomer
201
207
  @actions.clear
202
208
  end
203
209
 
210
+ SPECIAL_KEYS = %w[id type index version version_type routing parent percolator timestamp ttl retry_on_conflict]
211
+ SPECIAL_KEYS_HASH = SPECIAL_KEYS.inject({}) { |h, k| h[k] = "_#{k}"; h }
212
+
213
+ # Internal: convert special key parameters to their wire representation
214
+ # and apply any override document parameters.
215
+ def prepare_params(document, params)
216
+ params = convert_special_keys(params)
217
+ unless document.nil? || String === document
218
+ params = from_document(document).merge(params)
219
+ end
220
+ params.delete(:_id) if params[:_id].nil? || params[:_id].to_s.empty?
221
+ params
222
+ end
223
+
204
224
  # Internal: Extract special keys for bulk indexing from the given
205
225
  # `document`. The keys and their values are returned as a Hash from this
206
226
  # method. If a value is `nil` then it will be ignored.
@@ -211,15 +231,36 @@ module Elastomer
211
231
  def from_document( document )
212
232
  opts = {}
213
233
 
214
- %w[_id _type _index _version _version_type _routing _parent _percolator _timestamp _ttl _retry_on_conflict].each do |field|
234
+ SPECIAL_KEYS_HASH.values.each do |field|
215
235
  key = field.to_sym
216
- opts[key] = document.delete field if document[field]
217
- opts[key] = document.delete key if document[key]
236
+ opts[key] = document.delete field if document.key? field
237
+ opts[key] = document.delete key if document.key? key
218
238
  end
219
239
 
220
240
  opts
221
241
  end
222
242
 
243
+ # Internal: Convert incoming Ruby symbol keys to their special underscore
244
+ # versions. Maintains API compaibility with the `Docs` API for `index`,
245
+ # `create`, `update` and `delete`.
246
+ #
247
+ # :id -> :_id
248
+ # 'id' -> '_id'
249
+ #
250
+ # params - Hash.
251
+ #
252
+ # Returns a new params Hash with the special keys replaced.
253
+ def convert_special_keys(params)
254
+ new_params = params.dup
255
+
256
+ SPECIAL_KEYS_HASH.each do |k1, k2|
257
+ new_params[k2] = new_params.delete k1 if new_params.key? k1
258
+ new_params[k2.to_sym] = new_params.delete k1.to_sym if new_params.key? k1.to_sym
259
+ end
260
+
261
+ new_params
262
+ end
263
+
223
264
  # Internal: Add the given `action` to the list of actions that will be
224
265
  # performed by this bulk request. An optional `document` can also be
225
266
  # given.
@@ -267,7 +267,7 @@ Percolate
267
267
  # params - Parameters Hash
268
268
  #
269
269
  # Examples
270
- #
270
+ #
271
271
  # # request body query
272
272
  # validate(:query_string => {:query => "*:*"})
273
273
  #
@@ -1,5 +1,5 @@
1
1
  module Elastomer
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -223,4 +223,74 @@ describe Elastomer::Client::Bulk do
223
223
 
224
224
  assert_equal 10, h['hits']['total']
225
225
  end
226
+
227
+ it 'uses :id from parameters' do
228
+ response = @index.bulk do |b|
229
+ document = { :_type => 'tweet', :author => 'pea53', :message => 'just a test tweet' }
230
+ params = { :id => 'foo' }
231
+
232
+ b.index document, params
233
+ end
234
+
235
+ assert_instance_of Fixnum, response['took']
236
+
237
+ items = response['items']
238
+ assert_bulk_index(items[0])
239
+
240
+ assert_equal 'foo', items[0]['index']['_id']
241
+ end
242
+
243
+ it 'supports symbol and string parameters' do
244
+ response = @index.bulk do |b|
245
+ doc1 = { :author => 'pea53', :message => 'a tweet about foo' }
246
+ b.index doc1, { :id => 'foo', :type => 'tweet' }
247
+
248
+ doc2 = { :author => 'pea53', :message => 'a tweet about bar' }
249
+ b.index doc2, { 'id' => 'bar', 'type' => 'tweet' }
250
+ end
251
+
252
+ assert_instance_of Fixnum, response['took']
253
+
254
+ items = response['items']
255
+ assert_bulk_index(items[0])
256
+ assert_bulk_index(items[1])
257
+
258
+ assert_equal 'a tweet about foo', @index.docs('tweet').get(:id => 'foo')['_source']['message']
259
+ assert_equal 'a tweet about bar', @index.docs('tweet').get(:id => 'bar')['_source']['message']
260
+ end
261
+
262
+ it 'doesn\'t override parameters from the document' do
263
+ document = { :_id => 1, :_type => 'tweet', :author => 'pea53', :message => 'just a test tweet' }
264
+ params = { :id => 2 }
265
+
266
+ response = @index.bulk do |b|
267
+ b.index document, params
268
+ end
269
+
270
+ assert_instance_of Fixnum, response['took']
271
+
272
+ items = response['items']
273
+ assert_bulk_index(items[0])
274
+
275
+ refute_found @index.docs('tweet').get(:id => 1)
276
+ assert_equal 'just a test tweet', @index.docs('tweet').get(:id => 2)['_source']['message']
277
+ end
278
+
279
+ it 'doesn\'t upgrade non-prefixed keys to parameters' do
280
+ document = { :id => 1, :type => 'book', :version => 5, :author => 'pea53', :message => 'just a test tweet' }
281
+ params = { :id => 2, :type => 'tweet' }
282
+
283
+ response = @index.bulk do |b|
284
+ b.index document, params
285
+ end
286
+
287
+ assert_instance_of Fixnum, response['took']
288
+
289
+ items = response['items']
290
+ assert_bulk_index(items[0])
291
+
292
+ assert_equal '2', items[0]['index']['_id']
293
+ assert_equal 'tweet', items[0]['index']['_type']
294
+ assert_equal 1, items[0]['index']['_version']
295
+ end
226
296
  end
@@ -363,26 +363,42 @@ describe Elastomer::Client::Docs do
363
363
  end
364
364
  end
365
365
 
366
- def populate!
367
- @docs.add \
366
+ it 'supports bulk operations with the same parameters as docs' do
367
+ response = @docs.bulk do |b|
368
+ populate!(b)
369
+ end
370
+
371
+ assert_instance_of Fixnum, response['took']
372
+
373
+ response = @docs.get(:id => 1, :type => 'doc1')
374
+ assert_found response
375
+ assert_equal 'mojombo', response['_source']['author']
376
+ end
377
+
378
+ # Create/index multiple documents.
379
+ #
380
+ # docs - An instance of Elastomer::Client::Docs or Elastomer::Client::Bulk. If
381
+ # nil uses the @docs instance variable.
382
+ def populate!(docs = @docs)
383
+ docs.add \
368
384
  :_id => 1,
369
385
  :_type => 'doc1',
370
386
  :title => 'the author of gravatar',
371
387
  :author => 'mojombo'
372
388
 
373
- @docs.add \
389
+ docs.add \
374
390
  :_id => 2,
375
391
  :_type => 'doc1',
376
392
  :title => 'the author of resque',
377
393
  :author => 'defunkt'
378
394
 
379
- @docs.add \
395
+ docs.add \
380
396
  :_id => 1,
381
397
  :_type => 'doc2',
382
398
  :title => 'the author of logging',
383
399
  :author => 'pea53'
384
400
 
385
- @docs.add \
401
+ docs.add \
386
402
  :_id => 2,
387
403
  :_type => 'doc2',
388
404
  :title => 'the author of rubber-band',
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastomer-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-24 00:00:00.000000000 Z
12
+ date: 2014-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -109,6 +109,20 @@ dependencies:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: debugger
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: 1.6.8
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: 1.6.8
112
126
  description: |-
113
127
  Elastomer is a low level API client for the
114
128
  Elasticsearch HTTP interface.