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
# File 'lib/xapian/rack/search.rb', line 73

def initialize(app, options = {})
	@app = app
	@database_path = options[: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 = options[:logger] || Logger.new($stderr)
	
	unless options[:indexer] == :disabled
		@indexer = Thread.new do
			while true
				begin
					@logger.info "Updating index in background..."
					index(options[:roots], options)
				rescue
					@logger.error "Error while updating index: #{$!}"
					$!.backtrace.each{|line| @logger.error(line)}
				ensure
					@logger.info "Index update finished."
				end
				
				sleep options[:refresh] || 3600
			end
		end
	end
end

Instance Attribute Details

#databaseObject (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, 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



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, options = {})
	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(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
		writable_database.close
	end
end