Class: Xapian::Rack::Search
- Inherits:
-
Object
- Object
- Xapian::Rack::Search
- Defined in:
- lib/xapian/rack/search.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #find(query, options = {}) ⇒ Object
- #index(roots, options = {}) ⇒ Object
-
#initialize(app, options = {}) ⇒ Search
constructor
A new instance of Search.
Constructor Details
#initialize(app, options = {}) ⇒ Search
Returns a new instance of Search.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/xapian/rack/search.rb', line 73 def initialize(app, = {}) @app = app @database_path = [:database] @database = nil # Setup the controller @controller = Xapian::Indexer::Controller.new @controller.loaders << RelativeLoader.new(@app) @controller.loaders << Xapian::Indexer::Loaders::HTTP.new @controller.extractors['text/html'] = Xapian::Indexer::Extractors::HTML.new # Setup the generator @generator = Xapian::TermGenerator.new() @logger = [:logger] || Logger.new($stderr) unless [:indexer] == :disabled @indexer = Thread.new do while true begin @logger.info "Updating index in background..." index([:roots], ) rescue @logger.error "Error while updating index: #{$!}" $!.backtrace.each{|line| @logger.error(line)} ensure @logger.info "Index update finished." end sleep [:refresh] || 3600 end end end end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
160 161 162 |
# File 'lib/xapian/rack/search.rb', line 160 def database @database end |
Instance Method Details
#call(env) ⇒ Object
162 163 164 165 |
# File 'lib/xapian/rack/search.rb', line 162 def call(env) env['xapian.search'] = self return @app.call(env) end |
#find(query, options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/xapian/rack/search.rb', line 109 def find(query, = {}) 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, [:flags] || 0) enquire.query = query start = [:start] || 0 count = [:count] || 10 matchset = enquire.mset(start, count) return matchset end |
#index(roots, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/xapian/rack/search.rb', line 134 def index(roots, = {}) writable_database = Xapian::WritableDatabase.new(@database_path, Xapian::DB_CREATE_OR_OPEN) begin spider = Xapian::Indexer::Spider.new(writable_database, @generator, @controller) spider.add(roots) spider.process() do |link| uri = URI.parse(link) if uri.relative? link elsif [:domains] && [:domains].include?(uri.host) link else nil end end spider.remove_old! ensure writable_database.close end end |