~ubuntu-branches/ubuntu/saucy/dhelp/saucy-proposed

« back to all changes in this revision

Viewing changes to tmp/xapian/simplesearch.rb

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2008-06-19 01:25:07 UTC
  • Revision ID: james.westby@ubuntu.com-20080619012507-adt75omul1shucde
Tags: 0.6.9ubuntu1
* Resynchronise with Debian. Remaining changes:
  - Recommends: firefox-3.0.
  - Exit zero if the bdb module is not available; this usually indicates
    that dhelp is not configured yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
require 'xapian'
4
 
 
5
 
class Index
6
 
  def initialize(path)
7
 
    @path = path
8
 
    @xapian_database = Xapian::Database.new(@path)
9
 
  end
10
 
 
11
 
  def search(terms, user_opts={})
12
 
    opts = {:offset => 0, :limit => 10}.merge(user_opts)
13
 
 
14
 
    query_parser = Xapian::QueryParser.new
15
 
    query_parser.database = @xapian_database
16
 
    query_parser.stemmer = Xapian::Stem.new("english")
17
 
    query_parser.stemming_strategy = Xapian::QueryParser::STEM_SOME
18
 
 
19
 
    enquire = Xapian::Enquire.new(@xapian_database)
20
 
    enquire.query = query_parser.parse_query(terms)
21
 
    return XapianResultSet.new(enquire.mset(opts[:offset], opts[:limit]))
22
 
  end
23
 
end
24
 
 
25
 
class SearchResultSet; end
26
 
 
27
 
class XapianResultSet < SearchResultSet
28
 
  def initialize(mset)
29
 
    @mset = mset
30
 
  end
31
 
 
32
 
  def each(&blk)
33
 
    @mset.matches.each &blk
34
 
  end
35
 
 
36
 
  def page_size
37
 
    @mset.size
38
 
  end
39
 
 
40
 
  def total_results
41
 
    @mset.matches_estimated
42
 
  end
43
 
end
44
 
 
45
 
if ARGV.size < 2
46
 
  $stderr.puts "Usage: #{$0} PATH_TO_DATABASE QUERY"
47
 
  exit 99
48
 
end
49
 
 
50
 
# Open the database for searching.
51
 
database = Index.new(ARGV[0])
52
 
result_set = database.search(ARGV[1..-1].join(' '))
53
 
 
54
 
# Display the results.
55
 
puts "#{result_set.total_results} results found."
56
 
puts "Matches 1-#{result_set.page_size}:\n"
57
 
 
58
 
result_set.each {|m|
59
 
  puts "#{m.rank + 1}: #{m.percent}% docid=#{m.docid} [#{m.document.data}]\n"
60
 
}