This repository has been archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename RedisSearch -> Redis::Search, see the documents.
- Loading branch information
Showing
13 changed files
with
206 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
require "redis_search" | ||
# coding: utf-8 | ||
require "redis/search/base" | ||
require "redis/search/finder" | ||
require "redis/search/index" | ||
require "redis/search/config" | ||
require 'redis/search/railtie' if defined?(Rails) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# coding: utf-8 | ||
class Redis | ||
module Search | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
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 = {} | ||
ext_fields.each do |f| | ||
exts[f] = instance_eval(f.to_s) | ||
end | ||
exts | ||
end | ||
after_create :redis_search_index_create | ||
def redis_search_index_create | ||
s = Search::Index.new(:title => self.#{title_field}, | ||
:id => self.id, | ||
:exts => self.redis_search_ext_fields(#{ext_fields.inspect}), | ||
:type => self.class.to_s, | ||
:prefix_index_enable => #{prefix_index_enable}) | ||
s.save | ||
# release s | ||
s = nil | ||
end | ||
before_destroy :redis_search_index_remove | ||
def redis_search_index_remove | ||
Search::Index.remove(:id => self.id, :title => self.#{title_field}, :type => self.class.to_s) | ||
end | ||
before_update :redis_search_index_update | ||
def redis_search_index_update | ||
index_fields_changed = false | ||
#{ext_fields.inspect}.each do |f| | ||
next if f.to_s == "id" | ||
if instance_eval(f.to_s + "_changed?") | ||
index_fields_changed = true | ||
end | ||
end | ||
begin | ||
if(self.#{title_field}_changed?) | ||
index_fields_changed = true | ||
end | ||
rescue | ||
end | ||
if index_fields_changed | ||
Search::Index.remove(:id => self.id, :title => self.#{title_field}_was, :type => self.class.to_s) | ||
self.redis_search_index_create | ||
end | ||
end | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# coding: utf-8 | ||
class Redis | ||
module Search | ||
class << self | ||
attr_accessor :config, :indexed_models | ||
|
||
def configure | ||
yield self.config ||= Config.new | ||
end | ||
end | ||
|
||
class Config | ||
# Redis | ||
attr_accessor :redis | ||
# Debug toggle | ||
attr_accessor :debug | ||
# config for max length of content with Redis::Search.complete method,default 100 | ||
# Please change this with your real data length, short is fast | ||
# For example: You use complete search for your User model name field, and the "name" as max length in 15 chars, then you can set here to 15 | ||
# warring! The long content will can't be found, if the config length less than real content. | ||
attr_accessor :complete_max_length | ||
|
||
def initialize | ||
self.debug = false | ||
self.redis = nil | ||
self.complete_max_length = 100 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.