class Xapian::Rack::Search

Attributes

database[R]

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/xapian/rack/search.rb, line 73
def initialize(app, options = {})
        @app = app
        @database_path = options[:database]
        @database = nil
        
        unless options[:logger]
                options[:logger] = Logger.new($stderr)
                options[:logger].level = Logger::DEBUG
        end
        
        # Setup the controller
        @controller = Xapian::Indexer::Controller.new(options)
        
        @controller.loaders << RelativeLoader.new(@app)
        @controller.loaders << Xapian::Indexer::Loaders::HTTP.new(options)
        @controller.extractors['text/html'] = Xapian::Indexer::Extractors::HTML.new(options)
        
        # Setup the generator
        @generator = Xapian::TermGenerator.new()
        
        @logger = options[:logger]
        
        unless options[:indexer] == :disabled
                @indexer = Thread.new do
                        while true
                                begin
                                        @logger.info "Updating index in background..."
                                        index(options[:roots], options)
                                        @logger.info "Index update finished successfully."
                                rescue
                                        @logger.error "Index update failed: #{$!}"
                                        $!.backtrace.each{|line| @logger.error(line)}
                                end
                                
                                sleep options[:refresh] || 3600
                        end
                end
        end
end

Public Instance Methods

call(env) click to toggle source
# File lib/xapian/rack/search.rb, line 168
def call(env)
        env['xapian.search'] = self
        return @app.call(env)
end
find(query, options = {}) click to toggle source
# File lib/xapian/rack/search.rb, line 113
def find(query, options = {})
        if @database
                @database.reopen
        else
                @database = Xapian::Database.new(@database_path)
        end
        
        # Start an enquire session.
        enquire = Xapian::Enquire.new(@database)

        # Setup the query parser
        qp = Xapian::QueryParser.new()
        qp.database = @database
        query = qp.parse_query(query, options[:flags] || 0)

        enquire.query = query
        
        start = options[:start] || 0
        count = options[:count] || 10
        
        matchset = enquire.mset(start, count)
        
        return matchset
end
index(roots, options = {}) click to toggle source
# File lib/xapian/rack/search.rb, line 138
def index(roots, options = {})
        writable_database = Xapian::WritableDatabase.new(@database_path, Xapian::DB_CREATE_OR_OPEN)
        @logger.debug "Opening xapian database for writing: #{@database_path}"
        
        begin
                spider = Xapian::Indexer::Spider.new(writable_database, @generator, @controller)
        
                spider.add(roots)
        
                spider.process(options) do |link|
                        uri = URI.parse(link)
                
                        if uri.relative?
                                link
                        elsif options[:domains] && options[:domains].include?(uri.host)
                                link
                        else
                                nil
                        end
                end
        
                spider.remove_old!
        ensure
                @logger.debug "Closing xapian database: #{@database_path}"
                writable_database.close
        end
end