diff --git a/README.md b/README.md index c169ffb..75de7e9 100644 --- a/README.md +++ b/README.md @@ -172,10 +172,14 @@ Product.search "zucini", misspellings: {distance: 2} # zucchini ### Indexing -Control what data is indexed with the `search_data` method. Call `Product.reindex` after changing this method. +Control which models are indexed with `#should_index?` and what data is indexed with the `#search_data`. Call `Product.reindex` after changing this method. ```ruby class Product < ActiveRecord::Base + def should_index? + active + end + def search_data as_json only: [:name, :active] # or equivalently diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 933512f..3eb007c 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -18,12 +18,18 @@ module Searchkick extend Searchkick::Reindex include Searchkick::Similar + def should_index? + true + end + def reindex - index = self.class.searchkick_index - if destroyed? - index.remove self - else - index.store self + if should_index? + index = self.class.searchkick_index + if destroyed? + index.remove self + else + index.store self + end end end diff --git a/lib/searchkick/reindex.rb b/lib/searchkick/reindex.rb index 5f39f6e..491ad32 100644 --- a/lib/searchkick/reindex.rb +++ b/lib/searchkick/reindex.rb @@ -62,14 +62,14 @@ module Searchkick scope = scope.search_import if scope.respond_to?(:search_import) if scope.respond_to?(:find_in_batches) scope.find_in_batches do |batch| - index.import batch + index.import batch.select{ |item| item.should_index? } end else # https://github.com/karmi/tire/blob/master/lib/tire/model/import.rb # use cursor for Mongoid items = [] scope.all.each do |item| - items << item + items << item if item.should_index? if items.length % 1000 == 0 index.import items items = [] diff --git a/searchkick.gemspec b/searchkick.gemspec index 0e60113..ab88d95 100644 --- a/searchkick.gemspec +++ b/searchkick.gemspec @@ -26,4 +26,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "minitest", "~> 4.7" spec.add_development_dependency "activerecord" spec.add_development_dependency "pg" + spec.add_development_dependency "mocha" end diff --git a/test/model_test.rb b/test/model_test.rb new file mode 100644 index 0000000..d57e4c7 --- /dev/null +++ b/test/model_test.rb @@ -0,0 +1,26 @@ +require_relative "test_helper" + +describe "Model#should_index?" do + it 'Model.reindex checks for #should_index?' do + indexed = Animal.new(name: 'should') + indexed.stubs(:should_index?).returns(true) + + not_indexed = Animal.new(name: 'should not') + not_indexed.stubs(:should_index?).returns(false) + + [indexed, not_indexed].each(&:save!) + Animal.searchkick_index.refresh + + assert_search 'should', ['should'], {}, Animal + end + + it 'Model#reindex checks for #should_index?' do + Animal.any_instance.stubs(:should_index?).returns(false) + store_names ['should not', 'shouldnt'], Animal + + Animal.reindex + Animal.searchkick_index.refresh + + assert_search 'should', [], {}, Animal + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 29dccb3..1958986 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,6 +2,7 @@ require "bundler/setup" Bundler.require(:default) require "minitest/autorun" require "minitest/pride" +require "mocha/setup" ENV["RACK_ENV"] = "test" @@ -151,16 +152,16 @@ class Minitest::Unit::TestCase end # no order - def assert_search(term, expected, options = {}) - assert_equal expected.sort, Product.search(term, options).map(&:name).sort + def assert_search(term, expected, options = {}, klass = Product) + assert_equal expected.sort, klass.search(term, options).map(&:name).sort end - def assert_order(term, expected, options = {}) - assert_equal expected, Product.search(term, options).map(&:name) + def assert_order(term, expected, options = {}, klass = Product) + assert_equal expected, klass.search(term, options).map(&:name) end - def assert_first(term, expected, options = {}) - assert_equal expected, Product.search(term, options).map(&:name).first + def assert_first(term, expected, options = {}, klass = Product) + assert_equal expected, klass.search(term, options).map(&:name).first end end -- libgit2 0.21.0