Skip to content

Commit 351e3b7

Browse files
committed
[MODEL] Retouched the code, tests and examples for the preprocess option
Related: #151, #152
1 parent 9339bf7 commit 351e3b7

File tree

4 files changed

+31
-41
lines changed

4 files changed

+31
-41
lines changed

elasticsearch-model/README.md

+3-22
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ Article.import
146146
# => 0
147147
```
148148

149+
It's possible to import only records from a specific `scope`, or transform the batch with the `transform`
150+
and `preprocess` options -- look for examples in the method documentation.
151+
149152
No errors were reported during importing, so... let's search the index!
150153

151154

@@ -657,28 +660,6 @@ response.records.records.class
657660
More examples can be found in the `examples` folder. Please see the `Elasticsearch::Model::Adapter`
658661
module and its submodules for technical information.
659662

660-
#### Customizing `Elasticsearch::Model::Adapter::ActiveRecord`
661-
662-
If you need to integrate with an external service (such as Quickbooks) to get content during an import, you might need to run each batch of records through a preprocessor before importing. Use the `preprocess` option to specify the method used to preprocess each batch:
663-
664-
```ruby
665-
class PurchaseOrder
666-
# Queries the external service in batch
667-
# to improve performance and reduce requests.
668-
def add_content_from_quickbooks(batch)
669-
quickbooks_purchase_orders = Hash[QuickbooksPurchaseOrder.where(id: batch.collect(&:quickbooks_id)).collect { |qpo| [qpo.id, qpo] }]
670-
batch.each do |purchase_order|
671-
purchase_order.in_quickbooks = quickbooks_purchase_orders[purchase_order.quickbooks_id]
672-
end
673-
batch
674-
end
675-
end
676-
677-
PurchaseOrder.import(preprocess: :add_content_from_quickbooks)
678-
679-
```
680-
681-
682663
## Development and Community
683664

684665
For local development, clone the repository and run `bundle install`. See `rake -T` for a list of

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def self.included(base)
7878

7979
module Importing
8080

81-
# Fetch batches of records from the database
81+
# Fetch batches of records from the database (used by the import method)
82+
#
8283
#
8384
# @see http://api.rubyonrails.org/classes/ActiveRecord/Batches.html ActiveRecord::Batches.find_in_batches
8485
#

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

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ module ClassMethods
7676
#
7777
# Article.import transform: transform
7878
#
79+
# @example Update the batch before yielding it
80+
#
81+
# class Article
82+
# # ...
83+
# def enrich(batch)
84+
# batch.each do |item|
85+
# item.metadata = MyAPI.get_metadata(item.id)
86+
# end
87+
# batch
88+
# end
89+
# end
90+
#
91+
# Article.import preprocess: enrich
92+
#
7993
def import(options={}, &block)
8094
errors = 0
8195
refresh = options.delete(:refresh) || false

elasticsearch-model/test/unit/adapter_active_record_test.rb

+12-18
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ def response
1616
def ids
1717
[2, 1]
1818
end
19-
20-
class << self
21-
def augment_collection(batch)
22-
# This might update the batch with content from an external system (like Quickbooks).
23-
# Performing that external query in batch might be required for performance/throttling reasons.
24-
25-
# This is just a silly example for the test.
26-
batch.collect { |b| b.to_s + '!' }
27-
end
28-
29-
def find_in_batches(options={}, &block)
30-
yield [:a, :b]
31-
end
32-
end
3319
end
3420

3521
RESPONSE = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [] } }
@@ -119,12 +105,20 @@ def find_in_batches(options={}, &block)
119105
end
120106

121107
should "preprocess the batch if option provided" do
122-
DummyClassForActiveRecord.__find_in_batches(preprocess: :augment_collection) do |batch|
123-
batch.each do |b|
124-
# the example augment collection method adds an '!' to the end of each element
125-
assert_equal b[-1], '!'
108+
class << DummyClassForActiveRecord
109+
# Updates/transforms the batch while fetching it from the database
110+
# (eg. with information from an external system)
111+
#
112+
def update_batch(batch)
113+
batch.collect { |b| b.to_s + '!' }
126114
end
127115
end
116+
117+
DummyClassForActiveRecord.expects(:__find_in_batches).returns( [:a, :b] )
118+
119+
DummyClassForActiveRecord.__find_in_batches(preprocess: :update_batch) do |batch|
120+
assert_same_elements ["a!", "b!"], batch
121+
end
128122
end
129123

130124
context "when transforming models" do

0 commit comments

Comments
 (0)