3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use HyperEstraier; |
use Search::Estraier; |
7 |
use Text::Iconv; |
use Encode qw/from_to/; |
8 |
use Data::Dumper; |
use Data::Dumper; |
9 |
|
|
10 |
=head1 NAME |
=head1 NAME |
13 |
|
|
14 |
=head1 VERSION |
=head1 VERSION |
15 |
|
|
16 |
Version 0.02 |
Version 0.07 |
17 |
|
|
18 |
=cut |
=cut |
19 |
|
|
20 |
our $VERSION = '0.02'; |
our $VERSION = '0.07'; |
21 |
|
|
22 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
23 |
|
|
31 |
Connect to Hyper Estraier index using HTTP |
Connect to Hyper Estraier index using HTTP |
32 |
|
|
33 |
my $est = new WebPAC::Search::Estraier( |
my $est = new WebPAC::Search::Estraier( |
34 |
url => 'http://localhost:1978/node/webpac2', |
masterurl => 'http://localhost:1978/', |
35 |
|
database => 'webpac2', |
36 |
user => 'admin', |
user => 'admin', |
37 |
passwd => 'admin', |
passwd => 'admin', |
38 |
encoding => 'iso-8859-2', |
encoding => 'iso-8859-2', |
43 |
|
|
44 |
=over 4 |
=over 4 |
45 |
|
|
46 |
=item url |
=item maseterurl |
47 |
|
|
48 |
URI to C<estmaster> node |
URI to C<estmaster> node |
49 |
|
|
50 |
|
=item database |
51 |
|
|
52 |
|
name of C<estmaster> node |
53 |
|
|
54 |
=item user |
=item user |
55 |
|
|
56 |
C<estmaster> user with read rights |
C<estmaster> user with read rights |
81 |
|
|
82 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
83 |
|
|
84 |
foreach my $p (qw/url user passwd/) { |
foreach my $p (qw/masterurl user passwd/) { |
85 |
$log->logdie("need $p") unless ($self->{$p}); |
$log->logdie("need $p") unless ($self->{$p}); |
86 |
} |
} |
87 |
|
|
88 |
$log->info("opening Hyper Estraier index $self->{'url'} as $self->{'user'}"); |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
89 |
|
$self->{url} = $url; |
90 |
|
|
91 |
$self->{'db'} = HyperEstraier::Node->new($self->{'url'}); |
$log->info("opening Hyper Estraier index $self->{'url'} as $self->{'user'}"); |
|
$self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'}); |
|
92 |
|
|
93 |
my $encoding = $self->{'encoding'} || 'ISO-8859-2'; |
$self->{db} = Search::Estraier::Node->new; |
94 |
$log->info("using encoding $encoding"); |
$self->{db}->set_url($self->{'url'}); |
95 |
|
$self->{db}->set_auth($self->{'user'}, $self->{'passwd'}); |
96 |
|
|
97 |
$self->{'iconv'} = new Text::Iconv('UTF-8', $encoding) or |
$self->{'encoding'} ||= 'ISO-8859-2'; |
98 |
$log->die("can't create conversion from UTF-8 to $encoding"); |
$log->info("using encoding ",$self->{encoding}); |
99 |
|
|
100 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
101 |
} |
} |
115 |
order => 'NUMD', |
order => 'NUMD', |
116 |
max => 100, |
max => 100, |
117 |
options => $HyperEstraier::Condition::SURE, |
options => $HyperEstraier::Condition::SURE, |
118 |
|
page => 42, |
119 |
|
depth => 0, |
120 |
); |
); |
121 |
|
|
122 |
Options are close match to Hyper Estraier API, except C<get_attr> which defines |
Options are close match to Hyper Estraier API, except C<get_attr> which defines |
124 |
|
|
125 |
Results are returned as hash array with keys named by attributes |
Results are returned as hash array with keys named by attributes |
126 |
|
|
127 |
|
Pages are numbered C< 1 ... hits/max >. |
128 |
|
|
129 |
=cut |
=cut |
130 |
|
|
131 |
sub search { |
sub search { |
146 |
|
|
147 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("args: " . Dumper( $args )); |
148 |
|
|
149 |
my $cond = HyperEstraier::Condition->new(); |
my $cond = Search::Estraier::Condition->new(); |
150 |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
151 |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
152 |
map { |
map { |
153 |
$cond->add_attr( $self->{iconv}->convert( $_ ) ); |
$cond->add_attr( $self->convert( $_ ) ); |
154 |
$log->debug(" + $_"); |
$log->debug(" + $_"); |
155 |
} @{ $args->{add_attr} }; |
} @{ $args->{add_attr} }; |
156 |
}; |
}; |
157 |
|
|
158 |
$cond->set_phrase( $self->{iconv}->convert($q) ) if ($q); |
$cond->set_phrase( $self->convert($q) ) if ($q); |
|
$cond->set_max( $args->{max} ) if ($args->{max}); |
|
159 |
$cond->set_options( $args->{options} ) if ($args->{options}); |
$cond->set_options( $args->{options} ) if ($args->{options}); |
160 |
$cond->set_order( $args->{order} ) if ($args->{order}); |
$cond->set_order( $args->{order} ) if ($args->{order}); |
161 |
|
|
162 |
my $result = $self->{db}->search($cond, 0) || |
my $max = $args->{max} || 7; |
163 |
$log->die("can't search for ", sub { Dumper( $args ) }); |
my $page = $args->{page} || 1; |
164 |
|
if ($page < 1) { |
165 |
|
$log->warn("page number $page < 1"); |
166 |
|
$page = 1; |
167 |
|
} |
168 |
|
|
169 |
|
$cond->set_max( $page * $max ); |
170 |
|
|
171 |
|
my $result = $self->{db}->search($cond, ( $args->{depth} || 0 )) || |
172 |
|
$log->logdie("can't search for ", sub { Dumper( $args ) }); |
173 |
|
|
174 |
my $hits = $result->doc_num; |
my $hits = $result->doc_num; |
175 |
$log->debug("found $hits hits for '$q'"); |
$log->debug("found $hits hits for '$q'"); |
176 |
|
|
177 |
my @results; |
my @results; |
178 |
|
|
179 |
for my $i ( 0 .. ( $hits - 1 ) ) { |
for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { |
180 |
|
|
181 |
#$log->debug("get_doc($i)"); |
#$log->debug("get_doc($i)"); |
182 |
my $doc = $result->get_doc( $i ); |
my $doc = $result->get_doc( $i ); |
190 |
foreach my $attr (@{ $args->{get_attr} }) { |
foreach my $attr (@{ $args->{get_attr} }) { |
191 |
my $val = $doc->attr( $attr ); |
my $val = $doc->attr( $attr ); |
192 |
#$log->debug("attr $attr = ", $val || 'undef'); |
#$log->debug("attr $attr = ", $val || 'undef'); |
193 |
$hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val)); |
$hash->{$attr} = $self->convert( $val ) if (defined($val)); |
194 |
} |
} |
195 |
|
|
196 |
if ($hash) { |
if ($hash) { |
232 |
} |
} |
233 |
} |
} |
234 |
|
|
235 |
|
=head2 convert |
236 |
|
|
237 |
|
convert internal encoding to UTF-8 |
238 |
|
|
239 |
|
my $utf8 = $self->convert( $text ); |
240 |
|
|
241 |
|
=cut |
242 |
|
|
243 |
|
sub convert { |
244 |
|
my $self = shift; |
245 |
|
|
246 |
|
my $text = shift || return; |
247 |
|
|
248 |
|
from_to($text, $self->{encoding}, 'UTF-8'); |
249 |
|
return $text; |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
=head2 _get_logger |
=head2 _get_logger |
254 |
|
|
255 |
For compatibility with same method from L<WebPAC::Common>, but without |
For compatibility with same method from L<WebPAC::Common>, but without |