1 # $Id: Resultset.pm,v 1.11 2004-07-28 08:15:46 adam Exp $
3 # Zebra perl API header
4 # =============================================================================
5 package IDZebra::Resultset;
12 use IDZebra::Logger qw(:flags :calls);
13 use Scalar::Util qw(weaken);
15 our $VERSION = do { my @r = (q$Revision: 1.11 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
16 our @ISA = qw(IDZebra::Logger);
20 # -----------------------------------------------------------------------------
21 # Class constructors, destructor
22 # -----------------------------------------------------------------------------
24 my ($proto,$session, %args) = @_;
25 my $class = ref($proto) || $proto;
27 bless ($self, $class);
29 $self->{session} = $session;
30 weaken ($self->{session});
32 $self->{odr_stream} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
34 $self->{name} = $args{name};
35 $self->{query} = $args{query};
36 $self->{recordCount} = $args{recordCount};
37 $self->{errCode} = $args{errCode};
38 $self->{errString} = $args{errString};
45 return ($self->{recordCount});
49 return ($self->{recordCount});
54 return ($self->{errCode});
59 return ($self->{errCode});
65 my $count = 0; my $type = 0; my $len = 0;
66 my $tc = IDZebra::resultSetTerms($self->{session}{zh},$self->{name},
67 0, \$count, \$type, "\0", \$len);
69 logf (LOG_LOG,"Got $tc terms");
73 for (my $i=0; $i<$tc; $i++) {
75 my $t = {term => "\0" x $len, count => 0, type => 0};
76 my $stat = IDZebra::resultSetTerms($self->{session}{zh},$self->{name},
77 $i, \$t->{count}, \$t->{type},
79 $t->{term} = substr($t->{term}, 0, $len);
81 "term $i: type $t->{type}, '$t->{term}' ($t->{count})");
87 # =============================================================================
91 # print STDERR "Destroy RS\n";
95 if ($self->{session}{zh}) {
96 my $r = IDZebra::deleteResultSet($self->{session}{zh},
97 0, #Z_DeleteRequest_list,
102 if ($self->{odr_stream}) {
103 IDZebra::odr_reset($self->{odr_stream});
104 IDZebra::odr_destroy($self->{odr_stream});
105 $self->{odr_stream} = undef;
108 delete($self->{session});
110 # -----------------------------------------------------------------------------
112 my ($self, %args) = @_;
114 unless ($self->{session}{zh}) {
115 croak ("Session is closed or out of scope");
117 my $from = $args{from} ? $args{from} : 1;
118 my $to = $args{to} ? $args{to} : $self->{recordCount};
120 if (($to-$from) >= 1000) {
122 croak ("Cannot fetch more than 1000 records at a time");
128 my $elementSet = $args{elementSet} ? $args{elementSet} : 'R';
129 my $schema = $args{schema} ? $args{schema} : '';
130 my $recordSyntax = $args{recordSyntax} ? $args{recordSyntax} : '';
132 my $class = $args{class} ? $args{class} : '';
134 # ADAM: Reset before we use it (not after)
135 IDZebra::odr_reset($self->{odr_stream});
137 my $ro = IDZebra::RetrievalObj->new();
138 IDZebra::records_retrieve($self->{session}{zh},
150 for (my $i=$from; $i<=$to; $i++) {
151 my $rec = IDZebra::RetrievalRecord->new();
152 IDZebra::record_retrieve($ro, $self->{odr_stream}, $rec, $i-$from+1);
163 # ============================================================================
165 my ($self, $sortspec, $setname) = @_;
167 unless ($self->{session}{zh}) {
168 croak ("Session is closed or out of scope");
172 return ($_[0] = $self->{session}->sortResultsets($sortspec,
173 $self->{session}->_new_setname, ($self)));
176 return ($self->{session}->sortResultsets($sortspec,
181 # ============================================================================
186 IDZebra::Resultset - Representation of Zebra search results
192 printf ("RS Status is %d (%s)\n", $rs->errCode, $rs->errString);
194 my @recs = $rs->records(from => 1,
199 The I<Resultset> object represents results of a Zebra search. Contains number of hits, search status, and can be used to sort and retrieve the records.
203 The folowing properties are available, trough object methods and the object hash reference:
209 The error code returned from search, resulting the Resultset object.
213 The optional error string
217 The number of hits (records available) in the resultset
221 Just the synonym for I<recordCount>
225 =head1 RETRIEVING RECORDS
227 In order to retrieve records, use the I<records> method:
229 my @recs = $rs->records();
231 By default this is going to return an array of IDZebra::RetrievalRecord objects. The possible arguments are:
237 Retrieve records from the given position. The first record corresponds to position 1. If not specified, retrieval starts from the first record.
241 The last record position to be fetched. If not specified, all records are going to be fetched, starting from position I<from>.
245 The element set used for retrieval. If not specified 'I<R>' is used, which will return the "record" in the original format (ie.: without extraction, just as the original file, or data buffer in the update call).
249 The schema used for retrieval. The default is "".
251 =item B<recordSyntax>
253 The record syntax for retrieval. The default is SUTRS.
259 You can sort resultsets by calling:
261 $rs1->sort($sort_expr);
263 or create a new sorted resultset:
265 $rs2 = $rs1->sort($sort_expr);
267 The sort expression has the same format as described in the I<yaz_client> documentation. For example:
269 $rs1->sort('1=4 id');
271 will sort thr results by title, in a case insensitive way, in descending order, while
275 will sort ascending by titles.
283 Peter Popovics, pop@technomat.hu
287 Zebra documentation, IDZebra::ResultSet, IDZebra::RetrievalRecord manpages.