Ticket #110: xapian_sortorder1.pl

File xapian_sortorder1.pl, 1.3 KB (added by Olly Betts, 17 years ago)

perl script which demonstrates the problem

Line 
1#!/usr/bin/perl
2use Search::Xapian qw/:db/;
3
4# open index for writing
5my $db = Search::Xapian::WritableDatabase->new('./testdb', DB_CREATE_OR_OVERWRITE) or die $!;
6
7# populating the index: term with termcount 1
8for (0..4) {
9 my $doc = Search::Xapian::Document->new() or die $!;
10 $doc->add_value(0, $_);
11 $doc->add_term("test");
12 $db->add_document($doc);
13}
14
15# populating the index: same term with termcount 2
16for (0..4) {
17 my $doc = Search::Xapian::Document->new() or die $!;
18 $doc->add_value(0, $_);
19 $doc->add_term("test", 2);
20 $db->add_document($doc);
21}
22
23# close index
24undef $db;
25
26# reopen for search
27my $db = Search::Xapian::Database->new('./testdb') or die $!;
28
29# set query
30my $enquire = $db->enquire();
31$enquire->set_query(Search::Xapian::Query->new('test'));
32
33## comment this out to try without sorting by value
34$enquire->set_sort_by_value_then_relevance(0, 1);
35##
36
37# 10 results, unpaged
38print "(unpaged)\n";
39search(0, 10);
40
41# 10 results, splitted in several pages
42print "(paged)\n";
43search(0, 1);
44search(1, 1);
45search(2, 8);
46
47
48# performs the search (according to an example from cpan)
49sub search {
50 my ($start, $num) = @_;
51
52 my @matches = $enquire->matches($start, $num);
53 foreach my $match ( @matches ) {
54 my $doc = $match->get_document();
55 printf "ID %d %d%%\n", $match->get_docid(), $match->get_percent();
56 }
57}