1 |
package Webpacus::Controller::AutoComplete; |
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::AutoComplete - 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} = 'autocomplete.tt'; |
33 |
} |
34 |
|
35 |
=item suggest |
36 |
|
37 |
=cut |
38 |
|
39 |
sub suggest : Regex('^autocomplete/suggest/*([^/]*)') { |
40 |
my ( $self, $c ) = @_; |
41 |
|
42 |
my $what = $c->request->snippets->[0]; |
43 |
|
44 |
my $log = $c->log; |
45 |
|
46 |
my $est = new WebPAC::Search::Estraier( |
47 |
url => 'http://localhost:1978/node/webpac2', |
48 |
user => 'admin', |
49 |
passwd => 'admin', |
50 |
encoding => 'UTF-8', |
51 |
log => $c->log, |
52 |
); |
53 |
|
54 |
|
55 |
my $q = $c->req->params->{$what}; |
56 |
my @suggestions; |
57 |
|
58 |
$log->info("search for '$q' in $what\n"); |
59 |
my @hits = $est->search( query => $q, max => 10, attr => [ $what ] ); |
60 |
|
61 |
my $used; |
62 |
|
63 |
foreach my $res (@hits) { |
64 |
my $v = $res->{ $what } || next; |
65 |
next if ($used->{ $v }++); |
66 |
push @suggestions, $v; |
67 |
} |
68 |
|
69 |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
70 |
} |
71 |
|
72 |
=back |
73 |
|
74 |
|
75 |
=head1 AUTHOR |
76 |
|
77 |
Dobrica Pavlinusic,,, |
78 |
|
79 |
=head1 LICENSE |
80 |
|
81 |
This library is free software, you can redistribute it and/or modify |
82 |
it under the same terms as Perl itself. |
83 |
|
84 |
=cut |
85 |
|
86 |
1; |