Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 23, 2014
@huacnlee huacnlee Merge pull request #33 from tumayun/master
修改 tasks.rb, 判断是否继承自 ActiveRecord::Base, 应该用 klass < ActiveRecord::Base
f58fb3c
@huacnlee huacnlee # coding: utf-8 for all of the ruby files. 7e94383
Commits on Apr 24, 2014
tumayun 重构 tasks 4cda21c
tumayun Merge branch 'master' of github.com:tumayun/redis-search 123df19
tumayun update README.md 1fd4ebf
Showing with 131 additions and 37 deletions.
  1. +23 −2 README.md
  2. +25 −1 lib/redis/search/base.rb
  3. +1 −0 lib/redis/search/index.rb
  4. +1 −0 lib/redis/search/railtie.rb
  5. +65 −34 lib/redis/search/tasks.rb
  6. +16 −0 spec/base_spec.rb
View
25 README.md
@@ -130,12 +130,33 @@ And there is an [Example App](https://github.com/huacnlee/redis-search-example)
## Index data to Redis
-If you are first install it in you old project, or your Redis cache lose, you can use this command to rebuild indices.
+### Specify Model
+
+Redis-Search index data to Redis from your model (pass name as CLASS environment variable).
```bash
-$ rake redis_search:index
+$ rake environment redis_search:index:model CLASS='MyModel'
```
+Customize the batch size:
+
+```bash
+$ rake environment redis_search:index:model CLASS='MyModel' BATCH=100
+```
+
+### All Models
+
+Redis-Search all index data to Redis from `app/models` (or use DIR environment variabl).
+
+```bash
+$ rake environment redis_search:index DIR=app/models
+```
+
+Customize the batch size:
+
+```bash
+$ rake environment redis_search:index DIR=app/models BATCH=100
+```
## Documentation
View
26 lib/redis/search/base.rb
@@ -1,5 +1,4 @@
# coding: utf-8
-
class Redis
module Search
autoload :PinYin, 'ruby-pinyin'
@@ -130,6 +129,31 @@ def redis_search_index_update
end
)
end
+
+ def redis_search_index_batch_create(batch_size = 1000, progressbar = false)
+ count = 0
+ if self.ancestors.collect { |klass| klass.to_s }.include?("ActiveRecord::Base")
+ find_in_batches(:batch_size => batch_size) do |items|
+ items.each do |item|
+ item.redis_search_index_create
+ count += 1
+ print "." if progressbar
+ end
+ end
+ elsif self.included_modules.collect { |m| m.to_s }.include?("Mongoid::Document")
+ all.each_slice(batch_size) do |items|
+ items.each do |item|
+ item.redis_search_index_create
+ count += 1
+ print "." if progressbar
+ end
+ end
+ else
+ puts "skiped, not support this ORM in current."
+ end
+
+ count
+ end
end
end
end
View
1 lib/redis/search/index.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
class Redis
module Search
class Index
View
1 lib/redis/search/railtie.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
class Redis
module Search
class Railtie < ::Rails::Railtie
View
99 lib/redis/search/tasks.rb
@@ -1,42 +1,73 @@
# coding: utf-8
require "redis-search"
namespace :redis_search do
- desc "Redis-Search 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 ""
- Redis::Search.indexed_models.each do |klass|
- print "[#{klass.to_s}]"
- if klass < ActiveRecord::Base
- 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
- elsif klass.included_modules.collect { |m| m.to_s }.include?("Mongoid::Document")
- klass.all.each_slice(1000) do |items|
- items.each do |item|
- item.redis_search_index_create
- item = nil
- count += 1
- print "."
- end
- end
- else
- puts "skiped, not support this ORM in current."
+ task :index => 'index:all'
+
+ namespace :index do
+ index_model_desc = <<-DESC.gsub(/ /, '')
+ Redis-Search index data to Redis from your model (pass name as CLASS environment variable).
+
+ $ rake environment redis_search:index:model CLASS='MyModel'
+
+ Customize the batch size:
+
+ $ rake environment redis_search:index:model CLASS='Article' BATCH=100
+ DESC
+
+ index_all_desc = <<-DESC.gsub(/ /, '')
+ Redis-Search all index data to Redis from `app/models` (or use DIR environment variabl).
+
+ $ rake environment redis_search:index:all DIR=app/models
+
+ Customize the batch size:
+
+ $ rake environment redis_search:index:all DIR=app/models BATCH=100
+ DESC
+
+ desc index_model_desc
+ task :model => :environment do
+ if ENV['CLASS'].to_s == ''
+ puts '='*90, 'USAGE', '='*90, index_model_desc, ""
+ exit(1)
end
+
+ klass = eval(ENV['CLASS'].to_s)
+ batch = ENV['BATCH'].to_i > 0 ? ENV['BATCH'].to_i : 1000
+ tm = Time.now
+ puts "Redis-Search index data to Redis from [#{klass.to_s}]"
+ count = klass.redis_search_index_batch_create(batch, true)
puts ""
+ puts "Indexed #{count} rows | Time spend: #{(Time.now - tm)}s"
+ puts "Rebuild Index done."
+ end
+
+ desc index_all_desc
+ task :all => :environment do
+ tm = Time.now
+ count = 0
+ dir = ENV['DIR'].to_s != '' ? ENV['DIR'] : 'app/models'
+ batch = ENV['BATCH'].to_i > 0 ? ENV['BATCH'].to_i : 1000
+
+ Dir.glob(File.join("#{dir}/**/*.rb")).each do |path|
+ model_filename = path[/#{Regexp.escape(dir.to_s)}\/([^\.]+).rb/, 1]
+
+ next if model_filename.match(/^concerns\//i) # Skip concerns/ folder
+
+ begin
+ klass = model_filename.camelize.constantize
+ rescue NameError
+ require(path) ? retry : raise(RuntimeError, "Cannot load class '#{klass}'")
+ end
+ end
+
+ puts "Redis-Search index data to Redis from [#{dir}]"
+ Redis::Search.indexed_models.each do |klass|
+ puts "[#{klass.to_s}]"
+ count += klass.redis_search_index_batch_create(batch, true)
+ puts ""
+ end
+ puts "Indexed #{count} rows | Time spend: #{(Time.now - tm)}s"
+ puts "Rebuild Index done."
end
- puts ""
- puts "-"*120
- puts "Indexed #{count} rows | Time spend: #{(Time.now - tm)}s".rjust(120)
- puts "Rebuild Index done.".rjust(120)
end
end
View
16 spec/base_spec.rb
@@ -27,4 +27,20 @@
Redis::Search.should respond_to(:split)
end
end
+
+ describe 'Redis::Search ClassMethods module' do
+ before :all do
+ @user1 = User.create(:email => "zsf@gmail.com", :sex => 1, :name => "张三丰", :alias => ["张三疯","张麻子"], :score => 100, :password => "123456")
+ @user2 = User.create(:email => "liubei@gmail.com", :sex => 2, :name => "刘备", :score => 200, :password => "abcd")
+ @user3 = User.create(:email => "zicheng.lhs@taobao.com", :sex => 1, :name => "李自成", :score => 20, :password => "dsad")
+ end
+
+ after :all do
+ User.destroy_all
+ end
+
+ it 'User.redis_search_index_batch_create should return indices size' do
+ User.count.should == User.redis_search_index_batch_create(1000, true)
+ end
+ end
end

No commit comments for this range

Something went wrong with that request. Please try again.