/[webpac2]/trunk/lib/WebPAC/Search/Estraier.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Search/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 81 by dpavlin, Mon Nov 21 14:42:22 2005 UTC revision 123 by dpavlin, Wed Nov 23 21:53:01 2005 UTC
# Line 13  WebPAC::Search::Estraier - search Hyper Line 13  WebPAC::Search::Estraier - search Hyper
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    
# Line 100  sub new { Line 100  sub new {
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
# Line 115  sub search { Line 125  sub search {
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          $log->logconfess("need attr as array not " . ref($args->{'attr'}) ) unless (ref($args->{'attr'}) eq 'ARRAY');          $self->confess("need get_attr as array not " . ref($args->{get_attr}) ) unless (ref($args->{get_attr}) eq 'ARRAY');
132    
133          my $cond = HyperEstraier::Condition->new();          my $q = $args->{phrase};
 #       $cond->add_attr("filepath ISTRINC $q");  
134    
135          $cond->set_phrase( $args->{'query'} ) if ($args->{'query'});          $log->debug("args: " . Dumper( $args ));
136          $cond->set_max( $args->{'max'} ) if ($args->{'max'});  
137  #       $cond->set_options( $HyperEstraier::Condition::SURE );          my $cond = HyperEstraier::Condition->new();
138  #       $cond->set_order( 'NUMD' );          if ( ref($args->{add_attr}) eq 'ARRAY' ) {
139                    $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) );
140                    map {
141                            $cond->add_attr( $self->{iconv}->convert( $_ ) );
142                            $log->debug(" + $_");
143                    } @{ $args->{add_attr} };
144            };
145    
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});
150    
151          my $result = $self->{'db'}->search($cond, 0) ||          my $result = $self->{db}->search($cond, 0) ||
152                  $log->die("can't search for ", sub { Dumper( $args ) });                  $log->die("can't search for ", sub { Dumper( $args ) });
153    
154          my $hits = $result->doc_num;          my $hits = $result->doc_num;
155          $log->debug("found $hits hits");          $log->debug("found $hits hits for '$q'");
156    
157          my @results;          my @results;
158    
159          for my $i ( 0 .. ( $hits - 1 ) ) {          for my $i ( 0 .. ( $hits - 1 ) ) {
160    
161                  $log->debug("get_doc($i)");                  #$log->debug("get_doc($i)");
162                  my $doc = $result->get_doc( $i );                  my $doc = $result->get_doc( $i );
163                  if (! $doc) {                  if (! $doc) {
164                          $log->warn("can't find result $i");                          $log->warn("can't find result $i");
# Line 147  sub search { Line 167  sub search {
167    
168                  my $hash;                  my $hash;
169    
170                  foreach my $attr (@{ $args->{'attr'} }) {                  foreach my $attr (@{ $args->{get_attr} }) {
171                          my $val = $doc->attr( $attr );                          my $val = $doc->attr( $attr );
172                          $log->debug("attr $attr = ", $val || 'undef');                          #$log->debug("attr $attr = ", $val || 'undef');
173                          $hash->{$attr} = $self->{'iconv'}->convert( $val ) if (defined($val));                          $hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val));
174                  }                  }
175    
176                  if ($hash) {                  if ($hash) {
# Line 159  sub search { Line 179  sub search {
179    
180          }          }
181    
182          $log->debug("results " . Dumper( \@results ));  #       $log->debug("results " . Dumper( \@results ));
183    
184          $log->logconfess("expected to return array") unless (wantarray);          $self->confess("expected to return array") unless (wantarray);
185    
186          return @results;          return @results;
187  }  }
# Line 177  C<die>. Line 197  C<die>.
197  sub confess {  sub confess {
198          my $self = shift;          my $self = shift;
199          if (my $log = $self->{'log'}) {          if (my $log = $self->{'log'}) {
200                  if ($log->can('confess')) {                  if ($log->can('logconfess')) {
201                          $log->confess(@_);                          $log->logconfess(@_);
202                  } elsif ($log->can('fatal')) {                  } elsif ($log->can('fatal')) {
203                          $log->fatal(@_);                          $log->fatal(@_);
204                            die @_;
205                  } elsif ($log->can('error')) {                  } elsif ($log->can('error')) {
206                          $log->error(@_);                          $log->error(@_);
207                  } else {                  } else {

Legend:
Removed from v.81  
changed lines
  Added in v.123

  ViewVC Help
Powered by ViewVC 1.1.26