1 # $Id: Resultset.pm,v 1.13 2004-09-16 14:58:47 heikki 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.13 $ =~ /\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);
28 logf(LOG_DEBUG,"creating a Resultset ".$args{name});
30 $self->{session} = $session;
31 weaken ($self->{session});
33 $self->{odr_stream} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
35 $self->{name} = $args{name};
36 $self->{query} = $args{query};
37 $self->{recordCount} = $args{recordCount};
38 $self->{errCode} = $args{errCode};
39 $self->{errString} = $args{errString};
41 logf(LOG_DEBUG,"created a Resultset ".$self->{name});
47 return ($self->{recordCount});
51 return ($self->{recordCount});
56 return ($self->{errCode});
61 return ($self->{errCode});
64 ######################
65 # this is disabled, while the term counts are broken by the work done to
66 # rsets. To be reinstantiated some day real soon now...
70 # my $count = 0; my $type = 0; my $len = 0;
71 # my $tc = IDZebra::resultSetTerms($self->{session}{zh},$self->{name},
72 # 0, \$count, \$type, "\0", \$len);
74 # logf (LOG_LOG,"Got $tc terms");
78 # for (my $i=0; $i<$tc; $i++) {
80 # my $t = {term => "\0" x $len, count => 0, type => 0};
81 # my $stat = IDZebra::resultSetTerms($self->{session}{zh},$self->{name},
82 # $i, \$t->{count}, \$t->{type},
84 # $t->{term} = substr($t->{term}, 0, $len);
86 # "term $i: type $t->{type}, '$t->{term}' ($t->{count})");
92 # =============================================================================
96 # print STDERR "Destroy RS\n";
100 logf(LOG_DEBUG, "Destroying a Resultset ". $self->{name});
101 if ($self->{session}{zh}) {
102 my $r = IDZebra::deleteResultSet($self->{session}{zh},
103 0, #Z_DeleteRequest_list,
108 if ($self->{odr_stream}) {
109 IDZebra::odr_reset($self->{odr_stream});
110 IDZebra::odr_destroy($self->{odr_stream});
111 $self->{odr_stream} = undef;
114 delete($self->{session});
115 logf(LOG_DEBUG, "Destroyed a Resultset ". $self->{name});
117 # -----------------------------------------------------------------------------
119 my ($self, %args) = @_;
121 unless ($self->{session}{zh}) {
122 croak ("Session is closed or out of scope");
124 my $from = $args{from} ? $args{from} : 1;
125 my $to = $args{to} ? $args{to} : $self->{recordCount};
127 if (($to-$from) >= 1000) {
129 croak ("Cannot fetch more than 1000 records at a time");
135 my $elementSet = $args{elementSet} ? $args{elementSet} : 'R';
136 my $schema = $args{schema} ? $args{schema} : '';
137 my $recordSyntax = $args{recordSyntax} ? $args{recordSyntax} : '';
139 my $class = $args{class} ? $args{class} : '';
141 # ADAM: Reset before we use it (not after)
142 IDZebra::odr_reset($self->{odr_stream});
144 my $ro = IDZebra::RetrievalObj->new();
145 IDZebra::records_retrieve($self->{session}{zh},
157 for (my $i=$from; $i<=$to; $i++) {
158 my $rec = IDZebra::RetrievalRecord->new();
159 IDZebra::record_retrieve($ro, $self->{odr_stream}, $rec, $i-$from+1);
170 # ============================================================================
172 my ($self, $sortspec, $setname) = @_;
174 unless ($self->{session}{zh}) {
175 croak ("Session is closed or out of scope");
179 $setname=$self->{session}->_new_setname();
181 return ($self->{session}->sortResultsets($sortspec,
183 # unless ($setname) {
184 # return ($_[0] = $self->{session}->sortResultsets($sortspec,
185 # $self->{session}->_new_setname, ($self)));
188 # return ($self->{session}->sortResultsets($sortspec,
189 # $setname, ($self)));
193 # ============================================================================
198 IDZebra::Resultset - Representation of Zebra search results
204 printf ("RS Status is %d (%s)\n", $rs->errCode, $rs->errString);
206 my @recs = $rs->records(from => 1,
211 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.
215 The folowing properties are available, trough object methods and the object hash reference:
221 The error code returned from search, resulting the Resultset object.
225 The optional error string
229 The number of hits (records available) in the resultset
233 Just the synonym for I<recordCount>
237 =head1 RETRIEVING RECORDS
239 In order to retrieve records, use the I<records> method:
241 my @recs = $rs->records();
243 By default this is going to return an array of IDZebra::RetrievalRecord objects. The possible arguments are:
249 Retrieve records from the given position. The first record corresponds to position 1. If not specified, retrieval starts from the first record.
253 The last record position to be fetched. If not specified, all records are going to be fetched, starting from position I<from>.
257 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).
261 The schema used for retrieval. The default is "".
263 =item B<recordSyntax>
265 The record syntax for retrieval. The default is SUTRS.
271 You can sort resultsets by calling:
273 $rs1->sort($sort_expr);
275 or create a new sorted resultset:
277 $rs2 = $rs1->sort($sort_expr);
279 The sort expression has the same format as described in the I<yaz_client> documentation. For example:
281 $rs1->sort('1=4 id');
283 will sort thr results by title, in a case insensitive way, in descending order, while
287 will sort ascending by titles.
295 Peter Popovics, pop@technomat.hu
299 Zebra documentation, IDZebra::ResultSet, IDZebra::RetrievalRecord manpages.