4 |
use warnings; |
use warnings; |
5 |
use base 'Catalyst::Controller'; |
use base 'Catalyst::Controller'; |
6 |
|
|
|
use Data::Dumper; |
|
|
|
|
7 |
use lib '/data/webpac2/lib'; |
use lib '/data/webpac2/lib'; |
8 |
use WebPAC::Search::Estraier 0.03; |
use WebPAC::Search::Estraier 0.03; |
9 |
|
|
31 |
sub default : Private { |
sub default : Private { |
32 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
33 |
|
|
34 |
$c->log->debug("default search got param: ".Dumper($c->req->params)); |
$c->log->dumper($c->req->params, 'params'); |
35 |
|
|
36 |
$c->stash->{template} = 'search.tt'; |
$c->stash->{template} = 'search.tt'; |
37 |
} |
} |
38 |
|
|
39 |
=item suggest |
=item suggest |
40 |
|
|
41 |
Returns results for URLs like C<search/suggest/FieldName> |
Returns results for REST URIs like: |
42 |
|
|
43 |
|
C<search/suggest?search=FieldName&show=TitleProper&FieldName=query%20string> |
44 |
|
|
45 |
|
It will use C<search> field to filter results (and using additional |
46 |
|
C<FieldName> as value of search) and return C<TitleProper> field |
47 |
|
for results. |
48 |
|
|
49 |
It will return field from URL (C<FieldName> in example>) and use |
If C<search> field has magic value <all>, it will search over all data, not |
50 |
same filed in paramerers (comming from form or URL) or if it doesn't |
just one specified field: |
51 |
exist field named C<all>. |
|
52 |
|
C<search/suggest?search=all&show=TitleProper&all=query%20string> |
53 |
|
|
54 |
=cut |
=cut |
55 |
|
|
56 |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
sub suggest : Local { |
57 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
58 |
|
|
59 |
my $what = $c->request->snippets->[0]; |
my $search = $c->req->params->{search}; |
60 |
|
my $show = $c->req->params->{show}; |
61 |
|
|
62 |
my $log = $c->log; |
my $log = $c->log; |
63 |
|
|
64 |
my $webpac = $c->comp('Model::WebPAC'); |
my $webpac = $c->comp('Model::WebPAC'); |
65 |
|
$c->log->dumper( $c->stash, 'stash' ); |
66 |
|
$webpac->setup_site( $c->stash->{site} ); |
67 |
|
|
68 |
my $q = $c->req->params->{$what} || |
my $q = $c->req->params->{ $search || 'all' } || $c->response->body("no results"); |
|
$c->req->params->{all} || $c->response->body("no results"); |
|
|
|
|
69 |
|
|
70 |
$log->info("search for '$q' in $what\n"); |
$log->info("search for '$q' in $search and display $show\n"); |
71 |
|
|
72 |
my $max = $c->config->{'hits_for_suggest'}; |
my $max = $c->config->{'hits_for_suggest'}; |
73 |
if (! $max) { |
if (! $max) { |
76 |
$max = 10; |
$max = 10; |
77 |
} |
} |
78 |
|
|
79 |
my @hits = $webpac->search( |
$c->forward('filter_database'); |
80 |
|
|
81 |
|
my $hits = $webpac->search( |
82 |
phrase => $q, |
phrase => $q, |
83 |
|
add_attr => $c->stash->{attr}, |
84 |
|
get_attr => [ $show ], |
85 |
max => $max, |
max => $max, |
|
get_attr => [ $what ], |
|
86 |
); |
); |
87 |
|
|
88 |
my $used; |
my $used; |
89 |
my @suggestions; |
my @suggestions; |
90 |
|
|
91 |
foreach my $res (@hits) { |
foreach my $res (@{$hits}) { |
92 |
my $v = $res->{ $what } || next; |
my $v = $res->{ $show } || next; |
93 |
next if ($used->{ $v }++); |
next if ($used->{ $v }++); |
94 |
push @suggestions, $v; |
push @suggestions, $v; |
95 |
} |
} |
127 |
|
|
128 |
# specify all Hyper Estraier operators which should stop this module |
# specify all Hyper Estraier operators which should stop this module |
129 |
# from splitting search query and joining it with default operator |
# from splitting search query and joining it with default operator |
130 |
my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/o; |
my $hest_op_regex = '(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)'; |
131 |
|
|
132 |
sub results_ajax : Path( 'results/ajax' ) { |
sub results_ajax : Path( 'results/ajax' ) { |
133 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
134 |
|
|
135 |
my $params = $c->req->params; |
my $params = $c->req->params; |
136 |
my $webpac = $c->comp('Model::WebPAC'); |
my $webpac = $c->comp('Model::WebPAC'); |
137 |
|
$webpac->setup_site( $c->stash->{site} ); |
138 |
my $log = $c->log; |
my $log = $c->log; |
139 |
|
|
140 |
$log->debug("results got params: " . Dumper( $params ) ); |
$log->dumper($params, 'params'); |
141 |
|
|
142 |
if (! $params->{_page} || $params->{_page} < 1) { |
if (! $params->{_page} || $params->{_page} < 1) { |
143 |
$params->{_page} = 1; |
$params->{_page} = 1; |
144 |
$log->warn("fixed _page parametar to 1"); |
$log->warn("fixed _page parametar to 1"); |
145 |
} |
} |
146 |
|
|
|
my @attr; |
|
147 |
my @words; |
my @words; |
148 |
# default operator to join fields/words |
# default operator to join fields/words |
149 |
my $operator = 'AND'; |
my $operator = 'AND'; |
155 |
my $v = $params->{$f} || next; |
my $v = $params->{$f} || next; |
156 |
|
|
157 |
if (my $op = $params->{ '_' . $f}) { |
if (my $op = $params->{ '_' . $f}) { |
158 |
if ($v =~ $hest_op_regex) { |
if ($v =~ /$hest_op_regex/) { |
159 |
# don't split words if there is Hyper Estraier |
# don't split words if there is Hyper Estraier |
160 |
# operator in them |
# operator in them |
161 |
push @words, $v; |
push @words, $v; |
169 |
next if ($f eq 'all'); # don't add_attr for magic field all |
next if ($f eq 'all'); # don't add_attr for magic field all |
170 |
|
|
171 |
if ($v !~ /\s/) { |
if ($v !~ /\s/) { |
172 |
push @attr, "$f ISTRINC $v"; |
push @{ $c->stash->{attr} }, "$f ISTRINC $v"; |
173 |
} else { |
} else { |
174 |
map { |
map { |
175 |
push @attr, "$f ISTRINC $_"; |
push @{ $c->stash->{attr} }, "$f ISTRINC $_"; |
176 |
} grep { ! $hest_op_regex } split(/\s+/, $v); |
} grep { ! /$hest_op_regex/ } split(/\s+/, $v); |
177 |
} |
} |
178 |
} |
} |
179 |
|
|
180 |
if ($params->{_database}) { |
$c->forward('filter_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 @attr, '@uri STRRX /' . $type . '/' . $attr . '/'; |
|
|
} |
|
181 |
|
|
182 |
my $q = join(" $operator ", @words); |
my $q = join(" $operator ", @words); |
183 |
|
|
193 |
my $res = $webpac->search( |
my $res = $webpac->search( |
194 |
phrase => $q, |
phrase => $q, |
195 |
template => $template, |
template => $template, |
196 |
add_attr => \@attr, |
add_attr => $c->{stash}->{attr}, |
197 |
get_attr => [ '@uri' ], |
get_attr => [ '@uri' ], |
198 |
max => $hits_on_page, |
max => $hits_on_page, |
199 |
page => $params->{'_page'}, |
page => $params->{'_page'}, |
203 |
}; |
}; |
204 |
|
|
205 |
$c->stash->{phrase} = $q; |
$c->stash->{phrase} = $q; |
|
$c->stash->{attr} = \@attr; |
|
206 |
$c->stash->{page} = $params->{'_page'}; |
$c->stash->{page} = $params->{'_page'}; |
207 |
$c->stash->{hits_on_page} = $hits_on_page; |
$c->stash->{hits_on_page} = $hits_on_page; |
208 |
|
|
210 |
|
|
211 |
} |
} |
212 |
|
|
213 |
|
=item filter_database |
214 |
|
|
215 |
|
Takes C<< $c->req->params >> and adds Hyper Estraier |
216 |
|
filters for checked databases to C<< $c->stash->{attr} >>. |
217 |
|
|
218 |
|
=cut |
219 |
|
|
220 |
|
sub filter_database : Private { |
221 |
|
my ( $self, $c ) = @_; |
222 |
|
|
223 |
|
my $params = $c->req->params; |
224 |
|
|
225 |
|
if ($params->{_database}) { |
226 |
|
my $type = $c->config->{hyperstraier}->{type} || 'search'; |
227 |
|
my $attr; |
228 |
|
if (ref($params->{_database}) eq 'ARRAY') { |
229 |
|
# FIXME do we need to add $ at end? |
230 |
|
$attr .= '(' . join("|",@{$params->{_database}}) . ')'; |
231 |
|
} else { |
232 |
|
$attr .= $params->{_database}; |
233 |
|
} |
234 |
|
push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/'; |
235 |
|
$c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) ); |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
} |
240 |
|
|
241 |
=item record |
=item record |
242 |
|
|
243 |
forwarded to C</editor/record> |
forwarded to C</editor/record> |