Commit b1cad52499abd346bc2ef727930fde2220c018b1

Authored by Elom Gomez
1 parent d0084301

Add partial_reindex instance method and fix tests

lib/searchkick/model.rb
@@ -101,6 +101,12 @@ module Searchkick @@ -101,6 +101,12 @@ module Searchkick
101 self.class.searchkick_index.reindex_record_async(self) 101 self.class.searchkick_index.reindex_record_async(self)
102 end unless method_defined?(:reindex_async) 102 end unless method_defined?(:reindex_async)
103 103
  104 + def partial_reindex(method_name)
  105 + self.class.searchkick_index.bulk_update([self], method_name)
  106 + self.class.searchkick_index.refresh
  107 + true
  108 + end unless method_defined?(:partial_reindex)
  109 +
104 def similar(options = {}) 110 def similar(options = {})
105 self.class.searchkick_index.similar_record(self, options) 111 self.class.searchkick_index.similar_record(self, options)
106 end unless method_defined?(:similar) 112 end unless method_defined?(:similar)
test/partial_reindex_test.rb
@@ -9,8 +9,7 @@ class PartialReindexTest < Minitest::Test @@ -9,8 +9,7 @@ class PartialReindexTest < Minitest::Test
9 assert_search "blue", ["Hi"], fields: [:color], load: false 9 assert_search "blue", ["Hi"], fields: [:color], load: false
10 10
11 # update 11 # update
12 - Product.first.update_column :name, "Bye"  
13 - Product.first.update_column :color, "Red" 12 + Product.first.update_columns(name: "Bye", color: "Red")
14 Product.searchkick_index.refresh 13 Product.searchkick_index.refresh
15 14
16 # index not updated 15 # index not updated
@@ -24,4 +23,27 @@ class PartialReindexTest < Minitest::Test @@ -24,4 +23,27 @@ class PartialReindexTest < Minitest::Test
24 assert_search "bye", ["Bye"], fields: [:name], load: false 23 assert_search "bye", ["Bye"], fields: [:name], load: false
25 assert_search "blue", ["Bye"], fields: [:color], load: false 24 assert_search "blue", ["Bye"], fields: [:color], load: false
26 end 25 end
  26 +
  27 + def test_with_instance_method
  28 + store [{name: "Hi", color: "Blue"}]
  29 +
  30 + # normal search
  31 + assert_search "hi", ["Hi"], fields: [:name], load: false
  32 + assert_search "blue", ["Hi"], fields: [:color], load: false
  33 +
  34 + # update
  35 + product = Product.first
  36 + product.update_columns(name: "Bye", color: "Red")
  37 + Product.searchkick_index.refresh
  38 +
  39 + # index not updated
  40 + assert_search "hi", ["Hi"], fields: [:name], load: false
  41 + assert_search "blue", ["Hi"], fields: [:color], load: false
  42 +
  43 + product.partial_reindex(:search_name)
  44 +
  45 + # name updated, but not color
  46 + assert_search "bye", ["Bye"], fields: [:name], load: false
  47 + assert_search "blue", ["Bye"], fields: [:color], load: false
  48 + end
27 end 49 end