13 |
|
|
14 |
=head1 VERSION |
=head1 VERSION |
15 |
|
|
16 |
Version 0.01 |
Version 0.04 |
17 |
|
|
18 |
=cut |
=cut |
19 |
|
|
20 |
our $VERSION = '0.01'; |
our $VERSION = '0.04'; |
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 |
|
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
89 |
|
$url =~ s#//#/#g; |
90 |
|
$self->{url} = $url; |
91 |
|
|
92 |
$log->info("opening Hyper Estraier index $self->{'url'} as $self->{'user'}"); |
$log->info("opening Hyper Estraier index $self->{'url'} as $self->{'user'}"); |
93 |
|
|
94 |
$self->{'db'} = HyperEstraier::Node->new($self->{'url'}); |
$self->{'db'} = HyperEstraier::Node->new($self->{'url'}); |
109 |
Locate items in index |
Locate items in index |
110 |
|
|
111 |
my @results = $est->search( |
my @results = $est->search( |
112 |
query => 'name of book or novel', |
phrase => 'name of book or novel', |
113 |
attr => qw/PersonalName TitleProper/, |
add_attr => [ |
114 |
|
"filepath ISTRINC $q", |
115 |
|
"size NUMGT 100", |
116 |
|
], |
117 |
|
get_attr => qw/PersonalName TitleProper/, |
118 |
|
order => 'NUMD', |
119 |
|
max => 100, |
120 |
|
options => $HyperEstraier::Condition::SURE, |
121 |
|
page => 42, |
122 |
); |
); |
123 |
|
|
124 |
|
Options are close match to Hyper Estraier API, except C<get_attr> which defines |
125 |
|
attributes which will be returned in hash for each record. |
126 |
|
|
127 |
Results are returned as hash array with keys named by attributes |
Results are returned as hash array with keys named by attributes |
128 |
|
|
129 |
|
Pages are numbered C< 1 ... hits/max >. |
130 |
|
|
131 |
=cut |
=cut |
132 |
|
|
133 |
sub search { |
sub search { |
137 |
|
|
138 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
139 |
|
|
140 |
$log->logconfess('need db in object') unless ($self->{'db'}); |
#$log->debug( 'search args: ' . Dumper($args) ); |
|
$log->logconfess('need attr') unless ($args->{'attr'}); |
|
141 |
|
|
142 |
$log->logconfess("need attr as array not " . ref($args->{'attr'}) ) unless (ref($args->{'attr'}) eq 'ARRAY'); |
$self->confess('need db in object') unless ($self->{db}); |
143 |
|
$self->confess('need get_attr') unless ($args->{get_attr}); |
144 |
|
|
145 |
|
$self->confess("need get_attr as array not " . ref($args->{get_attr}) ) unless (ref($args->{get_attr}) eq 'ARRAY'); |
146 |
|
|
147 |
|
my $q = $args->{phrase}; |
148 |
|
|
149 |
|
$log->debug("args: " . Dumper( $args )); |
150 |
|
|
151 |
my $cond = HyperEstraier::Condition->new(); |
my $cond = HyperEstraier::Condition->new(); |
152 |
# $cond->add_attr("filepath ISTRINC $q"); |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
153 |
|
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
154 |
|
map { |
155 |
|
$cond->add_attr( $self->{iconv}->convert( $_ ) ); |
156 |
|
$log->debug(" + $_"); |
157 |
|
} @{ $args->{add_attr} }; |
158 |
|
}; |
159 |
|
|
160 |
|
$cond->set_phrase( $self->{iconv}->convert($q) ) if ($q); |
161 |
|
$cond->set_options( $args->{options} ) if ($args->{options}); |
162 |
|
$cond->set_order( $args->{order} ) if ($args->{order}); |
163 |
|
|
164 |
|
my $max = $args->{max} || 7; |
165 |
|
my $page = $args->{page} || 1; |
166 |
|
if ($page < 1) { |
167 |
|
$log->warn("page number $page < 1"); |
168 |
|
$page = 1; |
169 |
|
} |
170 |
|
|
171 |
$cond->set_phrase( $args->{'query'} ) if ($args->{'query'}); |
$cond->set_max( $page * $max ); |
|
$cond->set_max( $args->{'max'} ) if ($args->{'max'}); |
|
|
# $cond->set_options( $HyperEstraier::Condition::SURE ); |
|
|
# $cond->set_order( 'NUMD' ); |
|
172 |
|
|
173 |
my $result = $self->{'db'}->search($cond, 0) || |
my $result = $self->{db}->search($cond, 0) || |
174 |
$log->die("can't search for ", sub { Dumper( $args ) }); |
$log->die("can't search for ", sub { Dumper( $args ) }); |
175 |
|
|
176 |
my $hits = $result->doc_num; |
my $hits = $result->doc_num; |
177 |
$log->debug("found $hits hits"); |
$log->debug("found $hits hits for '$q'"); |
178 |
|
|
179 |
my @results; |
my @results; |
180 |
|
|
181 |
for my $i ( 0 .. ( $hits - 1 ) ) { |
for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { |
182 |
|
|
183 |
$log->debug("get_doc($i)"); |
#$log->debug("get_doc($i)"); |
184 |
my $doc = $result->get_doc( $i ); |
my $doc = $result->get_doc( $i ); |
185 |
if (! $doc) { |
if (! $doc) { |
186 |
$log->warn("can't find result $i"); |
$log->warn("can't find result $i"); |
189 |
|
|
190 |
my $hash; |
my $hash; |
191 |
|
|
192 |
foreach my $attr (@{ $args->{'attr'} }) { |
foreach my $attr (@{ $args->{get_attr} }) { |
193 |
my $val = $doc->attr( $attr ); |
my $val = $doc->attr( $attr ); |
194 |
$log->debug("attr $attr = ", $val || 'undef'); |
#$log->debug("attr $attr = ", $val || 'undef'); |
195 |
$hash->{$attr} = $self->{'iconv'}->convert( $val ) if (defined($val)); |
$hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val)); |
196 |
} |
} |
197 |
|
|
198 |
if ($hash) { |
if ($hash) { |
201 |
|
|
202 |
} |
} |
203 |
|
|
204 |
$log->debug("results " . Dumper( \@results )); |
# $log->debug("results " . Dumper( \@results )); |
205 |
|
|
206 |
$log->logconfess("expected to return array") unless (wantarray); |
$self->confess("expected to return array") unless (wantarray); |
207 |
|
|
208 |
return @results; |
return @results; |
209 |
} |
} |
219 |
sub confess { |
sub confess { |
220 |
my $self = shift; |
my $self = shift; |
221 |
if (my $log = $self->{'log'}) { |
if (my $log = $self->{'log'}) { |
222 |
if ($log->can('confess')) { |
if ($log->can('logconfess')) { |
223 |
$log->confess(@_); |
$log->logconfess(@_); |
224 |
} elsif ($log->can('fatal')) { |
} elsif ($log->can('fatal')) { |
225 |
$log->fatal(@_); |
$log->fatal(@_); |
226 |
|
die @_; |
227 |
} elsif ($log->can('error')) { |
} elsif ($log->can('error')) { |
228 |
$log->error(@_); |
$log->error(@_); |
229 |
} else { |
} else { |