1 |
use strict; |
2 |
use warnings; |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
Webpacus::Action::Search |
7 |
|
8 |
=cut |
9 |
|
10 |
package Webpacus::Action::Search; |
11 |
use base qw/Webpacus::Action Jifty::Action/; |
12 |
|
13 |
use Data::Dump qw/dump/; |
14 |
use Webpacus::Webpac; |
15 |
use KinoSearch::Simple; |
16 |
|
17 |
use Jifty::Param::Schema; |
18 |
use Jifty::Action schema { |
19 |
|
20 |
param field => |
21 |
label is _("Field name"), |
22 |
available are defer { |
23 |
my $coll = Webpacus::Model::SearchCollection->new; |
24 |
$coll->unlimit; |
25 |
[ '', { |
26 |
display_from => 'name', |
27 |
value_from => 'id', |
28 |
collection => $coll, |
29 |
}]; |
30 |
}, |
31 |
render as 'Select'; |
32 |
|
33 |
param query => |
34 |
label is _("Search for"), |
35 |
is mandatory; |
36 |
|
37 |
|
38 |
}; |
39 |
|
40 |
sub sticky_on_success { 1 } |
41 |
sub sticky_on_failure { 1 } |
42 |
|
43 |
=head2 take_action |
44 |
|
45 |
=cut |
46 |
|
47 |
sub take_action { |
48 |
my $self = shift; |
49 |
|
50 |
my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) ); |
51 |
my $query = $self->argument_value( 'query' ); |
52 |
|
53 |
warn "## search = ",dump( $search ); |
54 |
|
55 |
my $index_path = Webpacus::Webpac->index_path; |
56 |
|
57 |
warn "## index_path = $index_path"; |
58 |
|
59 |
my $index = KinoSearch::Simple->new( |
60 |
path => $index_path, |
61 |
language => 'en', |
62 |
); |
63 |
|
64 |
my $total_hits = $index->search( |
65 |
query => $query, |
66 |
offset => 0, |
67 |
num_wanted => 100, |
68 |
); |
69 |
|
70 |
my $message = |
71 |
_('Found') . " $total_hits " . |
72 |
_('results for') . " '$query'"; |
73 |
|
74 |
$message .= " " . _('on field') . ' ' . $search->name if $search->name; |
75 |
|
76 |
$self->result->content( |
77 |
results => Webpacus::Search::Results->new({ |
78 |
count => $total_hits, |
79 |
index => $index, |
80 |
}), |
81 |
); |
82 |
|
83 |
$self->result->message( $message ); |
84 |
|
85 |
return 1; |
86 |
} |
87 |
|
88 |
package Webpacus::Search::Results; |
89 |
|
90 |
use strict; |
91 |
use warnings; |
92 |
use base qw( Class::Accessor ); |
93 |
__PACKAGE__->mk_accessors( qw( |
94 |
count |
95 |
index |
96 |
) ); |
97 |
|
98 |
use Data::Dump qw/dump/; |
99 |
|
100 |
sub next { |
101 |
my $self = shift; |
102 |
my $row = $self->index->fetch_hit_hashref; |
103 |
|
104 |
warn "## next row = ", dump( $row ); |
105 |
|
106 |
return $row; |
107 |
} |
108 |
|
109 |
1; |
110 |
|