From d146b9c39ec9bd42493af8e5bbc2bac0ee6b1dec Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 14 Jul 2013 18:44:55 -0700 Subject: [PATCH] First commit --- .gitignore | 17 +++++++++++++++++ Gemfile | 4 ++++ LICENSE.txt | 22 ++++++++++++++++++++++ README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Rakefile | 1 + lib/searchkick.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/searchkick/tasks.rb | 9 +++++++++ lib/searchkick/version.rb | 3 +++ searchkick.gemspec | 25 +++++++++++++++++++++++++ 9 files changed, 185 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/searchkick.rb create mode 100644 lib/searchkick/tasks.rb create mode 100644 lib/searchkick/version.rb create mode 100644 searchkick.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f712136 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in searchkick.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e2daad2 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2013 Andrew Kane + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fed3bc6 --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +# Searchkick + +Search made easy + +## Usage + +### Reindex with Zero Downtime + +Elasticsearch has a feature called aliases that allows you to reindex with no downtime. + +```ruby +Book.tire.reindex +``` + +This creates a new index `books_20130714181054` and points the `books` alias to the new index when complete - an atomic operation :) + +**First time:** If books is an existing index, it will be replaced by an alias. + +Searchkick uses `find_in_batches` to import documents. To filter documents or eagar load associations, use the `tire_import` scope. + +```ruby +class Book < ActiveRecord::Base + scope :tire_import, where(active: true).includes(:author, :chapters) +end +``` + +There is also a rake task. + +```sh +rake searchkick:reindex CLASS=Book +``` + +[Thanks to Jaroslav Kalistsuk for the original source](https://gist.github.com/jarosan/3124884) + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem "searchkick" +``` + +And then execute: + +```sh +bundle +``` + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/lib/searchkick.rb b/lib/searchkick.rb new file mode 100644 index 0000000..c684e9e --- /dev/null +++ b/lib/searchkick.rb @@ -0,0 +1,49 @@ +require "searchkick/version" +require "searchkick/tasks" +require "tire" + +module Searchkick + module ClassMethods + + # https://gist.github.com/jarosan/3124884 + def reindex + alias_name = klass.tire.index.name + new_index = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S") + + # Rake::Task["tire:import"].invoke + index = Tire::Index.new(new_index) + Tire::Tasks::Import.create_index(index, klass) + scope = klass.respond_to?(:tire_import) ? klass.tire_import : klass + scope.find_in_batches do |batch| + index.import batch + end + + if a = Tire::Alias.find(alias_name) + puts "[IMPORT] Alias found: #{Tire::Alias.find(alias_name).indices.to_ary.join(",")}" + old_indices = Tire::Alias.find(alias_name).indices + old_indices.each do |index| + a.indices.delete index + end + + a.indices.add new_index + a.save + + old_indices.each do |index| + puts "[IMPORT] Deleting index: #{index}" + i = Tire::Index.new(index) + i.delete if i.exists? + end + else + puts "[IMPORT] No alias found. Deleting index, creating new one, and setting up alias" + i = Tire::Index.new(alias_name) + i.delete if i.exists? + Tire::Alias.create(name: alias_name, indices: [new_index]) + end + + puts "[IMPORT] Saved alias #{alias_name} pointing to #{new_index}" + end + + end +end + +Tire::Model::Search::ClassMethodsProxy.send :include, Searchkick::ClassMethods diff --git a/lib/searchkick/tasks.rb b/lib/searchkick/tasks.rb new file mode 100644 index 0000000..629779d --- /dev/null +++ b/lib/searchkick/tasks.rb @@ -0,0 +1,9 @@ +require "rake" + +namespace :searchkick do + desc "re-index elasticsearch" + task :reindex => :environment do + klass = ENV["CLASS"].constantize + klass.tire.reindex + end +end diff --git a/lib/searchkick/version.rb b/lib/searchkick/version.rb new file mode 100644 index 0000000..f933120 --- /dev/null +++ b/lib/searchkick/version.rb @@ -0,0 +1,3 @@ +module Searchkick + VERSION = "0.0.1" +end diff --git a/searchkick.gemspec b/searchkick.gemspec new file mode 100644 index 0000000..0707231 --- /dev/null +++ b/searchkick.gemspec @@ -0,0 +1,25 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'searchkick/version' + +Gem::Specification.new do |spec| + spec.name = "searchkick" + spec.version = Searchkick::VERSION + spec.authors = ["Andrew Kane"] + spec.email = ["andrew@chartkick.com"] + spec.description = %q{Search made easy} + spec.summary = %q{Search made easy} + spec.homepage = "https://github.com/ankane/searchkick" + spec.license = "MIT" + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_dependency "tire" + + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "rake" +end -- libgit2 0.21.0