Class: Xapian::Rack::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian/rack/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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

Instance Attribute Details

#databaseObject (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, 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 = {}) ⇒ 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, 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