1 |
package Webpacus::Controller::Search; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use base 'Catalyst::Controller'; |
6 |
|
7 |
use lib '/data/webpac2/lib'; |
8 |
use WebPAC::Search::Estraier; |
9 |
|
10 |
=head1 NAME |
11 |
|
12 |
Webpacus::Controller::Search - Catalyst Controller |
13 |
|
14 |
=head1 SYNOPSIS |
15 |
|
16 |
See L<Webpacus> |
17 |
|
18 |
=head1 DESCRIPTION |
19 |
|
20 |
Catalyst Controller for autocompleting search fields. |
21 |
|
22 |
=head1 METHODS |
23 |
|
24 |
=over 4 |
25 |
|
26 |
=item default |
27 |
|
28 |
=cut |
29 |
|
30 |
sub default : Private { |
31 |
my ( $self, $c ) = @_; |
32 |
$c->stash->{template} = 'search.tt'; |
33 |
} |
34 |
|
35 |
=item suggest |
36 |
|
37 |
Returns results for URLs like C<search/suggest/FieldName> |
38 |
|
39 |
=cut |
40 |
|
41 |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
42 |
my ( $self, $c ) = @_; |
43 |
|
44 |
my $what = $c->request->snippets->[0]; |
45 |
|
46 |
my $log = $c->log; |
47 |
|
48 |
my $est_conf = $c->config->{hyperestraier}; |
49 |
$est_conf->{log} = $c->log; |
50 |
|
51 |
my $est = new WebPAC::Search::Estraier( %{ $est_conf } ); |
52 |
|
53 |
my $q = $c->req->params->{$what} || |
54 |
$c->req->params->{all} || $c->res->output("no results"); |
55 |
|
56 |
|
57 |
$log->info("search for '$q' in $what\n"); |
58 |
|
59 |
my $max = $est_conf->{'hits_for_suggest'}; |
60 |
if (! $max) { |
61 |
$log->info("hits_for_suggest isn't defined, defaulting to 10"); |
62 |
$c->config->{'hits_for_suggest'} = 10; |
63 |
$max = 10; |
64 |
} |
65 |
|
66 |
my @hits = $est->search( phrase => $q, max => $max, get_attr => [ $what ] ); |
67 |
|
68 |
my $used; |
69 |
my @suggestions; |
70 |
|
71 |
foreach my $res (@hits) { |
72 |
my $v = $res->{ $what } || next; |
73 |
next if ($used->{ $v }++); |
74 |
push @suggestions, $v; |
75 |
} |
76 |
|
77 |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
78 |
|
79 |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
80 |
} |
81 |
|
82 |
=back |
83 |
|
84 |
|
85 |
=head1 AUTHOR |
86 |
|
87 |
Dobrica Pavlinusic,,, |
88 |
|
89 |
=head1 LICENSE |
90 |
|
91 |
This library is free software, you can redistribute it and/or modify |
92 |
it under the same terms as Perl itself. |
93 |
|
94 |
=cut |
95 |
|
96 |
1; |