redis-search 0.4 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -7,7 +7,8 @@ High performance real-time search (Support Chinese), index in Redis for Rails ap
7
7
  * Real-time search
8
8
  * High performance
9
9
  * Segment words search and prefix match search
10
- * Support ActiveRecord, Mongoid and others ORM
10
+ * Support ActiveRecord and Mongoid
11
+ * Sort results by one field
11
12
 
12
13
  ## Requirements
13
14
 
@@ -17,9 +18,9 @@ High performance real-time search (Support Chinese), index in Redis for Rails ap
17
18
 
18
19
  in Rails application Gemfile
19
20
 
20
- gem 'redis','>= 2.1.1'
21
- gem "rmmseg-cpp-huacnlee", "0.2.8"
22
- gem 'redis-search', '0.4'
21
+ gem 'redis','>= 2.1.1'
22
+ gem "rmmseg-cpp-huacnlee", "0.2.8"
23
+ gem 'redis-search', '0.5'
23
24
 
24
25
  install bundlers
25
26
 
@@ -48,11 +49,13 @@ bind Redis::Search callback event, it will to rebuild search indexes when data c
48
49
 
49
50
  field :title
50
51
  field :body
52
+ field :hits
51
53
 
52
54
  belongs_to :user
53
55
  belongs_to :category
54
56
 
55
57
  redis_search_index(:title_field => :title,
58
+ :score_field => :hits,
56
59
  :ext_fields => [:category_name])
57
60
 
58
61
  def category_name
@@ -67,9 +70,11 @@ bind Redis::Search callback event, it will to rebuild search indexes when data c
67
70
  field :name
68
71
  field :tagline
69
72
  field :email
73
+ field :followers_count
70
74
 
71
75
  redis_search_index(:title_field => :name,
72
76
  :prefix_index_enable => true,
77
+ :score_field => :followers_count,
73
78
  :ext_fields => [:email,:tagline])
74
79
  end
75
80
 
@@ -91,6 +96,10 @@ If you are first install it in you old project, or your Redis cache lose, you ca
91
96
 
92
97
  $ rake redis_search:index
93
98
 
99
+ ## Documentation
100
+
101
+ see [Rdoc.info redis-search](http://rubydoc.info/gems/redis-search)
102
+
94
103
  ## Benchmark test
95
104
 
96
105
  * [https://gist.github.com/1150933](https://gist.github.com/1150933)
@@ -4,11 +4,20 @@ class Redis
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
+ # Config redis-search index for Model
8
+ # == Params:
9
+ # title_field Query field for Search
10
+ # prefix_index_enable Is use prefix index search
11
+ # ext_fields What kind fields do you need inlucde to search indexes
12
+ # score_field Give a score for search sort, need Integer value, default is `created_at`
7
13
  def redis_search_index(options = {})
8
14
  title_field = options[:title_field] || :title
9
15
  prefix_index_enable = options[:prefix_index_enable] || false
10
16
  ext_fields = options[:ext_fields] || []
11
-
17
+ score_field = options[:score_field] || :created_at
18
+ # Add score field to ext_fields
19
+ ext_fields << score_field if !ext_fields.include?(score_field)
20
+
12
21
  # store Model name to indexed_models for Rake tasks
13
22
  Search.indexed_models = [] if Search.indexed_models == nil
14
23
  Search.indexed_models << self
@@ -28,6 +37,7 @@ class Redis
28
37
  :id => self.id,
29
38
  :exts => self.redis_search_ext_fields(#{ext_fields.inspect}),
30
39
  :type => self.class.to_s,
40
+ :score => self.#{score_field}.to_i,
31
41
  :prefix_index_enable => #{prefix_index_enable})
32
42
  s.save
33
43
  # release s
@@ -44,7 +54,12 @@ class Redis
44
54
  index_fields_changed = false
45
55
  #{ext_fields.inspect}.each do |f|
46
56
  next if f.to_s == "id"
47
- if instance_eval(f.to_s + "_changed?")
57
+ field_method = f.to_s + "_changed?"
58
+ if !self.methods.include?(field_method.to_sym)
59
+ ::Rails.logger.debug("[redis-search] warring! #{self.class.name} model reindex on update need "+field_method+" method.")
60
+ next
61
+ end
62
+ if instance_eval(field_method)
48
63
  index_fields_changed = true
49
64
  end
50
65
  end
@@ -21,6 +21,10 @@ class Redis
21
21
  def self.mk_sets_key(type, key)
22
22
  "#{type}:#{key.downcase}"
23
23
  end
24
+
25
+ def self.mk_score_key(type, id)
26
+ "#{type}:_score_:#{id}"
27
+ end
24
28
 
25
29
  def self.mk_complete_key(type)
26
30
  "Compl#{type}"
@@ -77,9 +81,15 @@ class Redis
77
81
  Redis::Search.config.redis.expire(temp_store_key,86400)
78
82
  end
79
83
  # 根据需要的数量取出 ids
80
- ids = Redis::Search.config.redis.sort(temp_store_key,:limit => [0,limit])
84
+ ids = Redis::Search.config.redis.sort(temp_store_key,
85
+ :limit => [0,limit],
86
+ :by => Search.mk_score_key(type,"*"),
87
+ :order => "desc")
81
88
  else
82
- ids = Redis::Search.config.redis.sort(words.first,:limit => [0,limit])
89
+ ids = Redis::Search.config.redis.sort(words.first,
90
+ :limit => [0,limit],
91
+ :by => Search.mk_score_key(type,"*"),
92
+ :order => "desc")
83
93
  end
84
94
  return [] if ids.blank?
85
95
  hmget(type,ids)
@@ -101,6 +111,7 @@ class Redis
101
111
  limit = options[:limit] || 10
102
112
  sort_field = options[:sort_field] || "id"
103
113
  words = words.collect { |w| Search.mk_sets_key(type,w) }
114
+ word_score = words.collect { |w| "#{w}:*" }
104
115
  return result if words.blank?
105
116
  temp_store_key = "tmpinterstore:#{words.join("+")}"
106
117
  if words.length > 1
@@ -111,10 +122,16 @@ class Redis
111
122
  Redis::Search.config.redis.expire(temp_store_key,86400)
112
123
  end
113
124
  # 根据需要的数量取出 ids
114
- ids = Redis::Search.config.redis.sort(temp_store_key,:limit => [0,limit])
125
+ ids = Redis::Search.config.redis.sort(temp_store_key,
126
+ :limit => [0,limit],
127
+ :by => Search.mk_score_key(type,"*"),
128
+ :order => "desc")
115
129
  else
116
130
  # 根据需要的数量取出 ids
117
- ids = Redis::Search.config.redis.sort(words.first,:limit => [0,limit])
131
+ ids = Redis::Search.config.redis.sort(words.first,
132
+ :limit => [0,limit],
133
+ :by => Search.mk_score_key(type,"*"),
134
+ :order => "desc")
118
135
  end
119
136
  hmget(type,ids, :sort_field => sort_field)
120
137
  end
@@ -1,7 +1,7 @@
1
1
  class Redis
2
2
  module Search
3
3
  class Index
4
- attr_accessor :type, :title, :id, :exts, :prefix_index_enable
4
+ attr_accessor :type, :title, :id,:score, :exts, :prefix_index_enable
5
5
  def initialize(options = {})
6
6
  self.exts = []
7
7
  options.keys.each do |k|
@@ -24,6 +24,7 @@ class Redis
24
24
  words.each do |word|
25
25
  key = Search.mk_sets_key(self.type,word)
26
26
  Redis::Search.config.redis.sadd(key, self.id)
27
+ Redis::Search.config.redis.set(Search.mk_score_key(self.type,self.id),self.score)
27
28
  end
28
29
 
29
30
  # 建立前最索引
@@ -50,6 +51,7 @@ class Redis
50
51
  words.each do |word|
51
52
  key = Search.mk_sets_key(type,word)
52
53
  Redis::Search.config.redis.srem(key, options[:id])
54
+ Redis::Search.config.redis.del(Search.mk_score_key(type,options[:id]))
53
55
  end
54
56
  end
55
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-search
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-22 00:00:00.000000000Z
12
+ date: 2011-09-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rmmseg-cpp-huacnlee
16
- requirement: &2153202040 !ruby/object:Gem::Requirement
16
+ requirement: &2151875920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.2.8
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153202040
24
+ version_requirements: *2151875920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: redis
27
- requirement: &2153198300 !ruby/object:Gem::Requirement
27
+ requirement: &2151873980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.1.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153198300
35
+ version_requirements: *2151873980
36
36
  description: High performance real-time search (Support Chinese), index in Redis for
37
37
  Rails application.
38
38
  email: