Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
Add rake tasks to build Redis indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Sep 22, 2011
1 parent 43e68e0 commit d7f4cde
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/redis-search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "redis_search"
9 changes: 1 addition & 8 deletions lib/redis_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,4 @@
require "redis_search/base"
require "redis_search/search"
require "redis_search/config"

module RedisSearch
class << self
def configure
yield self.config ||= Config.new
end
end
end
require 'redis_search/railtie' if defined?(Rails)
5 changes: 5 additions & 0 deletions lib/redis_search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def redis_search_index(options = {})
title_field = options[:title_field] || :title
prefix_index_enable = options[:prefix_index_enable] || false
ext_fields = options[:ext_fields] || []

# store Model name to indexed_models for Rake tasks
Search.indexed_models = [] if Search.indexed_models == nil
Search.indexed_models << self
# bind instance methods and callback events
class_eval %(
def redis_search_ext_fields(ext_fields)
exts = {}
Expand Down
4 changes: 4 additions & 0 deletions lib/redis_search/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
module RedisSearch
class << self
attr_accessor :config

def configure
yield self.config ||= Config.new
end
end

class Config
Expand Down
7 changes: 7 additions & 0 deletions lib/redis_search/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module RedisSearch
class Railtie < Rails::Railtie
rake_tasks do
load File.expand_path('../tasks.rb', __FILE__)
end
end
end
4 changes: 4 additions & 0 deletions lib/redis_search/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
require "rmmseg"
module RedisSearch
class Search
class << self
attr_accessor :indexed_models
end

attr_accessor :type, :title, :id, :exts, :prefix_index_enable
def initialize(options = {})
self.exts = []
Expand Down
28 changes: 28 additions & 0 deletions lib/redis_search/tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# coding: utf-8
namespace :redis_search do
desc "RedisSearch index data to Redis"
task :index => :environment do
tm = Time.now
count = 0
puts "redis-search index".upcase.rjust(120)
puts "-"*120
puts "Now indexing search to Redis...".rjust(120)
puts ""
RedisSearch::Search.indexed_models.each do |klass|
print "[#{klass.to_s}]"
klass.find_in_batches(:batch_size => 1000) do |items|
items.each do |item|
item.redis_search_index_create
item = nil
count += 1
print "."
end
end
puts ""
end
puts ""
puts "-"*120
puts "Indexed #{count} rows | Time spend: #{(Time.now - tm)}s".rjust(120)
puts "Rebuild Index done.".rjust(120)
end
end
2 changes: 1 addition & 1 deletion redis-search.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "redis-search"
s.version = "0.3.3"
s.version = "0.3.4"
s.platform = Gem::Platform::RUBY
s.authors = ["Jason Lee"]
s.email = ["huacnlee@gmail.com"]
Expand Down

0 comments on commit d7f4cde

Please sign in to comment.