--- trunk/lib/WebPAC/Search/Estraier.pm 2005/11/21 14:42:22 81 +++ trunk/lib/WebPAC/Search/Estraier.pm 2005/11/26 01:38:28 152 @@ -13,11 +13,11 @@ =head1 VERSION -Version 0.01 +Version 0.02 =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 SYNOPSIS @@ -100,10 +100,20 @@ Locate items in index my @results = $est->search( - query => 'name of book or novel', - attr => qw/PersonalName TitleProper/, + phrase => 'name of book or novel', + add_attr => [ + "filepath ISTRINC $q", + "size NUMGT 100", + ], + get_attr => qw/PersonalName TitleProper/, + order => 'NUMD', + max => 100, + options => $HyperEstraier::Condition::SURE, ); +Options are close match to Hyper Estraier API, except C which defines +attributes which will be returned in hash for each record. + Results are returned as hash array with keys named by attributes =cut @@ -115,30 +125,42 @@ my $log = $self->_get_logger; - $log->logconfess('need db in object') unless ($self->{'db'}); - $log->logconfess('need attr') unless ($args->{'attr'}); + #$log->debug( 'search args: ' . Dumper($args) ); - $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}); + $self->confess('need get_attr') unless ($args->{get_attr}); - my $cond = HyperEstraier::Condition->new(); -# $cond->add_attr("filepath ISTRINC $q"); + $self->confess("need get_attr as array not " . ref($args->{get_attr}) ) unless (ref($args->{get_attr}) eq 'ARRAY'); + + my $q = $args->{phrase}; - $cond->set_phrase( $args->{'query'} ) if ($args->{'query'}); - $cond->set_max( $args->{'max'} ) if ($args->{'max'}); -# $cond->set_options( $HyperEstraier::Condition::SURE ); -# $cond->set_order( 'NUMD' ); + $log->debug("args: " . Dumper( $args )); + + my $cond = HyperEstraier::Condition->new(); + if ( ref($args->{add_attr}) eq 'ARRAY' ) { + $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); + map { + $cond->add_attr( $self->{iconv}->convert( $_ ) ); + $log->debug(" + $_"); + } @{ $args->{add_attr} }; + }; + + $cond->set_phrase( $self->{iconv}->convert($q) ) if ($q); + $cond->set_max( $args->{max} ) if ($args->{max}); + $cond->set_options( $args->{options} ) if ($args->{options}); + $cond->set_order( $args->{order} ) if ($args->{order}); - my $result = $self->{'db'}->search($cond, 0) || + my $result = $self->{db}->search($cond, 0) || $log->die("can't search for ", sub { Dumper( $args ) }); my $hits = $result->doc_num; - $log->debug("found $hits hits"); + $log->debug("found $hits hits for '$q'"); my @results; for my $i ( 0 .. ( $hits - 1 ) ) { - $log->debug("get_doc($i)"); + #$log->debug("get_doc($i)"); my $doc = $result->get_doc( $i ); if (! $doc) { $log->warn("can't find result $i"); @@ -147,10 +169,10 @@ my $hash; - foreach my $attr (@{ $args->{'attr'} }) { + foreach my $attr (@{ $args->{get_attr} }) { my $val = $doc->attr( $attr ); - $log->debug("attr $attr = ", $val || 'undef'); - $hash->{$attr} = $self->{'iconv'}->convert( $val ) if (defined($val)); + #$log->debug("attr $attr = ", $val || 'undef'); + $hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val)); } if ($hash) { @@ -159,9 +181,9 @@ } - $log->debug("results " . Dumper( \@results )); +# $log->debug("results " . Dumper( \@results )); - $log->logconfess("expected to return array") unless (wantarray); + $self->confess("expected to return array") unless (wantarray); return @results; } @@ -177,10 +199,11 @@ sub confess { my $self = shift; if (my $log = $self->{'log'}) { - if ($log->can('confess')) { - $log->confess(@_); + if ($log->can('logconfess')) { + $log->logconfess(@_); } elsif ($log->can('fatal')) { $log->fatal(@_); + die @_; } elsif ($log->can('error')) { $log->error(@_); } else {