Commit 006fb543819d5105336cc5491cb346a1930d52f0

Authored by Andrew
1 parent fd936e49

Move logic out of model [skip ci]

lib/searchkick/index.rb
@@ -165,6 +165,27 @@ module Searchkick @@ -165,6 +165,27 @@ module Searchkick
165 165
166 # reindex 166 # reindex
167 167
  168 + def reindex(scope, method_name, scoped:, full: false, **options)
  169 + return unless Searchkick.callbacks?
  170 +
  171 + refresh = options.fetch(:refresh, !scoped)
  172 +
  173 + if method_name
  174 + # update
  175 + import_scope(scope, method_name: method_name)
  176 + self.refresh if refresh
  177 + true
  178 + elsif scoped && !full
  179 + # reindex association
  180 + import_scope(scope)
  181 + self.refresh if refresh
  182 + true
  183 + else
  184 + # full reindex
  185 + reindex_scope(scope, options)
  186 + end
  187 + end
  188 +
168 def create_index(index_options: nil) 189 def create_index(index_options: nil)
169 index_options ||= self.index_options 190 index_options ||= self.index_options
170 index = Searchkick::Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options) 191 index = Searchkick::Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options)
lib/searchkick/index_options.rb
@@ -174,7 +174,6 @@ module Searchkick @@ -174,7 +174,6 @@ module Searchkick
174 # - Only apply the synonym expansion at index time 174 # - Only apply the synonym expansion at index time
175 # - Don't have the synonym filter applied search 175 # - Don't have the synonym filter applied search
176 # - Use directional synonyms where appropriate. You want to make sure that you're not injecting terms that are too general. 176 # - Use directional synonyms where appropriate. You want to make sure that you're not injecting terms that are too general.
177 - settings[:analysis][:analyzer][default_analyzer][:filter].insert(4, "searchkick_synonym") if below60  
178 settings[:analysis][:analyzer][default_analyzer][:filter] << "searchkick_synonym" 177 settings[:analysis][:analyzer][default_analyzer][:filter] << "searchkick_synonym"
179 178
180 %w(word_start word_middle word_end).each do |type| 179 %w(word_start word_middle word_end).each do |type|
lib/searchkick/model.rb
@@ -36,28 +36,11 @@ module Searchkick @@ -36,28 +36,11 @@ module Searchkick
36 end 36 end
37 alias_method :search_index, :searchkick_index unless method_defined?(:search_index) 37 alias_method :search_index, :searchkick_index unless method_defined?(:search_index)
38 38
39 - def searchkick_reindex(method_name = nil, full: false, **options)  
40 - return unless Searchkick.callbacks?  
41 - 39 + def searchkick_reindex(method_name = nil, **options)
42 scoped = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || 40 scoped = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) ||
43 (respond_to?(:queryable) && queryable != unscoped.with_default_scope) 41 (respond_to?(:queryable) && queryable != unscoped.with_default_scope)
44 42
45 - refresh = options.fetch(:refresh, !scoped)  
46 -  
47 - if method_name  
48 - # update  
49 - searchkick_index.import_scope(searchkick_klass, method_name: method_name)  
50 - searchkick_index.refresh if refresh  
51 - true  
52 - elsif scoped && !full  
53 - # reindex association  
54 - searchkick_index.import_scope(searchkick_klass)  
55 - searchkick_index.refresh if refresh  
56 - true  
57 - else  
58 - # full reindex  
59 - searchkick_index.reindex_scope(searchkick_klass, options)  
60 - end 43 + searchkick_index.reindex(searchkick_klass, method_name, scoped: scoped, **options)
61 end 44 end
62 alias_method :reindex, :searchkick_reindex unless method_defined?(:reindex) 45 alias_method :reindex, :searchkick_reindex unless method_defined?(:reindex)
63 46