--- Webpacus/lib/Webpacus/Controller/Search.pm 2005/11/23 21:52:25 117 +++ Webpacus/lib/Webpacus/Controller/Search.pm 2005/11/26 01:54:31 153 @@ -4,9 +4,12 @@ use warnings; use base 'Catalyst::Controller'; +use Data::Dumper; + use lib '/data/webpac2/lib'; use WebPAC::Search::Estraier; + =head1 NAME Webpacus::Controller::Search - Catalyst Controller @@ -39,38 +42,119 @@ =cut sub suggest : Regex('^search/suggest/*([^/]*)') { - my ( $self, $c ) = @_; + my ( $self, $c ) = @_; + + my $what = $c->request->snippets->[0]; - my $what = $c->request->snippets->[0]; + my $log = $c->log; - my $log = $c->log; + my $est_conf = $c->config->{hyperestraier}; + $est_conf->{log} = $c->log; - my $est = new WebPAC::Search::Estraier( - url => 'http://localhost:1978/node/webpac2', - user => 'admin', - passwd => 'admin', - encoding => 'UTF-8', - log => $c->log, - ); - + my $est = new WebPAC::Search::Estraier( %{ $est_conf } ); - my $q = $c->req->params->{$what} || - $c->req->params->{all} || $c->res->output("no results"); + my $q = $c->req->params->{$what} || + $c->req->params->{all} || $c->res->output("no results"); - my @suggestions; $log->info("search for '$q' in $what\n"); - my @hits = $est->search( phrase => $q, max => 10, get_attr => [ $what ] ); - my $used; + my $max = $est_conf->{'hits_for_suggest'}; + if (! $max) { + $log->info("hits_for_suggest isn't defined, defaulting to 10"); + $c->config->{'hits_for_suggest'} = 10; + $max = 10; + } + + my @hits = $est->search( phrase => $q, max => $max, get_attr => [ $what ] ); + + my $used; + my @suggestions; + + foreach my $res (@hits) { + my $v = $res->{ $what } || next; + next if ($used->{ $v }++); + push @suggestions, $v; + } + + $log->debug( ($#suggestions + 1) . " unique hits returned"); + + $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); +} + + +# specify all Hyper Estraier operators which should stop this module +# from splitting search query and joining it with default operator +my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/; + +=item results + +Mothod which uses C and returns results for search query + +=cut + +sub results : Local { + my ( $self, $c ) = @_; + + my $webpac = $c->comp('Model::WebPAC'); + my $params = $c->req->params; + my $log = $c->log; + + $log->debug("results got params: " . Dumper( $params ) ); + + my @attr; + my @words; + # default operator to join fields/words + my $operator = 'AND'; + + foreach my $f (keys %{ $params }) { + + next if ($f =~ m/^_/o); + + my $v = $params->{$f} || next; + + if (my $op = $params->{ '_' . $f}) { + if ($v =~ $hest_op_regex) { + # don't split words if there is Hyper Estraier + # operator in them + push @words, $v; + } else { + push @words, join(" $op ", split(/\s+/, $v) ); + } + } else { + push @words, $v; + } + + next if ($f eq 'all'); # don't add_attr for magic field all + + if ($v !~ /\s/) { + push @attr, "$f ISTRINC $v"; + } else { + map { + push @attr, "$f ISTRINC $_"; + } grep { ! $hest_op_regex } split(/\s+/, $v); + } + } + + my $q = join(" $operator ", @words); + + my $template = $params->{'_template'}; + $log->debug("using template $template"); + + $c->stash->{html_results} = sub { + my $res = $webpac->search( + phrase => $q, + template => $template, + add_attr => \@attr + ); + # $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); + return $res; + }; - foreach my $res (@hits) { - my $v = $res->{ $what } || next; - next if ($used->{ $v }++); - push @suggestions, $v; - } + $c->stash->{phrase} = $q; + $c->stash->{attr} = \@attr; - $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); + $c->stash->{template} = 'results.tt'; } =back @@ -78,7 +162,7 @@ =head1 AUTHOR -Dobrica Pavlinusic,,, +Dobrica Pavlinusic C<< >> =head1 LICENSE