13 |
|
|
14 |
=head1 VERSION |
=head1 VERSION |
15 |
|
|
16 |
Version 0.01 |
Version 0.02 |
17 |
|
|
18 |
=cut |
=cut |
19 |
|
|
20 |
our $VERSION = '0.01'; |
our $VERSION = '0.02'; |
21 |
|
|
22 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
23 |
|
|
100 |
Locate items in index |
Locate items in index |
101 |
|
|
102 |
my @results = $est->search( |
my @results = $est->search( |
103 |
query => 'name of book or novel', |
phrase => 'name of book or novel', |
104 |
attr => qw/PersonalName TitleProper/, |
add_attr => [ |
105 |
|
"filepath ISTRINC $q", |
106 |
|
"size NUMGT 100", |
107 |
|
], |
108 |
|
get_attr => qw/PersonalName TitleProper/, |
109 |
|
order => 'NUMD', |
110 |
|
max => 100, |
111 |
|
options => $HyperEstraier::Condition::SURE, |
112 |
); |
); |
113 |
|
|
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. |
116 |
|
|
117 |
Results are returned as hash array with keys named by attributes |
Results are returned as hash array with keys named by attributes |
118 |
|
|
119 |
=cut |
=cut |
125 |
|
|
126 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
127 |
|
|
128 |
$log->logconfess('need db in object') unless ($self->{'db'}); |
$self->confess('need db in object') unless ($self->{db}); |
129 |
$log->logconfess('need attr') unless ($args->{'attr'}); |
$self->confess('need get_attr') unless ($args->{get_attr}); |
130 |
|
|
131 |
|
$self->confess("need get_attr as array not " . ref($args->{get_attr}) ) unless (ref($args->{get_attr}) eq 'ARRAY'); |
132 |
|
|
133 |
$log->logconfess("need attr as array not " . ref($args->{'attr'}) ) unless (ref($args->{'attr'}) eq 'ARRAY'); |
my $q = $args->{phrase}; |
134 |
|
|
135 |
my $cond = HyperEstraier::Condition->new(); |
my $cond = HyperEstraier::Condition->new(); |
136 |
# $cond->add_attr("filepath ISTRINC $q"); |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
137 |
|
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
138 |
|
map { |
139 |
|
$cond->add_attr( $self->{iconv}->conver( $_ ) ); |
140 |
|
$log->debug(" + $_"); |
141 |
|
} $args->{add_attr}; |
142 |
|
} |
143 |
|
|
144 |
$cond->set_phrase( $args->{'query'} ) if ($args->{'query'}); |
$cond->set_phrase( $self->{iconv}->convert($q) ) if ($q); |
145 |
$cond->set_max( $args->{'max'} ) if ($args->{'max'}); |
$cond->set_max( $args->{max} ) if ($args->{max}); |
146 |
# $cond->set_options( $HyperEstraier::Condition::SURE ); |
$cond->set_options( $args->{options} ) if ($args->{options}); |
147 |
# $cond->set_order( 'NUMD' ); |
$cond->set_order( $args->{order} ) if ($args->{order}); |
148 |
|
|
149 |
my $result = $self->{'db'}->search($cond, 0) || |
my $result = $self->{db}->search($cond, 0) || |
150 |
$log->die("can't search for ", sub { Dumper( $args ) }); |
$log->die("can't search for ", sub { Dumper( $args ) }); |
151 |
|
|
152 |
my $hits = $result->doc_num; |
my $hits = $result->doc_num; |
153 |
$log->debug("found $hits hits"); |
$log->debug("found $hits hits for '$q'"); |
154 |
|
|
155 |
my @results; |
my @results; |
156 |
|
|
157 |
for my $i ( 0 .. ( $hits - 1 ) ) { |
for my $i ( 0 .. ( $hits - 1 ) ) { |
158 |
|
|
159 |
$log->debug("get_doc($i)"); |
#$log->debug("get_doc($i)"); |
160 |
my $doc = $result->get_doc( $i ); |
my $doc = $result->get_doc( $i ); |
161 |
if (! $doc) { |
if (! $doc) { |
162 |
$log->warn("can't find result $i"); |
$log->warn("can't find result $i"); |
165 |
|
|
166 |
my $hash; |
my $hash; |
167 |
|
|
168 |
foreach my $attr (@{ $args->{'attr'} }) { |
foreach my $attr (@{ $args->{get_attr} }) { |
169 |
my $val = $doc->attr( $attr ); |
my $val = $doc->attr( $attr ); |
170 |
$log->debug("attr $attr = ", $val || 'undef'); |
#$log->debug("attr $attr = ", $val || 'undef'); |
171 |
$hash->{$attr} = $self->{'iconv'}->convert( $val ) if (defined($val)); |
$hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val)); |
172 |
} |
} |
173 |
|
|
174 |
if ($hash) { |
if ($hash) { |
177 |
|
|
178 |
} |
} |
179 |
|
|
180 |
$log->debug("results " . Dumper( \@results )); |
# $log->debug("results " . Dumper( \@results )); |
181 |
|
|
182 |
$log->logconfess("expected to return array") unless (wantarray); |
$self->confess("expected to return array") unless (wantarray); |
183 |
|
|
184 |
return @results; |
return @results; |
185 |
} |
} |
195 |
sub confess { |
sub confess { |
196 |
my $self = shift; |
my $self = shift; |
197 |
if (my $log = $self->{'log'}) { |
if (my $log = $self->{'log'}) { |
198 |
if ($log->can('confess')) { |
if ($log->can('logconfess')) { |
199 |
$log->confess(@_); |
$log->logconfess(@_); |
200 |
} elsif ($log->can('fatal')) { |
} elsif ($log->can('fatal')) { |
201 |
$log->fatal(@_); |
$log->fatal(@_); |
202 |
|
die @_; |
203 |
} elsif ($log->can('error')) { |
} elsif ($log->can('error')) { |
204 |
$log->error(@_); |
$log->error(@_); |
205 |
} else { |
} else { |