3 |
use strict; |
use strict; |
4 |
use warnings; |
use warnings; |
5 |
use base qw( Class::Accessor ); |
use base qw( Class::Accessor ); |
6 |
Grep::Search->mk_accessors( qw( analyzer store writer create index_path ) ); |
Grep::Search->mk_accessors( qw( create index_path hits ) ); |
7 |
|
|
8 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
9 |
use Lucene; |
use KinoSearch::InvIndexer; |
10 |
|
use KinoSearch::Searcher; |
11 |
use Jifty::Util; |
use Jifty::Util; |
12 |
|
|
13 |
my $debug = 0; |
my $debug = 1; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
17 |
Grep::Search - full text search |
Grep::Search - full text search using L<KinoSearch> |
18 |
|
|
19 |
=head1 METHODS |
=head1 METHODS |
20 |
|
|
22 |
|
|
23 |
my $search = Grep::Search->new(); |
my $search = Grep::Search->new(); |
24 |
|
|
25 |
|
my $search = Grep::Search->new( create => 1 ); |
26 |
|
|
27 |
=cut |
=cut |
28 |
|
|
29 |
sub log { Jifty->web->log } |
sub log { Jifty->web->log } |
32 |
my $class = shift; |
my $class = shift; |
33 |
my $self = $class->SUPER::new(@_); |
my $self = $class->SUPER::new(@_); |
34 |
|
|
35 |
my $index_path = Jifty::Util->app_root . '/var/lucene'; |
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
36 |
|
|
37 |
$self->index_path( $index_path ); |
$self->index_path( $index_path ); |
38 |
|
|
39 |
if (! -e "$index_path/segments") { |
return $self; |
40 |
$self->create( 1 ); |
} |
41 |
$self->log->debug("Creating new index $index_path"); |
|
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 { |
} else { |
60 |
$self->create( 0 ); |
if ( ! -e "$index_path" || $self->create ) { |
61 |
$self->log->debug("Opening index: $index_path"); |
$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->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() ); |
$self->invindexer( $invindexer ); |
|
$self->log->debug($self->analyzer . " created"); |
|
|
|
|
|
$self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) ); |
|
|
$self->log->debug($self->store, " created"); |
|
71 |
|
|
|
return $self; |
|
72 |
} |
} |
73 |
|
|
74 |
=head2 add |
=head2 add |
87 |
|
|
88 |
my $pk = { $i->primary_keys }; |
my $pk = { $i->primary_keys }; |
89 |
|
|
90 |
my $doc = new Lucene::Document; |
my $doc; |
91 |
|
|
92 |
my @columns = map { $_->name } $i->columns; |
my @columns = map { $_->name } $i->columns; |
93 |
|
|
103 |
my $col = $c . '_' . $f_c; |
my $col = $c . '_' . $f_c; |
104 |
if ( $f_v ) { |
if ( $f_v ) { |
105 |
warn " # $col = $f_v\n" if ($debug); |
warn " # $col = $f_v\n" if ($debug); |
106 |
$doc->add(Lucene::Document::Field->Text( $col, $f_v )); |
$doc->{ $col } = $f_v; |
107 |
} else { |
} else { |
108 |
warn " . $col is NULL\n" if ($debug); |
warn " . $col is NULL\n" if ($debug); |
109 |
} |
} |
112 |
|
|
113 |
if ($v->isa('Jifty::DateTime')) { |
if ($v->isa('Jifty::DateTime')) { |
114 |
warn " d $c = $v\n" if ($debug); |
warn " d $c = $v\n" if ($debug); |
115 |
$doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); |
$doc->{$c} = $v; |
116 |
} else { |
} else { |
117 |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
118 |
} |
} |
121 |
|
|
122 |
next if (! defined($v) || $v eq ''); |
next if (! defined($v) || $v eq ''); |
123 |
|
|
124 |
$v =~ s/<[^>]+>/ /gs; |
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} ) ) { |
if ( defined( $pk->{$c} ) ) { |
131 |
$doc->add(Lucene::Document::Field->Keyword( $c, $v )); |
$doc->{ $c } = $v; |
132 |
warn " * $c = $v\n" if ($debug); |
warn " * $c = $v\n" if ($debug); |
133 |
} else { |
} else { |
134 |
$doc->add(Lucene::Document::Field->Text( $c, $v )); |
$doc->{ $c } = $v; |
135 |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
136 |
} |
} |
137 |
} |
} |
138 |
|
|
139 |
# add _owner_id to speed up filtering of search results |
# add _owner_id to speed up filtering of search results |
140 |
$uid ||= Jifty->web->current_user->id; |
$uid ||= Jifty->web->current_user->id; |
141 |
$doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid )); |
$doc->{ '_owner_id' } = $uid; |
142 |
|
|
143 |
if (! defined( $self->writer )) { |
$self->invindexer->add_doc( $doc ); |
|
$self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) ); |
|
|
$self->log->debug($self->writer, " created"); |
|
|
} |
|
|
|
|
|
$self->writer->addDocument($doc); |
|
144 |
|
|
145 |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
146 |
|
|
147 |
|
return 1; |
148 |
} |
} |
149 |
|
|
150 |
=head2 collection |
=head2 collection |
151 |
|
|
152 |
|
Return C<Grep::Model::ItemCollection> which is result of C<search query> |
153 |
|
|
154 |
my $ItemCollection = $search->collection( 'search query' ); |
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 |
=cut |
163 |
|
|
164 |
sub collection { |
sub collection { |
166 |
|
|
167 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
168 |
|
|
169 |
return if ( $self->create ); |
my $searcher = KinoSearch::Searcher->new( |
170 |
|
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
|
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
|
171 |
$self->log->debug("$searcher created"); |
$self->log->debug("$searcher created"); |
|
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
|
|
$self->log->debug("$parser created"); |
|
172 |
|
|
173 |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
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 |
my $query = $parser->parse( $full_q ); |
$self->log->debug("searching for '$q' using $full_q"); |
179 |
|
|
180 |
$self->log->debug("searching for '$q' using ", $query->toString); |
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 |
my $hits = $searcher->search($query); |
$self->hits( $hits->total_hits ); |
|
my $num_hits = $hits->length(); |
|
193 |
|
|
194 |
$self->log->debug("found $num_hits results"); |
$self->log->debug("found ", $self->hits, " results"); |
195 |
|
|
196 |
my $collection = Grep::Model::ItemCollection->new(); |
my $collection = Grep::Model::ItemCollection->new(); |
197 |
|
|
198 |
my @results; |
my @results; |
199 |
|
|
200 |
for ( my $i = 0; $i < $num_hits; $i++ ) { |
my $i = 0; |
201 |
|
while ( my $hit = $hits->fetch_hit ) { |
202 |
|
|
203 |
my $doc = $hits->doc( $i ); |
my $score = $hit->{score}; |
204 |
|
my $title = $hit->{title}; |
205 |
|
my $id = $hit->{id}; |
206 |
|
|
207 |
my $score = $hits->score($i); |
$self->log->debug("result $i [$id] $title $score"); |
|
my $title = $doc->get("title"); |
|
|
my $id = $doc->get("id"); |
|
|
|
|
|
$self->log->debug("result $i $score $title"); |
|
208 |
|
|
209 |
my $item = Grep::Model::Item->new(); |
my $item = Grep::Model::Item->new(); |
210 |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
217 |
|
|
218 |
} |
} |
219 |
|
|
220 |
undef $hits; |
$self->log->debug("finished search"); |
|
undef $query; |
|
|
undef $parser; |
|
|
undef $searcher; |
|
|
|
|
|
$self->log->debug("finished Lucene search"); |
|
221 |
|
|
222 |
return $collection; |
return $collection; |
223 |
} |
} |
230 |
|
|
231 |
sub finish { |
sub finish { |
232 |
my $self = shift; |
my $self = shift; |
233 |
if ($self->writer) { |
if ($self->invindexer) { |
234 |
$self->log->debug("closing index"); |
$self->log->debug("closing index"); |
235 |
$self->writer->close; |
$self->invindexer->finish; |
236 |
} |
} |
237 |
|
|
238 |
$self->log->debug("finish"); |
$self->log->debug("finish"); |
239 |
|
|
240 |
undef $self; |
undef $self; |
241 |
|
|
242 |
return; |
return 1; |
|
} |
|
|
|
|
|
=for TODO |
|
|
|
|
|
sub _signal { |
|
|
my $s = shift; |
|
|
warn "catched SIG $s\n"; |
|
|
finish(); |
|
|
exit(0); |
|
243 |
} |
} |
244 |
|
|
|
$SIG{'__DIE__'} = \&_signal; |
|
|
$SIG{'INT'} = \&_signal; |
|
|
$SIG{'QUIT'} = \&_signal; |
|
|
|
|
|
=cut |
|
|
|
|
245 |
=head2 snippet |
=head2 snippet |
246 |
|
|
247 |
my $short = $self->snippet( 50, $text ); |
my $short = $self->snippet( 50, $text ); |
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; |
1; |