yoomee-mongosphinx 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mixins/indexer.rb +53 -14
  2. metadata +4 -4
@@ -101,7 +101,10 @@ module MongoMapper # :nodoc:
101
101
  # gem.
102
102
 
103
103
  cattr_accessor :fulltext_keys
104
- cattr_accessor :fulltext_opts
104
+ cattr_accessor :fulltext_opts
105
+ cattr_accessor :has_delta_index
106
+
107
+ self.has_delta_index = opts[:delta] || false
105
108
 
106
109
  self.fulltext_keys = keys
107
110
  self.fulltext_opts = opts
@@ -111,8 +114,40 @@ module MongoMapper # :nodoc:
111
114
  # Schema and cowardly ignore if not.
112
115
 
113
116
  before_save :save_callback
117
+ before_create :increment_sphinx_id
118
+
119
+ key :sphinx_id, Integer
120
+ alias_attribute :_sphinx_id, :sphinx_id
121
+
122
+ if opts[:delta]
123
+ before_save :set_delta
124
+ after_save :rebuild_delta_index
125
+ key :delta, Boolean, :default => true
126
+ define_method(:rebuild_delta_index) do
127
+ Rails.logger.info("rebuild_delta_index = #{self.class.to_s.underscore}_delta")
128
+ Process.fork {`rake mongosphinx:rebuild index=#{self.class.to_s.underscore}_delta`}
129
+ end
130
+ define_method(:set_delta) do
131
+ self.delta = true
132
+ end
133
+ end
134
+
135
+ (class << self; self; end).module_eval do
136
+ define_method :xml_for_sphinx_core do
137
+ puts MongoSphinx::Indexer::XMLDocset.new(self.all(:fields => keys << "sphinx_id")).to_s
138
+ end
139
+ if opts[:delta]
140
+ define_method :xml_for_sphinx_delta do
141
+ puts MongoSphinx::Indexer::XMLDocset.new(self.all(:fields => keys << "sphinx_id", :delta => true)).to_s
142
+ end
143
+ end
144
+ end
114
145
 
115
- end
146
+ define_method(:increment_sphinx_id) do
147
+ self.sphinx_id = ((last_id = self.class.sort(:sphinx_id).last.try(:sphinx_id)) ? last_id + 1 : 1)
148
+ end
149
+
150
+ end
116
151
 
117
152
  # Searches for an object of this model class (e.g. Post, Comment) and
118
153
  # the requested query string. The query string may contain any query
@@ -142,7 +177,7 @@ module MongoMapper # :nodoc:
142
177
  fulltext_opts[:port])
143
178
  query = query + " @classname #{self}"
144
179
  end
145
-
180
+
146
181
  client.match_mode = options[:match_mode] || :extended
147
182
 
148
183
  if (limit = options[:limit])
@@ -157,20 +192,24 @@ module MongoMapper # :nodoc:
157
192
  client.sort_mode = :extended
158
193
  client.sort_by = sort_by
159
194
  end
195
+
196
+ index_names = "*"
197
+ debugger
198
+ if self != Document
199
+ index_names = "#{self.to_s.underscore}_core"
200
+ index_names += " #{self.to_s.underscore}_delta" if self.has_delta_index
201
+ end
202
+ debugger
203
+ result = client.query(query, index_names)
160
204
 
161
- result = client.query(query)
162
-
163
- #TODO
164
- if result and result[:status] == 0 and !(matches = result[:matches]).empty?
165
- classname = nil
166
- ids = matches.collect do |row|
167
- classname = MongoSphinx::MultiAttribute.decode(row[:attributes]['csphinx-class'])
168
- (row[:doc].to_i) rescue nil
205
+ if result and result[:status] == 0 and !(sphinx_matches = result[:matches]).empty?
206
+ matches = sphinx_matches.collect do |row|
207
+ {:classname => MongoSphinx::MultiAttribute.decode(row[:attributes]['csphinx-class']), :id => (row[:doc].to_i)} rescue nil
169
208
  end.compact
170
- return ids if options[:raw]
209
+ return matches if options[:raw]
171
210
  objects = []
172
- ids.each do |id|
173
- objects << self.find_by_sphinx_id(id)
211
+ matches.each do |match|
212
+ objects << match[:classname].constantize.find_by_sphinx_id(match[:id])
174
213
  end
175
214
  return objects
176
215
  else
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yoomee-mongosphinx
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Atkins
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2010-08-13 00:00:00 +01:00
21
+ date: 2010-08-16 00:00:00 +01:00
22
22
  default_executable:
23
23
  dependencies: []
24
24