Skip to content

Commit b2fce1f

Browse files
committed
[MODEL] Minor changes and tweaks to the transform feature for import
Related: c4b42b5, #113
1 parent c4b42b5 commit b2fce1f

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __find_in_batches(options={}, &block)
9393
end
9494

9595
def __transform
96-
lambda {|model| { index: { _id: model.id, data: model.__elasticsearch__.as_indexed_json } }}
96+
lambda { |model| { index: { _id: model.id, data: model.__elasticsearch__.as_indexed_json } } }
9797
end
9898
end
9999
end

elasticsearch-model/lib/elasticsearch/model/importing.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ module ClassMethods
6868
#
6969
# Article.import scope: 'published'
7070
#
71-
# @example Customize how each record is [bulk imported](https://github.com/elasticsearch/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb)
71+
# @example Transform records during the import with a lambda
7272
#
73-
# transform = lambda do |article|
74-
# {index: {_id: article.id, _parent: article.author_id, data: article.__elasticsearch__.as_indexed_json}}
73+
# transform = lambda do |a|
74+
# {index: {_id: a.id, _parent: a.author_id, data: a.__elasticsearch__.as_indexed_json}}
7575
# end
7676
#
7777
# Article.import transform: transform
@@ -83,8 +83,9 @@ def import(options={}, &block)
8383
target_type = options.delete(:type) || document_type
8484
transform = options.delete(:transform) || __transform
8585

86-
if !transform.respond_to?(:call)
87-
raise ArgumentError, "You must pass an object that supports #call method, #{transform.class} given"
86+
unless transform.respond_to?(:call)
87+
raise ArgumentError,
88+
"Pass an object responding to `call` as the :transport option, #{transform.class} given"
8889
end
8990

9091
if options.delete(:force)
@@ -108,7 +109,7 @@ def import(options={}, &block)
108109
end
109110

110111
def __batch_to_bulk(batch, transform)
111-
batch.map {|model| transform.call(model)}
112+
batch.map { |model| transform.call(model) }
112113
end
113114
end
114115

elasticsearch-model/test/integration/active_record_import_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ class ::ImportArticle < ActiveRecord::Base
8181
ImportArticle.__elasticsearch__.refresh_index!
8282
assert_equal 100, ImportArticle.search('*').results.total
8383
end
84+
85+
should "transform documents with the option" do
86+
assert_equal 100, ImportArticle.count
87+
88+
assert_equal 0, ImportArticle.import( transform: ->(a) {{ index: { data: { name: a.title, foo: 'BAR' } }}} )
89+
90+
ImportArticle.__elasticsearch__.refresh_index!
91+
assert_contains ImportArticle.search('*').results.first._source.keys, 'name'
92+
assert_contains ImportArticle.search('*').results.first._source.keys, 'foo'
93+
assert_equal 100, ImportArticle.search('test').results.total
94+
assert_equal 100, ImportArticle.search('bar').results.total
95+
end
8496
end
8597

8698
end

elasticsearch-model/test/unit/adapter_active_record_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def ids
113113
assert_respond_to @transform, :call
114114
end
115115

116-
should "provide basic transformation" do
116+
should "provide default transformation" do
117117
model = mock("model", id: 1, __elasticsearch__: stub(as_indexed_json: {}))
118118
assert_equal @transform.call(model), { index: { _id: 1, data: {} } }
119119
end

elasticsearch-model/test/unit/importing_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def importing_mixin
126126
DummyImportingModel.import index: 'my-new-index', type: 'my-other-type'
127127
end
128128

129-
should "default to the adapter's bulk transform" do
129+
should "use the default transform from adapter" do
130130
client = mock('client', bulk: {'items' => []})
131131
transform = lambda {|a|}
132132

@@ -137,7 +137,7 @@ def importing_mixin
137137
DummyImportingModel.import index: 'foo', type: 'bar'
138138
end
139139

140-
should "use the optioned transform" do
140+
should "use the transformer from options" do
141141
client = mock('client', bulk: {'items' => []})
142142
transform = lambda {|a|}
143143

@@ -147,7 +147,7 @@ def importing_mixin
147147
DummyImportingModel.import index: 'foo', type: 'bar', transform: transform
148148
end
149149

150-
should "raise an ArgumentError if transform is an object that doesn't respond to #call" do
150+
should "raise an ArgumentError if transform doesn't respond to the call method" do
151151
assert_raise ArgumentError do
152152
DummyImportingModel.import index: 'foo', type: 'bar', transform: "not_callable"
153153
end

0 commit comments

Comments
 (0)