Changeset 267
- Timestamp:
- 11/17/07 16:08:44 (1 year ago)
- Files:
-
- trunk/plugin/acts_as_ferret/lib/class_methods.rb (modified) (7 diffs)
- trunk/plugin/acts_as_ferret/lib/local_index.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plugin/acts_as_ferret/lib/class_methods.rb
r254 r267 158 158 limit = options[:per_page] 159 159 offset = (options[:page] - 1) * limit 160 if find_options[:conditions] 160 if find_options[:conditions] && !options[:multi] 161 161 find_options[:limit] = limit 162 162 find_options[:offset] = offset … … 164 164 options.delete :offset 165 165 else 166 # do pagination with ferret 166 # do pagination with ferret (or after everything is done in the case 167 # of multi_search) 167 168 options[:limit] = limit 168 169 options[:offset] = offset 170 end 171 elsif find_options[:conditions] 172 if options[:multi] 173 # multisearch ignores find_options limit and offset 174 options[:limit] ||= find_options.delete(:limit) 175 options[:offset] ||= find_options.delete(:offset) 176 else 177 # let the db do the limiting and offsetting for single-table searches 178 find_options[:limit] ||= options.delete(:limit) 179 find_options[:offset] ||= options.delete(:offset) 180 options[:limit] = :all 169 181 end 170 182 end … … 185 197 # To count the results of a query across multiple models, specify an array of 186 198 # class names with the :multi option. 199 # 200 # Note that since we don't query the database here, this method won't deliver 201 # the expected results when used on an AR association. 187 202 def total_hits(q, options={}) 188 203 if options[:models] … … 216 231 end 217 232 218 # Search across multiple classes each having their own index.219 # requires the store_class_name option of acts_as_ferret to be true220 # for all models queried this way.221 #222 # Pagination with the +page+ and +per_page+ options is not supported when223 # combined with active_record +conditions+ in +find_options+.224 def multi_search(query, additional_models = [], options = {}, find_options = {})225 logger.warn "multi_search is deprecated. Use find_with_ferret with the :multi option instead."226 total_hits, result = _multi_search query, additional_models, options, find_options227 SearchResults.new(result, total_hits)228 end229 230 def _multi_search(query, additional_models = [], options = {}, find_options = {})231 result = []232 233 if options[:lazy]234 logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty?235 total_hits = id_multi_search(query, additional_models, options) do |model, id, score, data|236 result << FerretResult.new(model, id, score, data)237 end238 else239 id_arrays = {}240 rank = 0241 total_hits = id_multi_search(query, additional_models, options) do |model, id, score, data|242 id_arrays[model] ||= {}243 id_arrays[model][id] = [ rank += 1, score ]244 end245 limit, offset = [nil, nil]246 if options[:per_page]247 limit = find_options.delete :limit248 offset = find_options.delete :offset249 end250 result = retrieve_records(id_arrays, find_options)251 total_hits = result.size if find_options[:conditions]252 if limit && offset253 result = result[offset..limit+offset-1]254 end255 end256 [total_hits, result]257 end258 233 259 234 # returns an array of hashes, each containing :class_name, … … 271 246 protected 272 247 248 def _multi_search(query, additional_models = [], options = {}, find_options = {}) 249 result = [] 250 251 if options[:lazy] 252 logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? 253 total_hits = id_multi_search(query, additional_models, options) do |model, id, score, data| 254 result << FerretResult.new(model, id, score, data) 255 end 256 else 257 id_arrays = {} 258 rank = 0 259 260 limit = options.delete(:limit) 261 offset = options.delete(:offset) || 0 262 options[:limit] = :all 263 total_hits = id_multi_search(query, additional_models, options) do |model, id, score, data| 264 id_arrays[model] ||= {} 265 id_arrays[model][id] = [ rank += 1, score ] 266 end 267 result = retrieve_records(id_arrays, find_options) 268 total_hits = result.size if find_options[:conditions] 269 # total_hits += offset if offset 270 if limit && limit != :all 271 result = result[offset..limit+offset-1] 272 end 273 end 274 [total_hits, result] 275 end 276 273 277 def add_self_to_model_list_if_necessary(models) 274 278 models = [ models ] unless models.is_a? Array … … 289 293 result_ids = {} 290 294 total_hits = find_id_by_contents(q, options) do |model, id, score, data| 291 # stores ids, index of each idfor later ordering of292 # results , and score295 # stores ids, index and score of each hit for later ordering of 296 # results 293 297 result_ids[id] = [ result_ids.size + 1, score ] 294 298 end … … 296 300 result = retrieve_records( { self.name => result_ids }, find_options ) 297 301 298 if find_options[:conditions] 302 # count total_hits via sql when using conditions or when we're called 303 # from an ActiveRecord association. 304 if find_options[:conditions] or caller.find{ |call| call =~ %r{active_record/associations} } 299 305 # chances are the ferret result count is not our total_hits value, so 300 306 # we correct this here. 301 if options[:limit] != :all || options[:page] || options[:offset] 307 if options[:limit] != :all || options[:page] || options[:offset] || find_options[:limit] || find_options[:offset] 302 308 # our ferret result has been limited, so we need to re-run that 303 309 # search to get the full result set from ferret. trunk/plugin/acts_as_ferret/lib/local_index.rb
r248 r267 1 1 module ActsAsFerret 2 3 2 class LocalIndex < AbstractIndex 4 3 include MoreLikeThis::IndexMethods 5 6 4 7 5 def initialize(aaf_configuration) … … 79 77 # Total number of hits for the given query. 80 78 # To count the results of a multi_search query, specify an array of 81 # class names with the :m odelsoption.79 # class names with the :multi option. 82 80 def total_hits(query, options = {}) 83 81 index = (models = options.delete(:multi)) ? multi_index(models) : ferret_index … … 101 99 result = [] 102 100 index = ferret_index 103 logger.debug "query: #{ferret_index.process_query query}" # TODO only enable this for debugging purposes101 logger.debug "query: #{ferret_index.process_query query}" if logger.debug? 104 102 lazy_fields = determine_lazy_fields options 105 103
