--- Webpacus/lib/Webpacus/Controller/Search.pm 2005/12/17 00:37:12 270 +++ Webpacus/lib/Webpacus/Controller/Search.pm 2006/01/22 02:52:24 382 @@ -40,28 +40,34 @@ =item suggest -Returns results for URLs like C +Returns results for REST URIs like: -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. +C + +It will use C field to filter results (and using additional +C as value of search) and return C field +for results. + +If C field has magic value , it will search over all data, not +just one specified field: + +C =cut -sub suggest : Regex('^search/suggest/*([^/]*)') { +sub suggest : Local { my ( $self, $c ) = @_; - my $what = $c->request->snippets->[0]; + my $search = $c->req->params->{search}; + my $show = $c->req->params->{show}; my $log = $c->log; my $webpac = $c->comp('Model::WebPAC'); - my $q = $c->req->params->{$what} || - $c->req->params->{all} || $c->response->body("no results"); + my $q = $c->req->params->{ $search || 'all' } || $c->response->body("no results"); - - $log->info("search for '$q' in $what\n"); + $log->info("search for '$q' in $search and display $show\n"); my $max = $c->config->{'hits_for_suggest'}; if (! $max) { @@ -70,17 +76,20 @@ $max = 10; } - my @hits = $webpac->search( + $c->forward('filter_database'); + + my $hits = $webpac->search( phrase => $q, + add_attr => $c->stash->{attr}, + get_attr => [ $show ], max => $max, - get_attr => [ $what ], ); my $used; my @suggestions; - foreach my $res (@hits) { - my $v = $res->{ $what } || next; + foreach my $res (@{$hits}) { + my $v = $res->{ $show } || next; next if ($used->{ $v }++); push @suggestions, $v; } @@ -102,15 +111,9 @@ my $params = $c->req->params; - # oh, client didn't have ability to setup AJAX, fallback to - # page refresh - if (! $params->{'_ajax'}) { - $params->{_ajax} = 1; - $c->stash->{results} = $c->subreq('/search/results/ajax', {}, $params); - $c->stash->{template} = 'search.tt'; - } else { - $c->forward('/search/results/ajax'); - } + # do full-page refresh for clients without JavaScript + $c->stash->{results} = $c->subreq('/search/results/ajax', {}, $params); + $c->stash->{template} = 'search.tt'; } @@ -124,7 +127,7 @@ # 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)/o; +my $hest_op_regex = '(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)'; sub results_ajax : Path( 'results/ajax' ) { my ( $self, $c ) = @_; @@ -140,7 +143,6 @@ $log->warn("fixed _page parametar to 1"); } - my @attr; my @words; # default operator to join fields/words my $operator = 'AND'; @@ -152,7 +154,7 @@ my $v = $params->{$f} || next; if (my $op = $params->{ '_' . $f}) { - if ($v =~ $hest_op_regex) { + if ($v =~ /$hest_op_regex/) { # don't split words if there is Hyper Estraier # operator in them push @words, $v; @@ -166,14 +168,16 @@ next if ($f eq 'all'); # don't add_attr for magic field all if ($v !~ /\s/) { - push @attr, "$f ISTRINC $v"; + push @{ $c->stash->{attr} }, "$f ISTRINC $v"; } else { map { - push @attr, "$f ISTRINC $_"; - } grep { ! $hest_op_regex } split(/\s+/, $v); + push @{ $c->stash->{attr} }, "$f ISTRINC $_"; + } grep { ! /$hest_op_regex/ } split(/\s+/, $v); } } + $c->forward('filter_database'); + my $q = join(" $operator ", @words); my $template = $params->{'_template'} || $c->config->{webpac}->{template}; @@ -188,7 +192,7 @@ my $res = $webpac->search( phrase => $q, template => $template, - add_attr => \@attr, + add_attr => $c->{stash}->{attr}, get_attr => [ '@uri' ], max => $hits_on_page, page => $params->{'_page'}, @@ -198,7 +202,6 @@ }; $c->stash->{phrase} = $q; - $c->stash->{attr} = \@attr; $c->stash->{page} = $params->{'_page'}; $c->stash->{hits_on_page} = $hits_on_page; @@ -206,6 +209,34 @@ } +=item filter_database + +Takes C<< $c->req->params >> and adds Hyper Estraier +filters for checked databases to C<< $c->stash->{attr} >>. + +=cut + +sub filter_database : Private { + my ( $self, $c ) = @_; + + my $params = $c->req->params; + + if ($params->{_database}) { + my $type = $c->config->{hyperstraier}->{type} || 'search'; + my $attr; + if (ref($params->{_database}) eq 'ARRAY') { + # FIXME do we need to add $ at end? + $attr .= '(' . join("|",@{$params->{_database}}) . ')'; + } else { + $attr .= $params->{_database}; + } + push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/'; + $c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) ); + } + + +} + =item record forwarded to C