--- Webpacus/lib/Webpacus/Controller/Search.pm 2005/11/23 21:52:20 116 +++ Webpacus/lib/Webpacus/Controller/Search.pm 2005/11/26 16:37:04 161 @@ -4,20 +4,23 @@ use warnings; use base 'Catalyst::Controller'; +use Data::Dumper; + use lib '/data/webpac2/lib'; -use WebPAC::Search::Estraier; +use WebPAC::Search::Estraier 0.03; + =head1 NAME -Webpacus::Controller::Search - Catalyst Controller +Webpacus::Controller::Search - Search WebPAC data =head1 SYNOPSIS -See L +See L, L =head1 DESCRIPTION -Catalyst Controller for autocompleting search fields. +Catalyst Controller for search fields Hyper Estraier =head1 METHODS @@ -36,39 +39,142 @@ Returns results for URLs like C +It will return field from URL (C in example>) and use +same filed in paramerers (comming from form or URL) or if it doesn't +exist field named C. + =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 = new WebPAC::Search::Estraier( - url => 'http://localhost:1978/node/webpac2', - user => 'admin', - passwd => 'admin', - encoding => 'UTF-8', - log => $c->log, - ); - + my $webpac = $c->comp('Model::WebPAC'); + + my $q = $c->req->params->{$what} || + $c->req->params->{all} || $c->res->output("no results"); - my $q = $c->req->params->{$what}; - 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 = $c->config->{'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 = $webpac->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 + +Method which uses C and returns results for search query + +=cut + +sub results : Local { + my ( $self, $c ) = @_; - foreach my $res (@hits) { - my $v = $res->{ $what } || next; - next if ($used->{ $v }++); - push @suggestions, $v; - } + my $webpac = $c->comp('Model::WebPAC'); + my $params = $c->req->params; + my $log = $c->log; + + $log->debug("results got params: " . Dumper( $params ) ); + + if ($params->{_page} < 1) { + $params->{_page} = 1; + $log->warn("fixed _page parametar to 1"); + } + + 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'} || $c->config->{webpac}->{template}; + + $log->die("can't find _template or default from configuration!") unless ($template); + + my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10; + + $log->debug("using template $template to produce $hits_on_page results"); + + $c->stash->{html_results} = sub { + my $res = $webpac->search( + phrase => $q, + template => $template, + add_attr => \@attr, + get_attr => [ '@uri' ], + max => $hits_on_page, + page => $params->{'_page'}, + ); +# $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); + return $res; + }; + + $c->stash->{phrase} = $q; + $c->stash->{attr} = \@attr; + $c->stash->{page} = $params->{'_page'}; + $c->stash->{hits_on_page} = $hits_on_page; - $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); + $c->stash->{template} = 'results.tt'; } =back @@ -76,7 +182,7 @@ =head1 AUTHOR -Dobrica Pavlinusic,,, +Dobrica Pavlinusic C<< >> =head1 LICENSE