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 108 109 110 111 |
# File 'lib/xapian/rack/search.rb', line 73 def initialize(app, = {}) @app = app @database_path = [:database] @database = nil unless [:logger] [:logger] = Logger.new($stderr) [:logger].level = Logger::DEBUG end # 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] unless [:indexer] == :disabled @indexer = Thread.new do while true begin @logger.info "Updating index in background..." index([:roots], ) @logger.info "Index update finished successfully." rescue @logger.error "Index update failed: #{$!}" $!.backtrace.each{|line| @logger.error(line)} end sleep [:refresh] || 3600 end end end end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
166 167 168 |
# File 'lib/xapian/rack/search.rb', line 166 def database @database end |
Instance Method Details
#call(env) ⇒ Object
168 169 170 171 |
# File 'lib/xapian/rack/search.rb', line 168 def call(env) env['xapian.search'] = self return @app.call(env) end |
#find(query, options = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/xapian/rack/search.rb', line 113 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/xapian/rack/search.rb', line 138 def index(roots, = {}) 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() 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 @logger.debug "Closing xapian database: #{@database_path}" writable_database.close end end |