1 package WebPAC::Search::Estraier;
12 WebPAC::Search::Estraier - search Hyper Estraier full text index
20 our $VERSION = '0.02';
24 Search WebPAC data using Hyper Estraier full text index created with
25 L<WebPAC::Output::Estraier>.
31 Connect to Hyper Estraier index using HTTP
33 my $est = new WebPAC::Search::Estraier(
34 url => 'http://localhost:1978/node/webpac2',
37 encoding => 'iso-8859-2',
38 log => $Log::Log4perl->log_object,
47 URI to C<estmaster> node
51 C<estmaster> user with read rights
59 character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
60 (and it probably is). This encoding will be converted to C<UTF-8> for
65 L<Log::Log4perl> object or equivalent (C<< $c->log >> can be used in
66 L<Catalyst> and there is support for it).
77 my $log = $self->_get_logger;
79 foreach my $p (qw/url user passwd/) {
80 $log->logdie("need $p") unless ($self->{$p});
83 $log->info("opening Hyper Estraier index $self->{'url'} as $self->{'user'}");
85 $self->{'db'} = HyperEstraier::Node->new($self->{'url'});
86 $self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'});
88 my $encoding = $self->{'encoding'} || 'ISO-8859-2';
89 $log->info("using encoding $encoding");
91 $self->{'iconv'} = new Text::Iconv('UTF-8', $encoding) or
92 $log->die("can't create conversion from UTF-8 to $encoding");
94 $self ? return $self : return undef;
100 Locate items in index
102 my @results = $est->search(
103 phrase => 'name of book or novel',
105 "filepath ISTRINC $q",
108 get_attr => qw/PersonalName TitleProper/,
111 options => $HyperEstraier::Condition::SURE,
114 Options are close match to Hyper Estraier API, except C<get_attr> which defines
115 attributes which will be returned in hash for each record.
117 Results are returned as hash array with keys named by attributes
126 my $log = $self->_get_logger;
128 $self->confess('need db in object') unless ($self->{db});
129 $self->confess('need get_attr') unless ($args->{get_attr});
131 $self->confess("need get_attr as array not " . ref($args->{get_attr}) ) unless (ref($args->{get_attr}) eq 'ARRAY');
133 my $q = $args->{phrase};
135 $log->debug("args: " . Dumper( $args ));
137 my $cond = HyperEstraier::Condition->new();
138 if ( ref($args->{add_attr}) eq 'ARRAY' ) {
139 $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) );
141 $cond->add_attr( $self->{iconv}->convert( $_ ) );
142 $log->debug(" + $_");
143 } @{ $args->{add_attr} };
146 $cond->set_phrase( $self->{iconv}->convert($q) ) if ($q);
147 $cond->set_max( $args->{max} ) if ($args->{max});
148 $cond->set_options( $args->{options} ) if ($args->{options});
149 $cond->set_order( $args->{order} ) if ($args->{order});
151 my $result = $self->{db}->search($cond, 0) ||
152 $log->die("can't search for ", sub { Dumper( $args ) });
154 my $hits = $result->doc_num;
155 $log->debug("found $hits hits for '$q'");
159 for my $i ( 0 .. ( $hits - 1 ) ) {
161 #$log->debug("get_doc($i)");
162 my $doc = $result->get_doc( $i );
164 $log->warn("can't find result $i");
170 foreach my $attr (@{ $args->{get_attr} }) {
171 my $val = $doc->attr( $attr );
172 #$log->debug("attr $attr = ", $val || 'undef');
173 $hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val));
177 push @results, $hash;
182 # $log->debug("results " . Dumper( \@results ));
184 $self->confess("expected to return array") unless (wantarray);
191 wrapper around L<Log::Log4perl> C<confess> or C<< $log->fatal >> or
192 C<< $log->error >> if they exists (like in L<Catalyst>), else plain
199 if (my $log = $self->{'log'}) {
200 if ($log->can('logconfess')) {
201 $log->logconfess(@_);
202 } elsif ($log->can('fatal')) {
205 } elsif ($log->can('error')) {
217 For compatibility with same method from L<WebPAC::Common>, but without
225 return $self->{'log'} || die "really need log!";
230 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
232 =head1 COPYRIGHT & LICENSE
234 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
236 This program is free software; you can redistribute it and/or modify it
237 under the same terms as Perl itself.
241 1; # End of WebPAC::Search::Estraier