Commit af170b19c2e7c6e91c7277e646c25b29408bd822

Authored by Andrew Kane
1 parent df81e3c3

Much better organization

lib/searchkick.rb
... ... @@ -3,10 +3,8 @@ require "elasticsearch"
3 3 require "hashie"
4 4 require "searchkick/version"
5 5 require "searchkick/index"
6   -require "searchkick/reindex"
7 6 require "searchkick/results"
8 7 require "searchkick/query"
9   -require "searchkick/similar"
10 8 require "searchkick/reindex_job"
11 9 require "searchkick/model"
12 10 require "searchkick/tasks"
... ... @@ -30,11 +28,13 @@ module Searchkick
30 28 attr_accessor :search_method_name
31 29 attr_accessor :wordnet_path
32 30 attr_accessor :timeout
  31 + attr_accessor :models
33 32 end
34 33 self.callbacks = true
35 34 self.search_method_name = :search
36 35 self.wordnet_path = "/var/lib/wn_s.pl"
37 36 self.timeout = 10
  37 + self.models = []
38 38  
39 39 def self.client
40 40 @client ||=
... ...
lib/searchkick/index.rb
... ... @@ -27,6 +27,34 @@ module Searchkick
27 27 client.indices.exists_alias name: name
28 28 end
29 29  
  30 + def search_model(searchkick_klass, term = nil, options = {}, &block)
  31 + query = Searchkick::Query.new(searchkick_klass, term, options)
  32 + if block
  33 + block.call(query.body)
  34 + end
  35 + if options[:execute] == false
  36 + query
  37 + else
  38 + query.execute
  39 + end
  40 + end
  41 +
  42 + def similar_record(record, options = {})
  43 + like_text = retrieve(record).to_hash
  44 + .keep_if{|k,v| !options[:fields] || options[:fields].map(&:to_s).include?(k) }
  45 + .values.compact.join(" ")
  46 +
  47 + # TODO deep merge method
  48 + options[:where] ||= {}
  49 + options[:where][:_id] ||= {}
  50 + options[:where][:_id][:not] = record.id.to_s
  51 + options[:limit] ||= 10
  52 + options[:similar] = true
  53 +
  54 + # TODO use index class instead of record class
  55 + search_model(record.class, like_text, options)
  56 + end
  57 +
30 58 def swap(new_name)
31 59 old_indices =
32 60 begin
... ...
lib/searchkick/model.rb
... ... @@ -4,6 +4,8 @@ module Searchkick
4 4 def searchkick(options = {})
5 5 raise "Only call searchkick once per model" if respond_to?(:searchkick_index)
6 6  
  7 + Searchkick.models << self
  8 +
7 9 class_eval do
8 10 cattr_reader :searchkick_options, :searchkick_klass
9 11  
... ... @@ -14,28 +16,50 @@ module Searchkick
14 16 class_variable_set :@@searchkick_callbacks, callbacks
15 17 class_variable_set :@@searchkick_index, options[:index_name] || [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_")
16 18  
17   - def self.searchkick_index
18   - index = class_variable_get :@@searchkick_index
19   - index = index.call if index.respond_to? :call
20   - Searchkick::Index.new(index, class_variable_get(:@@searchkick_options))
  19 + define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}, &block|
  20 + searchkick_index.search_model(self, term, options, &block)
21 21 end
22 22  
23   - define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}, &block|
24   - query = Searchkick::Query.new(self, term, options)
25   - if block
26   - block.call(query.body)
  23 + class << self
  24 +
  25 + def searchkick_index
  26 + index = class_variable_get :@@searchkick_index
  27 + index = index.call if index.respond_to? :call
  28 + Searchkick::Index.new(index, class_variable_get(:@@searchkick_options))
27 29 end
28   - if options[:execute] == false
29   - query
30   - else
31   - query.execute
  30 +
  31 + def enable_search_callbacks
  32 + class_variable_set :@@searchkick_callbacks, true
  33 + end
  34 +
  35 + def disable_search_callbacks
  36 + class_variable_set :@@searchkick_callbacks, false
  37 + end
  38 +
  39 + def search_callbacks?
  40 + class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks?
  41 + end
  42 +
  43 + def reindex(options = {})
  44 + searchkick_index.reindex_scope(searchkick_klass, options)
  45 + end
  46 +
  47 + def clean_indices
  48 + searchkick_index.clean_indices
  49 + end
  50 +
  51 + def searchkick_import(options = {})
  52 + (options[:index] || searchkick_index).import_scope(searchkick_klass)
  53 + end
  54 +
  55 + def searchkick_create_index
  56 + searchkick_index.create_index
  57 + end
  58 +
  59 + def searchkick_index_options
  60 + searchkick_index.index_options
32 61 end
33   - end
34   - extend Searchkick::Reindex
35   - include Searchkick::Similar
36 62  
37   - def reindex_async
38   - self.class.searchkick_index.reindex_record_async(self)
39 63 end
40 64  
41 65 if callbacks
... ... @@ -48,30 +72,26 @@ module Searchkick
48 72 end
49 73 end
50 74  
51   - def self.enable_search_callbacks
52   - class_variable_set :@@searchkick_callbacks, true
53   - end
54   -
55   - def self.disable_search_callbacks
56   - class_variable_set :@@searchkick_callbacks, false
57   - end
58   -
59   - def self.search_callbacks?
60   - class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks?
  75 + def reindex
  76 + self.class.searchkick_index.reindex_record(self)
61 77 end
62 78  
63   - def should_index?
64   - true
  79 + def reindex_async
  80 + self.class.searchkick_index.reindex_record_async(self)
65 81 end
66 82  
67   - def reindex
68   - self.class.searchkick_index.reindex_record(self)
  83 + def similar(options = {})
  84 + self.class.searchkick_index.similar_record(self, options)
69 85 end
70 86  
71 87 def search_data
72 88 respond_to?(:to_hash) ? to_hash : serializable_hash
73 89 end
74 90  
  91 + def should_index?
  92 + true
  93 + end
  94 +
75 95 end
76 96 end
77 97  
... ...
lib/searchkick/reindex.rb
... ... @@ -1,30 +0,0 @@
1   -module Searchkick
2   - module Reindex
3   -
4   - def self.extended(klass)
5   - @descendents ||= []
6   - @descendents << klass unless @descendents.include?(klass)
7   - end
8   -
9   - def reindex(options = {})
10   - searchkick_index.reindex_scope(searchkick_klass, options)
11   - end
12   -
13   - def clean_indices
14   - searchkick_index.clean_indices
15   - end
16   -
17   - def searchkick_import(options = {})
18   - (options[:index] || searchkick_index).import_scope(searchkick_klass)
19   - end
20   -
21   - def searchkick_create_index
22   - searchkick_index.create_index
23   - end
24   -
25   - def searchkick_index_options
26   - searchkick_index.index_options
27   - end
28   -
29   - end
30   -end
lib/searchkick/similar.rb
... ... @@ -1,19 +0,0 @@
1   -module Searchkick
2   - module Similar
3   -
4   - def similar(options = {})
5   - like_text = self.class.searchkick_index.retrieve(self).to_hash
6   - .keep_if{|k,v| !options[:fields] || options[:fields].map(&:to_s).include?(k) }
7   - .values.compact.join(" ")
8   -
9   - # TODO deep merge method
10   - options[:where] ||= {}
11   - options[:where][:_id] ||= {}
12   - options[:where][:_id][:not] = id.to_s
13   - options[:limit] ||= 10
14   - options[:similar] = true
15   - self.class.send(Searchkick.search_method_name, like_text, options)
16   - end
17   -
18   - end
19   -end
lib/searchkick/tasks.rb
... ... @@ -22,7 +22,7 @@ namespace :searchkick do
22 22 desc "reindex all models"
23 23 task :all => :environment do
24 24 Rails.application.eager_load!
25   - (Searchkick::Reindex.instance_variable_get(:@descendents) || []).each do |model|
  25 + Searchkick.models.each do |model|
26 26 puts "Reindexing #{model.name}..."
27 27 model.reindex
28 28 end
... ...