/[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 80 by dpavlin, Mon Nov 21 14:42:16 2005 UTC revision 156 by dpavlin, Sat Nov 26 14:37:33 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 35  Connect to Hyper Estraier index using HT Line 35  Connect to Hyper Estraier index using HT
35          user => 'admin',          user => 'admin',
36          passwd => 'admin',          passwd => 'admin',
37          encoding => 'iso-8859-2',          encoding => 'iso-8859-2',
38          log => $Log::Log4Perl->log_object,          log => $Log::Log4perl->log_object,
39   );   );
40    
41  Options are:  Options are:
# Line 60  character encoding of C<data_structure> Line 60  character encoding of C<data_structure>
60  (and it probably is). This encoding will be converted to C<UTF-8> for  (and it probably is). This encoding will be converted to C<UTF-8> for
61  Hyper Estraier.  Hyper Estraier.
62    
63    =item log
64    
65    L<Log::Log4perl> object or equivalent (C<< $c->log >> can be used in
66    L<Catalyst> and there is support for it).
67    
68  =back  =back
69    
70  =cut  =cut
# Line 83  sub new { Line 88  sub new {
88          my $encoding = $self->{'encoding'} || 'ISO-8859-2';          my $encoding = $self->{'encoding'} || 'ISO-8859-2';
89          $log->info("using encoding $encoding");          $log->info("using encoding $encoding");
90    
91          $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or          $self->{'iconv'} = new Text::Iconv('UTF-8', $encoding) or
92                  $log->die("can't create conversion from $encoding to UTF-8");                  $log->die("can't create conversion from UTF-8 to $encoding");
93    
94          $self ? return $self : return undef;          $self ? return $self : return undef;
95  }  }
# Line 94  sub new { Line 99  sub new {
99    
100  Locate items in index  Locate items in index
101    
102    $est->search(    my @results = $est->search(
103          query => 'name of book or novel',          phrase => 'name of book or novel',
104          attr => [ qw/PersonalName Title/ ],          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
119    
120    Pages are numbered C< 1 ... hits/max >.
121    
122  =cut  =cut
123    
124  sub search {  sub search {
# Line 108  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          $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});
134            $self->confess('need get_attr') unless ($args->{get_attr});
135  #       my $database = $self->{'database'} || $log->logconfess('no database in $self');  
136  #       foreach my $p (qw/id ds type/) {          $self->confess("need get_attr as array not " . ref($args->{get_attr}) ) unless (ref($args->{get_attr}) eq 'ARRAY');
137  #               $log->logconfess("need $p") unless ($args->{$p});  
138  #       }          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          $cond->set_phrase( $args->{'query'} ) if ($args->{'query'});          my $max = $args->{max} || 7;
156          $cond->set_max( $args->{'max'} ) if ($args->{'max'});          my $page = $args->{page} || 1;
 #       $cond->set_options( $HyperEstraier::Condition::SURE );  
 #       $cond->set_order( 'NUMD' );  
157    
158          my $result = $self->{'db'}->search($cond, 0) ||          $cond->set_max( $page * $max );
159    
160            my $result = $self->{db}->search($cond, 0) ||
161                  $log->die("can't search for ", sub { Dumper( $args ) });                  $log->die("can't search for ", sub { Dumper( $args ) });
162    
163          my $hits = $result->doc_num;          my $hits = $result->doc_num;
164          $log->debug("found $hits hits");          $log->debug("found $hits hits for '$q'");
   
         my @attrs = $args->{'attr'} || $self->confess("need attr");  
165    
166          my @results;          my @results;
167    
168          for my $i ( 0 .. $hits ) {          for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) {
169    
170                  $log->debug("get_doc($i)");                  #$log->debug("get_doc($i)");
171                  my $doc = $result->get_doc( $i );                  my $doc = $result->get_doc( $i );
172                  if (! $doc) {                  if (! $doc) {
173                          $log->warn("can't find result $i");                          $log->warn("can't find result $i");
# Line 146  sub search { Line 176  sub search {
176    
177                  my $hash;                  my $hash;
178    
179                  foreach my $attr (@attrs) {                  foreach my $attr (@{ $args->{get_attr} }) {
180                          my $val = $doc->attr( $attr );                          my $val = $doc->attr( $attr );
181                          $log->debug("attr $attr = ", $val || 'undef');                          #$log->debug("attr $attr = ", $val || 'undef');
182                          $hash->{$attr} = $val if (defined($val));                          $hash->{$attr} = $self->{iconv}->convert( $val ) if (defined($val));
183                  }                  }
184    
185                  if ($hash) {                  if ($hash) {
# Line 158  sub search { Line 188  sub search {
188    
189          }          }
190    
191          $log->debug("results " . Dumper( \@results ));  #       $log->debug("results " . Dumper( \@results ));
192    
193          $log->logconfess("expected to return array") unless (wantarray);          $self->confess("expected to return array") unless (wantarray);
194    
195          return @results;          return @results;
196  }  }
# Line 176  C<die>. Line 206  C<die>.
206  sub confess {  sub confess {
207          my $self = shift;          my $self = shift;
208          if (my $log = $self->{'log'}) {          if (my $log = $self->{'log'}) {
209                  if ($log->can('confess')) {                  if ($log->can('logconfess')) {
210                          $log->confess(@_);                          $log->logconfess(@_);
211                  } elsif ($log->can('fatal')) {                  } elsif ($log->can('fatal')) {
212                          $log->fatal(@_);                          $log->fatal(@_);
213                            die @_;
214                  } elsif ($log->can('error')) {                  } elsif ($log->can('error')) {
215                          $log->error(@_);                          $log->error(@_);
216                  } else {                  } else {

Legend:
Removed from v.80  
changed lines
  Added in v.156

  ViewVC Help
Powered by ViewVC 1.1.26