class XapianDb::Resultset
The resultset encapsulates a Xapian::Query object and allows paged access to the found documents. The resultset is compatible with will_paginate and kaminari. @example Process the first page of a resultsest
resultset.paginate(:page => 1, :per_page => 10).each do |doc| # do something with the xapian document end
@example Use the resultset and will_paginate in a view
<%= will_paginate resultset %>
@example Use the resultset and kaminari in a view
<%= paginate resultset %>
@author Gernot Kogler
Attributes
The current page @return [Integer]
The number of hits @return [Integer]
The number of records per page
The number of pages @return [Integer]
The spelling corrected query (if a language is configured) @return [String]
The number of hits @return [Integer]
The number of hits @return [Integer]
The number of pages @return [Integer]
Public Class Methods
Constructor @param [Xapian::Enquire] enquiry a Xapian query result (see xapian.org/docs/apidoc/html/classXapian_1_1Enquire.html).
Pass nil to get an empty result set.
@param [Hash] options @option options [Integer] :db_size The current size (nr of docs) of the database @option options [Integer] :limit The maximum number of documents to retrieve @option options [Integer] :offset The index of the first result to retrieve @option options [Integer] :page (1) The page number to retrieve @option options [Integer] :per_page (10) How many docs per page? Ignored if a limit option is given @option options [String] :spelling_suggestion (nil) The spelling corrected query (if a language is configured)
# File lib/xapian_db/resultset.rb 53 def initialize(enquiry, options={}) 54 55 return build_empty_resultset if enquiry.nil? 56 params = options.dup 57 db_size = params.delete :db_size 58 @spelling_suggestion = params.delete :spelling_suggestion 59 limit = params.delete :limit 60 offset = params.delete :offset 61 page = params.delete :page 62 per_page = params.delete :per_page 63 raise ArgumentError.new "unsupported options for resultset: #{params}" if params.size > 0 64 raise ArgumentError.new "db_size option is required" unless db_size 65 66 unless (page.nil? && per_page.nil?) || (limit.nil? && offset.nil?) 67 raise ArgumentError.new "Impossible combination of parameters. Either pass page and/or per_page, or limit and/or offset." 68 end 69 70 calculated_page = offset.nil? || limit.nil? ? nil : (offset.to_f / limit.to_f) + 1 71 72 limit = limit.nil? ? db_size : limit.to_i 73 per_page = per_page.nil? ? limit.to_i : per_page.to_i 74 page = page.nil? ? (calculated_page.nil? ? 1 : calculated_page) : page.to_i 75 offset = offset.nil? ? (page - 1) * per_page : offset.to_i 76 count = per_page < limit ? per_page : limit 77 78 return build_empty_resultset if (page - 1) * per_page > db_size 79 result_window = enquiry.mset(offset, count) 80 @hits = result_window.matches_estimated 81 return build_empty_resultset if @hits == 0 82 83 self.replace result_window.matches.map{|match| decorate(match).document} 84 @total_pages = (@hits / per_page.to_f).ceil 85 @current_page = (page == page.to_i) ? page.to_i : page 86 @limit_value = per_page 87 end
Public Instance Methods
Build an empty resultset
# File lib/xapian_db/resultset.rb 102 def build_empty_resultset 103 @hits = 0 104 @total_pages = 0 105 @current_page = 0 106 @limit_value = 0 107 self 108 end
Decorate a Xapian match with field accessors for each configured attribute @param [Xapian::Match] a match @return [Xapian::Match] the decorated match
# File lib/xapian_db/resultset.rb 113 def decorate(match) 114 klass_name = match.document.values[0].value 115 blueprint = XapianDb::DocumentBlueprint.blueprint_for klass_name 116 match.document.extend blueprint.accessors_module 117 match.document.instance_variable_set :@score, match.percent 118 match 119 end
The next page number @return [Integer] The number of the next page or nil, if we are at the last page
# File lib/xapian_db/resultset.rb 97 def next_page 98 @current_page < @total_pages ? (@current_page + 1): nil 99 end
The previous page number @return [Integer] The number of the previous page or nil, if we are at page 1
# File lib/xapian_db/resultset.rb 91 def previous_page 92 @current_page > 1 ? (@current_page - 1) : nil 93 end