Skip to content

Commit 51cbe7b

Browse files
nonokarmi
authored andcommitted
[MODEL] Added, that records to import can be limited by the query option
Closes elastic#247
1 parent edae9eb commit 51cbe7b

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

elasticsearch-model/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Article.import
146146
# => 0
147147
```
148148

149-
It's possible to import only records from a specific `scope`, transform the batch with the `transform`
149+
It's possible to import only records from a specific `scope` or `query`, transform the batch with the `transform`
150150
and `preprocess` options, or re-create the index by deleting it and creating it with correct mapping with the `force` option -- look for examples in the method documentation.
151151

152152
No errors were reported during importing, so... let's search the index!

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ module Importing
8484
# @see http://api.rubyonrails.org/classes/ActiveRecord/Batches.html ActiveRecord::Batches.find_in_batches
8585
#
8686
def __find_in_batches(options={}, &block)
87+
query = options.delete(:query)
8788
named_scope = options.delete(:scope)
8889
preprocess = options.delete(:preprocess)
8990

90-
scope = named_scope ? self.__send__(named_scope) : self
91+
scope = self
92+
scope = scope.__send__(named_scope) if named_scope
93+
scope = scope.instance_exec(&query) if query
9194

9295
scope.find_in_batches(options) do |batch|
9396
yield (preprocess ? self.__send__(preprocess, batch) : batch)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ module ClassMethods
6868
#
6969
# Article.import scope: 'published'
7070
#
71+
# @example Pass an ActiveRecord query to limit the imported records
72+
#
73+
# Article.import query: -> { where(author_id: author_id) }
74+
#
7175
# @example Transform records during the import with a lambda
7276
#
7377
# transform = lambda do |a|

elasticsearch-model/test/integration/active_record_import_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ class ::ImportArticle < ActiveRecord::Base
6262
assert_equal 50, ImportArticle.search('*').results.total
6363
end
6464

65+
should "import only documents from a specific query" do
66+
assert_equal 100, ImportArticle.count
67+
68+
assert_equal 0, ImportArticle.import(query: -> { where('views >= 30') })
69+
70+
ImportArticle.__elasticsearch__.refresh_index!
71+
assert_equal 70, ImportArticle.search('*').results.total
72+
end
73+
6574
should "report and not store/index invalid documents" do
6675
ImportArticle.create! title: "Test INVALID", numeric: "INVALID"
6776

elasticsearch-model/test/unit/adapter_active_record_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ def ids
104104
DummyClassForActiveRecord.__find_in_batches(scope: :published) do; end
105105
end
106106

107+
should "limit the relation to a specific query" do
108+
DummyClassForActiveRecord.expects(:find_in_batches).returns([])
109+
DummyClassForActiveRecord.expects(:where).returns(DummyClassForActiveRecord)
110+
111+
DummyClassForActiveRecord.__find_in_batches(query: -> { where(color: "red") }) do; end
112+
end
113+
107114
should "preprocess the batch if option provided" do
108115
class << DummyClassForActiveRecord
109116
# Updates/transforms the batch while fetching it from the database

0 commit comments

Comments
 (0)