Permalink
Browse files

Added the ability to transform documents when reindexing

  • Loading branch information...
1 parent 5129f8a commit 5818e50e26c9e21c8b3291544e1fecc388b04875 @karmi committed Aug 21, 2012
Showing with 19 additions and 2 deletions.
  1. +9 −2 lib/tire/index.rb
  2. +10 −0 test/integration/reindex_test.rb
View
11 lib/tire/index.rb
@@ -149,10 +149,17 @@ def reindex(name, options={}, &block)
new_index = Index.new(name)
new_index.create(options) unless new_index.exists?
+ transform = options.delete(:transform)
+
Search::Scan.new(self.name, &block).each do |results|
- new_index.bulk_store results.map do |document|
- document.to_hash.except(:type, :_index, :_explanation, :_score, :_version, :highlight, :sort)
+
+ documents = results.map do |document|
+ document = document.to_hash.except(:type, :_index, :_explanation, :_score, :_version, :highlight, :sort)
+ document = transform.call(document) if transform
+ document
end
+
+ new_index.bulk_store documents
end
end
View
10 test/integration/reindex_test.rb
@@ -39,6 +39,16 @@ class ReindexIntegrationTest < Test::Unit::TestCase
assert_equal 2, Tire.search('reindex-test-new').results.total
end
+ should "transform documents with a passed lambda" do
+ Tire.index('reindex-test').reindex 'reindex-test-new', transform: lambda { |document|
+ document[:title] += 'UPDATED'
+ document
+ }
+
+ Tire.index('reindex-test-new').refresh
+ assert_match /UPDATED/, Tire.search('reindex-test-new').results.first.title
+ end
+
end
end

0 comments on commit 5818e50

Please sign in to comment.