/[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 86 by dpavlin, Tue Nov 22 08:37:40 2005 UTC revision 177 by dpavlin, Sun Nov 27 05:02:53 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.03
17    
18  =cut  =cut
19    
20  our $VERSION = '0.01';  our $VERSION = '0.03';
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            page => 42,
113    );    );
114    
115    Options are close match to Hyper Estraier API, except C<get_attr> which defines
116    attributes which will be returned in hash for each record.
117    
118  Results are returned as hash array with keys named by attributes  Results are returned as hash array with keys named by attributes
119    
120    Pages are numbered C< 1 ... hits/max >.
121    
122  =cut  =cut
123    
124  sub search {  sub search {
# Line 115  sub search { Line 128  sub search {
128    
129          my $log = $self->_get_logger;          my $log = $self->_get_logger;
130    
131          $log->logconfess('need db in object') unless ($self->{'db'});          #$log->debug( 'search args: ' . Dumper($args) );
132          $log->logconfess('need attr') unless ($args->{'attr'});  
133            $self->confess('need db in object') unless ($self->{db});
134            $self->confess('need get_attr') unless ($args->{get_attr});
135    
136          $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');
137    
138          my $q = $args->{'query'};          my $q = $args->{phrase};
139    
140            $log->debug("args: " . Dumper( $args ));
141    
142          my $cond = HyperEstraier::Condition->new();          my $cond = HyperEstraier::Condition->new();
143  #       $cond->add_attr("filepath ISTRINC $q");          if ( ref($args->{add_attr}) eq 'ARRAY' ) {
144                    $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) );
145                    map {
146                            $cond->add_attr( $self->{iconv}->convert( $_ ) );
147                            $log->debug(" + $_");
148                    } @{ $args->{add_attr} };
149            };
150    
151            $cond->set_phrase( $self->{iconv}->convert($q) ) if ($q);
152            $cond->set_options( $args->{options} ) if ($args->{options});
153            $cond->set_order( $args->{order} ) if ($args->{order});
154    
155            my $max = $args->{max} || 7;
156            my $page = $args->{page} || 1;
157            if ($page < 1) {
158                    $log->warn("page number $page < 1");
159                    $page = 1;
160            }
161    
162          $cond->set_phrase( $self->{'iconv'}->convert( $q ) ) if ($q);          $cond->set_max( $page * $max );
         $cond->set_max( $args->{'max'} ) if ($args->{'max'});  
 #       $cond->set_options( $HyperEstraier::Condition::SURE );  
 #       $cond->set_order( 'NUMD' );  
163    
164          my $result = $self->{'db'}->search($cond, 0) ||          my $result = $self->{db}->search($cond, 0) ||
165                  $log->die("can't search for ", sub { Dumper( $args ) });                  $log->die("can't search for ", sub { Dumper( $args ) });
166    
167          my $hits = $result->doc_num;          my $hits = $result->doc_num;
# Line 138  sub search { Line 169  sub search {
169    
170          my @results;          my @results;
171    
172          for my $i ( 0 .. ( $hits - 1 ) ) {          for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) {
173    
174                  #$log->debug("get_doc($i)");                  #$log->debug("get_doc($i)");
175                  my $doc = $result->get_doc( $i );                  my $doc = $result->get_doc( $i );
# Line 149  sub search { Line 180  sub search {
180    
181                  my $hash;                  my $hash;
182    
183                  foreach my $attr (@{ $args->{'attr'} }) {                  foreach my $attr (@{ $args->{get_attr} }) {
184                          my $val = $doc->attr( $attr );                          my $val = $doc->attr( $attr );
185                          #$log->debug("attr $attr = ", $val || 'undef');                          #$log->debug("attr $attr = ", $val || 'undef');
186                          $hash->{$attr} = $self->{'iconv'}->convert( $val ) if (defined($val));                          $hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val));
187                  }                  }
188    
189                  if ($hash) {                  if ($hash) {
# Line 161  sub search { Line 192  sub search {
192    
193          }          }
194    
195          $log->debug("results " . Dumper( \@results ));  #       $log->debug("results " . Dumper( \@results ));
196    
197          $log->logconfess("expected to return array") unless (wantarray);          $self->confess("expected to return array") unless (wantarray);
198    
199          return @results;          return @results;
200  }  }
# Line 179  C<die>. Line 210  C<die>.
210  sub confess {  sub confess {
211          my $self = shift;          my $self = shift;
212          if (my $log = $self->{'log'}) {          if (my $log = $self->{'log'}) {
213                  if ($log->can('confess')) {                  if ($log->can('logconfess')) {
214                          $log->confess(@_);                          $log->logconfess(@_);
215                  } elsif ($log->can('fatal')) {                  } elsif ($log->can('fatal')) {
216                          $log->fatal(@_);                          $log->fatal(@_);
217                            die @_;
218                  } elsif ($log->can('error')) {                  } elsif ($log->can('error')) {
219                          $log->error(@_);                          $log->error(@_);
220                  } else {                  } else {

Legend:
Removed from v.86  
changed lines
  Added in v.177

  ViewVC Help
Powered by ViewVC 1.1.26