From af170b19c2e7c6e91c7277e646c25b29408bd822 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 26 Nov 2014 03:23:14 -0800 Subject: [PATCH] Much better organization --- lib/searchkick.rb | 4 ++-- lib/searchkick/index.rb | 28 ++++++++++++++++++++++++++++ lib/searchkick/model.rb | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- lib/searchkick/reindex.rb | 30 ------------------------------ lib/searchkick/similar.rb | 19 ------------------- lib/searchkick/tasks.rb | 2 +- 6 files changed, 82 insertions(+), 83 deletions(-) delete mode 100644 lib/searchkick/reindex.rb delete mode 100644 lib/searchkick/similar.rb diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 80e65ef..6290a94 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -3,10 +3,8 @@ require "elasticsearch" require "hashie" require "searchkick/version" require "searchkick/index" -require "searchkick/reindex" require "searchkick/results" require "searchkick/query" -require "searchkick/similar" require "searchkick/reindex_job" require "searchkick/model" require "searchkick/tasks" @@ -30,11 +28,13 @@ module Searchkick attr_accessor :search_method_name attr_accessor :wordnet_path attr_accessor :timeout + attr_accessor :models end self.callbacks = true self.search_method_name = :search self.wordnet_path = "/var/lib/wn_s.pl" self.timeout = 10 + self.models = [] def self.client @client ||= diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index dd4a1db..a28da28 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -27,6 +27,34 @@ module Searchkick client.indices.exists_alias name: name end + def search_model(searchkick_klass, term = nil, options = {}, &block) + query = Searchkick::Query.new(searchkick_klass, term, options) + if block + block.call(query.body) + end + if options[:execute] == false + query + else + query.execute + end + end + + def similar_record(record, options = {}) + like_text = retrieve(record).to_hash + .keep_if{|k,v| !options[:fields] || options[:fields].map(&:to_s).include?(k) } + .values.compact.join(" ") + + # TODO deep merge method + options[:where] ||= {} + options[:where][:_id] ||= {} + options[:where][:_id][:not] = record.id.to_s + options[:limit] ||= 10 + options[:similar] = true + + # TODO use index class instead of record class + search_model(record.class, like_text, options) + end + def swap(new_name) old_indices = begin diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 16c2d1c..e64ca73 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -4,6 +4,8 @@ module Searchkick def searchkick(options = {}) raise "Only call searchkick once per model" if respond_to?(:searchkick_index) + Searchkick.models << self + class_eval do cattr_reader :searchkick_options, :searchkick_klass @@ -14,28 +16,50 @@ module Searchkick class_variable_set :@@searchkick_callbacks, callbacks class_variable_set :@@searchkick_index, options[:index_name] || [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") - def self.searchkick_index - index = class_variable_get :@@searchkick_index - index = index.call if index.respond_to? :call - Searchkick::Index.new(index, class_variable_get(:@@searchkick_options)) + define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}, &block| + searchkick_index.search_model(self, term, options, &block) end - define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}, &block| - query = Searchkick::Query.new(self, term, options) - if block - block.call(query.body) + class << self + + def searchkick_index + index = class_variable_get :@@searchkick_index + index = index.call if index.respond_to? :call + Searchkick::Index.new(index, class_variable_get(:@@searchkick_options)) end - if options[:execute] == false - query - else - query.execute + + def enable_search_callbacks + class_variable_set :@@searchkick_callbacks, true + end + + def disable_search_callbacks + class_variable_set :@@searchkick_callbacks, false + end + + def search_callbacks? + class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks? + end + + def reindex(options = {}) + searchkick_index.reindex_scope(searchkick_klass, options) + end + + def clean_indices + searchkick_index.clean_indices + end + + def searchkick_import(options = {}) + (options[:index] || searchkick_index).import_scope(searchkick_klass) + end + + def searchkick_create_index + searchkick_index.create_index + end + + def searchkick_index_options + searchkick_index.index_options end - end - extend Searchkick::Reindex - include Searchkick::Similar - def reindex_async - self.class.searchkick_index.reindex_record_async(self) end if callbacks @@ -48,30 +72,26 @@ module Searchkick end end - def self.enable_search_callbacks - class_variable_set :@@searchkick_callbacks, true - end - - def self.disable_search_callbacks - class_variable_set :@@searchkick_callbacks, false - end - - def self.search_callbacks? - class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks? + def reindex + self.class.searchkick_index.reindex_record(self) end - def should_index? - true + def reindex_async + self.class.searchkick_index.reindex_record_async(self) end - def reindex - self.class.searchkick_index.reindex_record(self) + def similar(options = {}) + self.class.searchkick_index.similar_record(self, options) end def search_data respond_to?(:to_hash) ? to_hash : serializable_hash end + def should_index? + true + end + end end diff --git a/lib/searchkick/reindex.rb b/lib/searchkick/reindex.rb deleted file mode 100644 index 717fd62..0000000 --- a/lib/searchkick/reindex.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Searchkick - module Reindex - - def self.extended(klass) - @descendents ||= [] - @descendents << klass unless @descendents.include?(klass) - end - - def reindex(options = {}) - searchkick_index.reindex_scope(searchkick_klass, options) - end - - def clean_indices - searchkick_index.clean_indices - end - - def searchkick_import(options = {}) - (options[:index] || searchkick_index).import_scope(searchkick_klass) - end - - def searchkick_create_index - searchkick_index.create_index - end - - def searchkick_index_options - searchkick_index.index_options - end - - end -end diff --git a/lib/searchkick/similar.rb b/lib/searchkick/similar.rb deleted file mode 100644 index 0ccb99d..0000000 --- a/lib/searchkick/similar.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Searchkick - module Similar - - def similar(options = {}) - like_text = self.class.searchkick_index.retrieve(self).to_hash - .keep_if{|k,v| !options[:fields] || options[:fields].map(&:to_s).include?(k) } - .values.compact.join(" ") - - # TODO deep merge method - options[:where] ||= {} - options[:where][:_id] ||= {} - options[:where][:_id][:not] = id.to_s - options[:limit] ||= 10 - options[:similar] = true - self.class.send(Searchkick.search_method_name, like_text, options) - end - - end -end diff --git a/lib/searchkick/tasks.rb b/lib/searchkick/tasks.rb index 66f7561..ffabbce 100644 --- a/lib/searchkick/tasks.rb +++ b/lib/searchkick/tasks.rb @@ -22,7 +22,7 @@ namespace :searchkick do desc "reindex all models" task :all => :environment do Rails.application.eager_load! - (Searchkick::Reindex.instance_variable_get(:@descendents) || []).each do |model| + Searchkick.models.each do |model| puts "Reindexing #{model.name}..." model.reindex end -- libgit2 0.21.0