39 |
=cut |
=cut |
40 |
|
|
41 |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
42 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
43 |
|
|
44 |
my $what = $c->request->snippets->[0]; |
my $what = $c->request->snippets->[0]; |
45 |
|
|
46 |
my $log = $c->log; |
my $log = $c->log; |
47 |
|
|
48 |
my $est = new WebPAC::Search::Estraier( |
my $est_conf = $c->config->{hyperestraier}; |
49 |
url => 'http://localhost:1978/node/webpac2', |
$est_conf->{log} = $c->log; |
50 |
user => 'admin', |
|
51 |
passwd => 'admin', |
my $est = new WebPAC::Search::Estraier( %{ $est_conf } ); |
52 |
encoding => 'UTF-8', |
|
53 |
log => $c->log, |
my $q = $c->req->params->{$what} || |
54 |
); |
$c->req->params->{all} || $c->res->output("no results"); |
|
|
|
55 |
|
|
|
my $q = $c->req->params->{$what}; |
|
|
my @suggestions; |
|
56 |
|
|
57 |
$log->info("search for '$q' in $what\n"); |
$log->info("search for '$q' in $what\n"); |
|
my @hits = $est->search( query => $q, max => 10, attr => [ $what ] ); |
|
58 |
|
|
59 |
my $used; |
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 |
foreach my $res (@hits) { |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
|
my $v = $res->{ $what } || next; |
|
|
next if ($used->{ $v }++); |
|
|
push @suggestions, $v; |
|
|
} |
|
78 |
|
|
79 |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
80 |
} |
} |
81 |
|
|
82 |
=back |
=back |