From 006fb543819d5105336cc5491cb346a1930d52f0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Mar 2018 12:44:46 -0800 Subject: [PATCH] Move logic out of model [skip ci] --- lib/searchkick/index.rb | 21 +++++++++++++++++++++ lib/searchkick/index_options.rb | 1 - lib/searchkick/model.rb | 21 ++------------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index e05e4ce..351f742 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -165,6 +165,27 @@ module Searchkick # reindex + def reindex(scope, method_name, scoped:, full: false, **options) + return unless Searchkick.callbacks? + + refresh = options.fetch(:refresh, !scoped) + + if method_name + # update + import_scope(scope, method_name: method_name) + self.refresh if refresh + true + elsif scoped && !full + # reindex association + import_scope(scope) + self.refresh if refresh + true + else + # full reindex + reindex_scope(scope, options) + end + end + def create_index(index_options: nil) index_options ||= self.index_options index = Searchkick::Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options) diff --git a/lib/searchkick/index_options.rb b/lib/searchkick/index_options.rb index 006cc42..255145b 100644 --- a/lib/searchkick/index_options.rb +++ b/lib/searchkick/index_options.rb @@ -174,7 +174,6 @@ module Searchkick # - Only apply the synonym expansion at index time # - Don't have the synonym filter applied search # - Use directional synonyms where appropriate. You want to make sure that you're not injecting terms that are too general. - settings[:analysis][:analyzer][default_analyzer][:filter].insert(4, "searchkick_synonym") if below60 settings[:analysis][:analyzer][default_analyzer][:filter] << "searchkick_synonym" %w(word_start word_middle word_end).each do |type| diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 8091c82..1a0ba57 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -36,28 +36,11 @@ module Searchkick end alias_method :search_index, :searchkick_index unless method_defined?(:search_index) - def searchkick_reindex(method_name = nil, full: false, **options) - return unless Searchkick.callbacks? - + def searchkick_reindex(method_name = nil, **options) scoped = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || (respond_to?(:queryable) && queryable != unscoped.with_default_scope) - refresh = options.fetch(:refresh, !scoped) - - if method_name - # update - searchkick_index.import_scope(searchkick_klass, method_name: method_name) - searchkick_index.refresh if refresh - true - elsif scoped && !full - # reindex association - searchkick_index.import_scope(searchkick_klass) - searchkick_index.refresh if refresh - true - else - # full reindex - searchkick_index.reindex_scope(searchkick_klass, options) - end + searchkick_index.reindex(searchkick_klass, method_name, scoped: scoped, **options) end alias_method :reindex, :searchkick_reindex unless method_defined?(:reindex) -- libgit2 0.21.0