To edit pages or tickets please login with username/password: aaf/aaf

Changeset 323

Show
Ignore:
Timestamp:
02/18/08 20:36:06 (8 months ago)
Author:
jk
Message:

drb somewhat works

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugin/acts_as_ferret/lib/class_methods.rb

    r320 r323  
    102102    end 
    103103 
    104     # TODO change any references to this method to use 
    105     # ActsAsFerret::change_index_dir 
    106     def index_dir=(dir) 
    107       logger.warn "DEPRECATED, use ActsAsFerret::change_index_dir instead if index_dir=" 
    108       ActsAsFerret::change_index_dir(aaf_configuration[:name], dir) 
    109     end 
    110      
    111104    # Retrieve the index instance for this model class. This can either be a 
    112105    # LocalIndex, or a RemoteIndex instance. 
    113106    #  
    114     # Index instances are stored in a hash, using the index directory 
    115     # as the key. So model classes sharing a single index will share their 
    116     # Index object, too. 
    117107    def aaf_index 
    118108      @index ||= ActsAsFerret::get_index(aaf_configuration[:name]) 
     
    182172    end  
    183173 
    184     
    185174 
    186175    # Returns the total number of hits for the given query  
     
    212201 
    213202     
    214     # returns an array of hashes, each containing :class_name, 
    215     # :id and :score for a hit. 
    216     # 
    217     # if a block is given, class_name, id and score of each hit will  
    218     # be yielded, and the total number of hits is returned. 
    219     def id_multi_search(query, additional_models = [], options = {}, &proc) 
    220       logger.warn "Model.id_multi_search is deprecated, use ActsAsFerret::find_ids instead!" 
    221       models = add_self_to_model_list_if_necessary(additional_models) 
    222       ActsAsFerret::find_ids(query, models, options, &proc) 
    223     end 
    224      
    225  
    226203    protected 
    227204 
    228     def add_self_to_model_list_if_necessary(models) 
    229       models = [ models ] unless models.is_a? Array 
    230       models << self unless models.include?(self) 
    231       models 
    232     end 
    233  
    234     def find_records_lazy_or_not(q, options = {}, find_options = {}) 
    235       if options[:lazy] 
    236         logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? 
    237         lazy_find_by_contents q, options 
    238       else 
    239         ar_find_by_contents q, options, find_options 
    240       end 
    241     end 
    242  
    243     def ar_find_by_contents(q, options = {}, find_options = {}) 
    244       result_ids = {} 
    245       total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| 
    246         # stores ids, index and score of each hit for later ordering of 
    247         # results 
    248         result_ids[id] = [ result_ids.size + 1, score ] 
    249       end 
    250  
    251       result = ActsAsFerret::retrieve_records( { self.name => result_ids }, find_options ) 
    252        
    253       # count total_hits via sql when using conditions or when we're called 
    254       # from an ActiveRecord association. 
    255       if find_options[:conditions] or caller.find{ |call| call =~ %r{active_record/associations} } 
    256         # chances are the ferret result count is not our total_hits value, so 
    257         # we correct this here. 
    258         if options[:limit] != :all || options[:page] || options[:offset] || find_options[:limit] || find_options[:offset] 
    259           # our ferret result has been limited, so we need to re-run that 
    260           # search to get the full result set from ferret. 
    261           result_ids = {} 
    262           find_ids_with_ferret(q, options.update(:limit => :all, :offset => 0)) do |model, id, score, data| 
    263             result_ids[id] = [ result_ids.size + 1, score ] 
    264           end 
    265           # Now ask the database for the total size of the final result set. 
    266           total_hits = count_records( { self.name => result_ids }, find_options ) 
    267         else 
    268           # what we got from the database is our full result set, so take 
    269           # it's size 
    270           total_hits = result.length 
    271         end 
    272       end 
    273  
    274       [ total_hits, result ] 
    275     end 
    276  
    277     def lazy_find_by_contents(q, options = {}) 
    278       logger.debug "lazy_find_by_contents: #{q}" 
    279       result = [] 
    280       rank   = 0 
    281       total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| 
    282         logger.debug "model: #{model}, id: #{id}, data: #{data}" 
    283         result << FerretResult.new(model, id, score, rank += 1, data) 
    284       end 
    285       [ total_hits, result ] 
    286     end 
     205#    def find_records_lazy_or_not(q, options = {}, find_options = {}) 
     206#      if options[:lazy] 
     207#        logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? 
     208#        lazy_find_by_contents q, options 
     209#      else 
     210#        ar_find_by_contents q, options, find_options 
     211#      end 
     212#    end 
     213
     214#    def ar_find_by_contents(q, options = {}, find_options = {}) 
     215#      result_ids = {} 
     216#      total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| 
     217#        # stores ids, index and score of each hit for later ordering of 
     218#        # results 
     219#        result_ids[id] = [ result_ids.size + 1, score ] 
     220#      end 
     221
     222#      result = ActsAsFerret::retrieve_records( { self.name => result_ids }, find_options ) 
     223#       
     224#      # count total_hits via sql when using conditions or when we're called 
     225#      # from an ActiveRecord association. 
     226#      if find_options[:conditions] or caller.find{ |call| call =~ %r{active_record/associations} } 
     227#        # chances are the ferret result count is not our total_hits value, so 
     228#        # we correct this here. 
     229#        if options[:limit] != :all || options[:page] || options[:offset] || find_options[:limit] || find_options[:offset] 
     230#          # our ferret result has been limited, so we need to re-run that 
     231#          # search to get the full result set from ferret. 
     232#          result_ids = {} 
     233#          find_ids_with_ferret(q, options.update(:limit => :all, :offset => 0)) do |model, id, score, data| 
     234#            result_ids[id] = [ result_ids.size + 1, score ] 
     235#          end 
     236#          # Now ask the database for the total size of the final result set. 
     237#          total_hits = count_records( { self.name => result_ids }, find_options ) 
     238#        else 
     239#          # what we got from the database is our full result set, so take 
     240#          # it's size 
     241#          total_hits = result.length 
     242#        end 
     243#      end 
     244
     245#      [ total_hits, result ] 
     246#    end 
     247
     248#    def lazy_find_by_contents(q, options = {}) 
     249#      logger.debug "lazy_find_by_contents: #{q}" 
     250#      result = [] 
     251#      rank   = 0 
     252#      total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| 
     253#        logger.debug "model: #{model}, id: #{id}, data: #{data}" 
     254#        result << FerretResult.new(model, id, score, rank += 1, data) 
     255#      end 
     256#      [ total_hits, result ] 
     257#    end 
    287258 
    288259 
     
    292263 
    293264 
    294     def count_records(id_arrays, find_options = {}) 
    295       count_options = find_options.dup 
    296       count_options.delete :limit 
    297       count_options.delete :offset 
    298       count = 0 
    299       id_arrays.each do |model, id_array| 
    300         next if id_array.empty? 
    301         model = model.constantize 
    302         # merge conditions 
    303         conditions = ActsAsFerret::combine_conditions([ "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ],  
    304                                         find_options[:conditions]) 
    305         opts = find_options.merge :conditions => conditions 
    306         opts.delete :limit; opts.delete :offset 
    307         count += model.count opts 
    308       end 
    309       count 
    310     end 
     265#    def count_records(id_arrays, find_options = {}) 
     266#      count_options = find_options.dup 
     267#      count_options.delete :limit 
     268#      count_options.delete :offset 
     269#      count = 0 
     270#      id_arrays.each do |model, id_array| 
     271#        next if id_array.empty? 
     272#        model = model.constantize 
     273#        # merge conditions 
     274#        conditions = ActsAsFerret::combine_conditions([ "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ],  
     275#                                        find_options[:conditions]) 
     276#        opts = find_options.merge :conditions => conditions 
     277#        opts.delete :limit; opts.delete :offset 
     278#        count += model.count opts 
     279#      end 
     280#      count 
     281#    end 
    311282 
    312283  end 
  • trunk/plugin/acts_as_ferret/lib/ferret_find_methods.rb

    r320 r323  
    11module ActsAsFerret 
     2  # Ferret search logic common to single-class indexes, shared indexes and 
     3  # multi indexes. 
    24  module FerretFindMethods 
    35 
  • trunk/plugin/acts_as_ferret/lib/index.rb

    r320 r323  
    1919  # base class for local and remote indexes 
    2020  class AbstractIndex 
     21    include FerretFindMethods 
    2122 
    22     attr_reader :aaf_configuration 
    2323    attr_accessor :logger, :index_name, :index_definition 
    2424    def initialize(index_definition) 
     
    2727      @logger = IndexLogger.new(ActsAsFerret::logger, @index_name) 
    2828    end 
    29  
    30     #def index_definition 
    31     #  @index_definition ||= ActsAsFerret::index_definition(@index_name) 
    32     #end 
    3329 
    3430    # TODO allow for per-class field configuration (i.e. different via, boosts 
  • trunk/plugin/acts_as_ferret/lib/local_index.rb

    r320 r323  
    11module ActsAsFerret 
    22  class LocalIndex < AbstractIndex 
    3     include FerretFindMethods, MoreLikeThis::IndexMethods 
     3    include MoreLikeThis::IndexMethods 
    44 
    55    def initialize(index_name) 

To edit pages or tickets please login with username/password: aaf/aaf