1 |
package Grep::Search; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use base qw( Class::Accessor ); |
6 |
Grep::Search->mk_accessors( qw( create index_path hits ) ); |
7 |
|
8 |
use Data::Dump qw/dump/; |
9 |
use KinoSearch::InvIndexer; |
10 |
use KinoSearch::Searcher; |
11 |
use Jifty::Util; |
12 |
|
13 |
my $debug = 1; |
14 |
|
15 |
=head1 NAME |
16 |
|
17 |
Grep::Search - full text search using L<KinoSearch> |
18 |
|
19 |
=head1 METHODS |
20 |
|
21 |
=head2 new |
22 |
|
23 |
my $search = Grep::Search->new(); |
24 |
|
25 |
my $search = Grep::Search->new( create => 1 ); |
26 |
|
27 |
=cut |
28 |
|
29 |
sub log { Jifty->web->log } |
30 |
|
31 |
sub new { |
32 |
my $class = shift; |
33 |
my $self = $class->SUPER::new(@_); |
34 |
|
35 |
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
36 |
|
37 |
$self->index_path( $index_path ); |
38 |
|
39 |
return $self; |
40 |
} |
41 |
|
42 |
=head2 invindexer |
43 |
|
44 |
Accessor to call any method defined on L<KinoSearch::InvIndexer> |
45 |
|
46 |
$search->invindexer->delete_by_term( 'id', 42 ); |
47 |
|
48 |
=cut |
49 |
|
50 |
my $indexes; |
51 |
|
52 |
sub invindexer { |
53 |
my $self = shift; |
54 |
my $invindexer; |
55 |
my $index_path = $self->index_path or die "no index_path?"; |
56 |
|
57 |
if ( $invindexer = $indexes->{$index_path} ) { |
58 |
$self->log->debug("Using cached index $index_path"); |
59 |
} else { |
60 |
if ( ! -e "$index_path" || $self->create ) { |
61 |
$self->log->debug("Creating new index $index_path"); |
62 |
my $invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ); |
63 |
} else { |
64 |
$self->log->debug("Opening index: $index_path"); |
65 |
$invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ); |
66 |
} |
67 |
$indexes->{$index_path} = $invindexer; |
68 |
} |
69 |
|
70 |
$self->invindexer( $invindexer ); |
71 |
|
72 |
} |
73 |
|
74 |
=head2 add |
75 |
|
76 |
$search->add( $record, $owner_id ); |
77 |
|
78 |
=cut |
79 |
|
80 |
sub add { |
81 |
my $self = shift; |
82 |
|
83 |
my $i = shift or die "no record to add"; |
84 |
my $uid = shift; |
85 |
|
86 |
die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record')); |
87 |
|
88 |
my $pk = { $i->primary_keys }; |
89 |
|
90 |
my $doc; |
91 |
|
92 |
my @columns = map { $_->name } $i->columns; |
93 |
|
94 |
foreach my $c ( @columns ) { |
95 |
|
96 |
my $v = $i->$c; |
97 |
|
98 |
if ( ref($v) ne '' ) { |
99 |
|
100 |
foreach my $f_c ( qw/id name title/ ) { |
101 |
if ( $i->$c->can( $f_c ) ) { |
102 |
my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c }; |
103 |
my $col = $c . '_' . $f_c; |
104 |
if ( $f_v ) { |
105 |
warn " # $col = $f_v\n" if ($debug); |
106 |
$doc->{ $col } = $f_v; |
107 |
} else { |
108 |
warn " . $col is NULL\n" if ($debug); |
109 |
} |
110 |
} |
111 |
} |
112 |
|
113 |
if ($v->isa('Jifty::DateTime')) { |
114 |
warn " d $c = $v\n" if ($debug); |
115 |
$doc->{$c} = $v; |
116 |
} else { |
117 |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
118 |
} |
119 |
next; |
120 |
} |
121 |
|
122 |
next if (! defined($v) || $v eq ''); |
123 |
|
124 |
eval { $v =~ s/<[^>]+>/ /gs; }; |
125 |
if ($@) { |
126 |
Jifty->log->error("can't strip html from $c in item ", $i->id); |
127 |
next; |
128 |
} |
129 |
|
130 |
if ( defined( $pk->{$c} ) ) { |
131 |
$doc->{ $c } = $v; |
132 |
warn " * $c = $v\n" if ($debug); |
133 |
} else { |
134 |
$doc->{ $c } = $v; |
135 |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
136 |
} |
137 |
} |
138 |
|
139 |
# add _owner_id to speed up filtering of search results |
140 |
$uid ||= Jifty->web->current_user->id; |
141 |
$doc->{ '_owner_id' } = $uid; |
142 |
|
143 |
$self->invindexer->add_doc( $doc ); |
144 |
|
145 |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
146 |
|
147 |
return 1; |
148 |
} |
149 |
|
150 |
=head2 collection |
151 |
|
152 |
Return C<Grep::Model::ItemCollection> which is result of C<search query> |
153 |
|
154 |
my $ItemCollection = $search->collection( 'search query' ); |
155 |
|
156 |
=head2 hits |
157 |
|
158 |
Return number of results from last C<collection> call |
159 |
|
160 |
my $num_results = $search->hits; |
161 |
|
162 |
=cut |
163 |
|
164 |
sub collection { |
165 |
my $self = shift; |
166 |
|
167 |
my $q = shift or die "no q?"; |
168 |
|
169 |
my $searcher = KinoSearch::Searcher->new( |
170 |
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
171 |
$self->log->debug("$searcher created"); |
172 |
|
173 |
my $full_q = "($q)"; |
174 |
|
175 |
my $uid = Jifty->web->current_user->id; |
176 |
$full_q .= ' AND _owner_id:' . $uid if (defined $uid); |
177 |
|
178 |
$self->log->debug("searching for '$q' using $full_q"); |
179 |
|
180 |
my $query_parser = KinoSearch::QueryParser->new( |
181 |
schema => Grep::Search::Schema->new, |
182 |
fields => [ qw/ title link content summary category author / ], |
183 |
); |
184 |
$query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax |
185 |
my $query = $query_parser->parse( $full_q ); |
186 |
my $hits = $searcher->search( |
187 |
query => $query, |
188 |
# offset => $offset, |
189 |
# num_wanted => $hits_per_page, |
190 |
); |
191 |
|
192 |
$self->hits( $hits->total_hits ); |
193 |
|
194 |
$self->log->debug("found ", $self->hits, " results"); |
195 |
|
196 |
my $collection = Grep::Model::ItemCollection->new(); |
197 |
|
198 |
my @results; |
199 |
|
200 |
my $i = 0; |
201 |
while ( my $hit = $hits->fetch_hit ) { |
202 |
|
203 |
my $score = $hit->{score}; |
204 |
my $title = $hit->{title}; |
205 |
my $id = $hit->{id}; |
206 |
|
207 |
$self->log->debug("result $i [$id] $title $score"); |
208 |
|
209 |
my $item = Grep::Model::Item->new(); |
210 |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
211 |
|
212 |
if ( $ok ) { |
213 |
$collection->add_record( $item ); |
214 |
} else { |
215 |
warn "can't load item $id\n"; |
216 |
} |
217 |
|
218 |
} |
219 |
|
220 |
$self->log->debug("finished search"); |
221 |
|
222 |
return $collection; |
223 |
} |
224 |
|
225 |
=head2 finish |
226 |
|
227 |
$search->finish |
228 |
|
229 |
=cut |
230 |
|
231 |
sub finish { |
232 |
my $self = shift; |
233 |
if ($self->invindexer) { |
234 |
$self->log->debug("closing index"); |
235 |
$self->invindexer->finish; |
236 |
} |
237 |
|
238 |
$self->log->debug("finish"); |
239 |
|
240 |
undef $self; |
241 |
|
242 |
return 1; |
243 |
} |
244 |
|
245 |
=head2 snippet |
246 |
|
247 |
my $short = $self->snippet( 50, $text ); |
248 |
|
249 |
=cut |
250 |
|
251 |
sub snippet { |
252 |
my $self = shift; |
253 |
|
254 |
my $len = shift or die "no len?"; |
255 |
my $m = join(" ", @_); |
256 |
|
257 |
$m =~ s/\s+/ /gs; |
258 |
|
259 |
if (length($m) > $len) { |
260 |
return substr($m,0,$len) . '...'; |
261 |
} else { |
262 |
return $m; |
263 |
} |
264 |
} |
265 |
|
266 |
package Grep::Search::KeywordField; |
267 |
use base qw( KinoSearch::Schema::FieldSpec ); |
268 |
sub analyzed { 0 } |
269 |
#sub indexed { 1 } |
270 |
#sub stored { 1 } |
271 |
sub vectorized { 0 } |
272 |
|
273 |
package Grep::Search::Schema; |
274 |
|
275 |
=head1 NAME |
276 |
|
277 |
Grep::Search::Schema - schema definition for full-text search |
278 |
|
279 |
=cut |
280 |
|
281 |
use base 'KinoSearch::Schema'; |
282 |
use KinoSearch::Analysis::PolyAnalyzer; |
283 |
|
284 |
our %fields = ( |
285 |
id => 'Grep::Search::KeywordField', |
286 |
|
287 |
in_feed_id => 'Grep::Search::KeywordField', |
288 |
in_feed_url => 'Grep::Search::KeywordField', |
289 |
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
290 |
in_feed_owner => 'Grep::Search::KeywordField', |
291 |
in_feed_created_on => 'Grep::Search::KeywordField', |
292 |
|
293 |
title => 'KinoSearch::Schema::FieldSpec', |
294 |
link => 'Grep::Search::KeywordField', |
295 |
content => 'KinoSearch::Schema::FieldSpec', |
296 |
summary => 'KinoSearch::Schema::FieldSpec', |
297 |
category => 'KinoSearch::Schema::FieldSpec', |
298 |
author => 'KinoSearch::Schema::FieldSpec', |
299 |
created_on => 'Grep::Search::KeywordField', |
300 |
last_update => 'Grep::Search::KeywordField', |
301 |
|
302 |
_owner_id => 'Grep::Search::KeywordField', |
303 |
); |
304 |
|
305 |
sub analyzer { |
306 |
return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); |
307 |
} |
308 |
|
309 |
1; |