7 |
use Data::Dumper; |
use Data::Dumper; |
8 |
|
|
9 |
use lib '/data/webpac2/lib'; |
use lib '/data/webpac2/lib'; |
10 |
use WebPAC::Search::Estraier; |
use WebPAC::Search::Estraier 0.03; |
11 |
|
|
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
15 |
Webpacus::Controller::Search - Catalyst Controller |
Webpacus::Controller::Search - Search WebPAC data |
16 |
|
|
17 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
18 |
|
|
19 |
See L<Webpacus> |
See L<WebPAC>, L<Webpacus> |
20 |
|
|
21 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
22 |
|
|
23 |
Catalyst Controller for autocompleting search fields. |
Catalyst Controller for search fields Hyper Estraier |
24 |
|
|
25 |
=head1 METHODS |
=head1 METHODS |
26 |
|
|
39 |
|
|
40 |
Returns results for URLs like C<search/suggest/FieldName> |
Returns results for URLs like C<search/suggest/FieldName> |
41 |
|
|
42 |
|
It will return field from URL (C<FieldName> in example>) and use |
43 |
|
same filed in paramerers (comming from form or URL) or if it doesn't |
44 |
|
exist field named C<all>. |
45 |
|
|
46 |
=cut |
=cut |
47 |
|
|
48 |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
52 |
|
|
53 |
my $log = $c->log; |
my $log = $c->log; |
54 |
|
|
55 |
my $est_conf = $c->config->{hyperestraier}; |
my $webpac = $c->comp('Model::WebPAC'); |
|
$est_conf->{log} = $c->log; |
|
|
|
|
|
my $est = new WebPAC::Search::Estraier( %{ $est_conf } ); |
|
56 |
|
|
57 |
my $q = $c->req->params->{$what} || |
my $q = $c->req->params->{$what} || |
58 |
$c->req->params->{all} || $c->res->output("no results"); |
$c->req->params->{all} || $c->res->output("no results"); |
60 |
|
|
61 |
$log->info("search for '$q' in $what\n"); |
$log->info("search for '$q' in $what\n"); |
62 |
|
|
63 |
my $max = $est_conf->{'hits_for_suggest'}; |
my $max = $c->config->{'hits_for_suggest'}; |
64 |
if (! $max) { |
if (! $max) { |
65 |
$log->info("hits_for_suggest isn't defined, defaulting to 10"); |
$log->info("hits_for_suggest isn't defined, defaulting to 10"); |
66 |
$c->config->{'hits_for_suggest'} = 10; |
$c->config->{'hits_for_suggest'} = 10; |
67 |
$max = 10; |
$max = 10; |
68 |
} |
} |
69 |
|
|
70 |
my @hits = $est->search( phrase => $q, max => $max, get_attr => [ $what ] ); |
my @hits = $webpac->search( |
71 |
|
phrase => $q, |
72 |
|
max => $max, |
73 |
|
get_attr => [ $what ], |
74 |
|
); |
75 |
|
|
76 |
my $used; |
my $used; |
77 |
my @suggestions; |
my @suggestions; |
84 |
|
|
85 |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
86 |
|
|
87 |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
$c->response->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
88 |
} |
} |
89 |
|
|
90 |
|
|
94 |
|
|
95 |
=item results |
=item results |
96 |
|
|
97 |
Mothod which uses C<Model::WebPAC> and returns results for search query |
Method which uses C<Model::WebPAC> and returns results for search query |
98 |
|
|
99 |
=cut |
=cut |
100 |
|
|
107 |
|
|
108 |
$log->debug("results got params: " . Dumper( $params ) ); |
$log->debug("results got params: " . Dumper( $params ) ); |
109 |
|
|
110 |
|
if (! $params->{_page} || $params->{_page} < 1) { |
111 |
|
$params->{_page} = 1; |
112 |
|
$log->warn("fixed _page parametar to 1"); |
113 |
|
} |
114 |
|
|
115 |
my @attr; |
my @attr; |
116 |
my @words; |
my @words; |
117 |
# default operator to join fields/words |
# default operator to join fields/words |
148 |
|
|
149 |
my $q = join(" $operator ", @words); |
my $q = join(" $operator ", @words); |
150 |
|
|
151 |
my $template = $params->{'_template'}; |
my $template = $params->{'_template'} || $c->config->{webpac}->{template}; |
152 |
$log->debug("using template $template"); |
|
153 |
|
$log->die("can't find _template or default from configuration!") unless ($template); |
154 |
|
|
155 |
|
my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10; |
156 |
|
|
157 |
|
$log->debug("using template $template to produce $hits_on_page results"); |
158 |
|
|
159 |
$c->stash->{html_results} = sub { |
$c->stash->{html_results} = sub { |
160 |
my $res = $webpac->search( |
my $res = $webpac->search( |
161 |
phrase => $q, |
phrase => $q, |
162 |
template => $template, |
template => $template, |
163 |
add_attr => \@attr |
add_attr => \@attr, |
164 |
|
get_attr => [ '@uri' ], |
165 |
|
max => $hits_on_page, |
166 |
|
page => $params->{'_page'}, |
167 |
); |
); |
168 |
# $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); |
# $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); |
169 |
return $res; |
return $res; |
170 |
}; |
}; |
171 |
|
|
172 |
$c->stash->{phrase} = $q; |
$c->stash->{phrase} = $q; |
173 |
$c->stash->{attr} = \@attr; |
$c->stash->{attr} = \@attr; |
174 |
|
$c->stash->{page} = $params->{'_page'}; |
175 |
|
$c->stash->{hits_on_page} = $hits_on_page; |
176 |
|
|
177 |
$c->stash->{template} = 'results.tt'; |
$c->stash->{template} = 'results.tt'; |
178 |
} |
} |
179 |
|
|
180 |
=back |
=item record |
181 |
|
|
182 |
|
forwarded to C</editor/record> |
183 |
|
|
184 |
|
=cut |
185 |
|
|
186 |
|
sub record : Local { |
187 |
|
my ( $self, $c ) = @_; |
188 |
|
|
189 |
|
$c->forward( '/editor/record' ); |
190 |
|
} |
191 |
|
|
192 |
|
=back |
193 |
|
|
194 |
=head1 AUTHOR |
=head1 AUTHOR |
195 |
|
|