2 |
|
|
3 |
use strict; |
use strict; |
4 |
use warnings; |
use warnings; |
5 |
|
use base qw( Class::Accessor ); |
6 |
|
Grep::Search->mk_accessors( qw( invindexer 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 $index_path = Jifty::Util->app_root . '/var/lucene'; |
my $debug = 0; |
14 |
|
|
15 |
my ( $analyzer, $store, $writer ); |
=head1 NAME |
16 |
|
|
17 |
my $debug = 1; |
Grep::Search - full text search using L<KinoSearch> |
|
my $create; |
|
18 |
|
|
19 |
sub create { |
=head1 METHODS |
20 |
|
|
21 |
if (defined( $create )) { |
=head2 new |
|
Jifty->log->debug("using previous create $create"); |
|
|
return $create; |
|
|
} |
|
22 |
|
|
23 |
if (! -e "$index_path/segments") { |
my $search = Grep::Search->new(); |
|
$create = 1; |
|
|
Jifty->log->debug("create index $index_path"); |
|
|
} else { |
|
|
$create = 0; |
|
|
Jifty->log->debug("open index: $index_path"); |
|
|
} |
|
|
return $create; |
|
|
} |
|
24 |
|
|
25 |
sub analyzer { |
my $search = Grep::Search->new( create => 1 ); |
|
my $self = shift; |
|
|
$analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); |
|
|
return $analyzer; |
|
|
} |
|
26 |
|
|
27 |
sub store { |
=head2 invindexer |
|
my $self = shift; |
|
28 |
|
|
29 |
$store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ); |
Accessor to call any method defined on L<KinoSearch::InvIndexer> |
|
return $store; |
|
|
} |
|
30 |
|
|
31 |
sub writer { |
$search->invindexer->delete_by_term( 'id', 42 ); |
32 |
my $self = shift; |
|
33 |
$writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ); |
=cut |
34 |
return $writer; |
|
35 |
|
sub log { Jifty->web->log } |
36 |
|
|
37 |
|
sub new { |
38 |
|
my $class = shift; |
39 |
|
my $self = $class->SUPER::new(@_); |
40 |
|
|
41 |
|
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
42 |
|
|
43 |
|
$self->index_path( $index_path ); |
44 |
|
|
45 |
|
if ( ! -e "$index_path" || $self->create ) { |
46 |
|
$self->log->debug("Creating new index $index_path"); |
47 |
|
$self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) ); |
48 |
|
} else { |
49 |
|
$self->log->debug("Opening index: $index_path"); |
50 |
|
$self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) ); |
51 |
|
} |
52 |
|
|
53 |
|
return $self; |
54 |
} |
} |
55 |
|
|
56 |
=head2 add |
=head2 add |
57 |
|
|
58 |
Grep::Search->add( $record, $owner_id ); |
$search->add( $record, $owner_id ); |
59 |
|
|
60 |
=cut |
=cut |
61 |
|
|
69 |
|
|
70 |
my $pk = { $i->primary_keys }; |
my $pk = { $i->primary_keys }; |
71 |
|
|
72 |
my $doc = new Lucene::Document; |
my $doc; |
73 |
|
|
74 |
my @columns = map { $_->name } $i->columns; |
my @columns = map { $_->name } $i->columns; |
75 |
|
|
85 |
my $col = $c . '_' . $f_c; |
my $col = $c . '_' . $f_c; |
86 |
if ( $f_v ) { |
if ( $f_v ) { |
87 |
warn " # $col = $f_v\n" if ($debug); |
warn " # $col = $f_v\n" if ($debug); |
88 |
$doc->add(Lucene::Document::Field->Text( $col, $f_v )); |
$doc->{ $col } = $f_v; |
89 |
} else { |
} else { |
90 |
warn " . $col is NULL\n" if ($debug); |
warn " . $col is NULL\n" if ($debug); |
91 |
} |
} |
94 |
|
|
95 |
if ($v->isa('Jifty::DateTime')) { |
if ($v->isa('Jifty::DateTime')) { |
96 |
warn " d $c = $v\n" if ($debug); |
warn " d $c = $v\n" if ($debug); |
97 |
$doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); |
$doc->{$c} = $v; |
98 |
} else { |
} else { |
99 |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
100 |
} |
} |
106 |
$v =~ s/<[^>]+>/ /gs; |
$v =~ s/<[^>]+>/ /gs; |
107 |
|
|
108 |
if ( defined( $pk->{$c} ) ) { |
if ( defined( $pk->{$c} ) ) { |
109 |
$doc->add(Lucene::Document::Field->Keyword( $c, $v )); |
$doc->{ $c } = $v; |
110 |
warn " * $c = $v\n" if ($debug); |
warn " * $c = $v\n" if ($debug); |
111 |
} else { |
} else { |
112 |
$doc->add(Lucene::Document::Field->Text( $c, $v )); |
$doc->{ $c } = $v; |
113 |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
114 |
} |
} |
115 |
} |
} |
116 |
|
|
117 |
# add _owner_id to speed up filtering of search results |
# add _owner_id to speed up filtering of search results |
118 |
$uid ||= Jifty->web->current_user->id; |
$uid ||= Jifty->web->current_user->id; |
119 |
$doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid )); |
$doc->{ '_owner_id' } = $uid; |
120 |
|
|
121 |
|
$self->invindexer->add_doc( $doc ); |
122 |
|
|
123 |
$self->writer->addDocument($doc); |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
124 |
|
|
125 |
Jifty->log->debug("added ", $i->id, " for user $uid to index"); |
return 1; |
126 |
} |
} |
127 |
|
|
128 |
=head2 |
=head2 collection |
129 |
|
|
130 |
my $ItemCollection = Grep::Search->collection( 'search query' ); |
Return C<Grep::Model::ItemCollection> which is result of C<search query> |
131 |
|
|
132 |
=cut |
my $ItemCollection = $search->collection( 'search query' ); |
133 |
|
|
134 |
sub collection { |
=head2 hits |
|
my $self = shift; |
|
135 |
|
|
136 |
my $q = shift or die "no q?"; |
Return number of results from last C<collection> call |
137 |
|
|
138 |
return if ( $self->create ); |
my $num_results = $search->hits; |
139 |
|
|
140 |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
=cut |
|
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
|
141 |
|
|
142 |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
sub collection { |
143 |
|
my $self = shift; |
144 |
|
|
145 |
my $query = $parser->parse( $full_q ); |
my $q = shift or die "no q?"; |
146 |
|
|
147 |
Jifty->log->debug("searching for '$q' using ", $query->toString); |
my $searcher = KinoSearch::Searcher->new( |
148 |
|
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
149 |
|
$self->log->debug("$searcher created"); |
150 |
|
|
151 |
|
my $full_q = "($q)"; |
152 |
|
|
153 |
|
my $uid = Jifty->web->current_user->id; |
154 |
|
$full_q .= ' AND _owner_id:' . $uid if (defined $uid); |
155 |
|
|
156 |
|
$self->log->debug("searching for '$q' using $full_q"); |
157 |
|
|
158 |
|
my $query_parser = KinoSearch::QueryParser->new( |
159 |
|
schema => Grep::Search::Schema->new, |
160 |
|
fields => [ qw/ title link content summary category author / ], |
161 |
|
); |
162 |
|
$query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax |
163 |
|
my $query = $query_parser->parse( $full_q ); |
164 |
|
my $hits = $searcher->search( |
165 |
|
query => $query, |
166 |
|
# offset => $offset, |
167 |
|
# num_wanted => $hits_per_page, |
168 |
|
); |
169 |
|
|
170 |
my $hits = $searcher->search($query); |
$self->hits( $hits->total_hits ); |
|
my $num_hits = $hits->length(); |
|
171 |
|
|
172 |
Jifty->log->debug("found $num_hits results"); |
$self->log->debug("found ", $self->hits, " results"); |
173 |
|
|
174 |
my $collection = Grep::Model::ItemCollection->new(); |
my $collection = Grep::Model::ItemCollection->new(); |
175 |
|
|
176 |
my @results; |
my @results; |
177 |
|
|
178 |
for ( my $i = 0; $i < $num_hits; $i++ ) { |
my $i = 0; |
179 |
|
while ( my $hit = $hits->fetch_hit_hashref ) { |
|
my $doc = $hits->doc( $i ); |
|
180 |
|
|
181 |
my $score = $hits->score($i); |
my $score = $hit->{score}; |
182 |
my $title = $doc->get("title"); |
my $title = $hit->{title}; |
183 |
my $id = $doc->get("id"); |
my $id = $hit->{id}; |
184 |
|
|
185 |
warn "## $i $score $title\n"; |
$self->log->debug("result $i [$id] $title $score"); |
186 |
|
|
187 |
my $item = Grep::Model::Item->new(); |
my $item = Grep::Model::Item->new(); |
188 |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
195 |
|
|
196 |
} |
} |
197 |
|
|
198 |
undef $hits; |
$self->log->debug("finished search"); |
|
undef $query; |
|
|
undef $parser; |
|
|
$searcher->close; |
|
|
undef $searcher; |
|
199 |
|
|
200 |
return $collection; |
return $collection; |
201 |
} |
} |
202 |
|
|
203 |
=head2 finish |
=head2 finish |
204 |
|
|
205 |
Grep::Search->finish |
$search->finish |
206 |
|
|
207 |
=cut |
=cut |
208 |
|
|
209 |
sub finish { |
sub finish { |
210 |
my $self = shift; |
my $self = shift; |
211 |
if ($writer) { |
if ($self->invindexer) { |
212 |
warn "closing index\n"; |
$self->log->debug("closing index"); |
213 |
$writer->close; |
$self->invindexer->finish; |
214 |
} |
} |
|
undef $writer; |
|
|
undef $store; |
|
|
undef $create; |
|
|
undef $analyzer; |
|
215 |
|
|
216 |
return; |
$self->log->debug("finish"); |
|
} |
|
217 |
|
|
218 |
=for TODO |
undef $self; |
219 |
|
|
220 |
sub _signal { |
return 1; |
|
my $s = shift; |
|
|
warn "catched SIG $s\n"; |
|
|
finish(); |
|
|
exit(0); |
|
221 |
} |
} |
222 |
|
|
|
$SIG{'__DIE__'} = \&_signal; |
|
|
$SIG{'INT'} = \&_signal; |
|
|
$SIG{'QUIT'} = \&_signal; |
|
|
|
|
|
=cut |
|
|
|
|
223 |
=head2 snippet |
=head2 snippet |
224 |
|
|
225 |
my $short = $self->snippet( 50, $text ); |
my $short = $self->snippet( 50, $text ); |
226 |
|
|
|
|
|
227 |
=cut |
=cut |
228 |
|
|
229 |
sub snippet { |
sub snippet { |
241 |
} |
} |
242 |
} |
} |
243 |
|
|
244 |
|
package Grep::Search::KeywordField; |
245 |
|
use base qw( KinoSearch::Schema::FieldSpec ); |
246 |
|
sub analyzed { 0 } |
247 |
|
#sub indexed { 1 } |
248 |
|
#sub stored { 1 } |
249 |
|
sub vectorized { 0 } |
250 |
|
|
251 |
|
package Grep::Search::Schema; |
252 |
|
|
253 |
|
=head1 NAME |
254 |
|
|
255 |
|
Grep::Search::Schema - schema definition for full-text search |
256 |
|
|
257 |
|
=cut |
258 |
|
|
259 |
|
use base 'KinoSearch::Schema'; |
260 |
|
use KinoSearch::Analysis::PolyAnalyzer; |
261 |
|
|
262 |
|
our %fields = ( |
263 |
|
id => 'Grep::Search::KeywordField', |
264 |
|
|
265 |
|
in_feed_id => 'Grep::Search::KeywordField', |
266 |
|
in_feed_url => 'Grep::Search::KeywordField', |
267 |
|
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
268 |
|
in_feed_owner => 'Grep::Search::KeywordField', |
269 |
|
in_feed_created_on => 'Grep::Search::KeywordField', |
270 |
|
|
271 |
|
title => 'KinoSearch::Schema::FieldSpec', |
272 |
|
link => 'Grep::Search::KeywordField', |
273 |
|
content => 'KinoSearch::Schema::FieldSpec', |
274 |
|
summary => 'KinoSearch::Schema::FieldSpec', |
275 |
|
category => 'KinoSearch::Schema::FieldSpec', |
276 |
|
author => 'KinoSearch::Schema::FieldSpec', |
277 |
|
issued => 'Grep::Search::KeywordField', |
278 |
|
modified => 'Grep::Search::KeywordField', |
279 |
|
|
280 |
|
_owner_id => 'Grep::Search::KeywordField', |
281 |
|
); |
282 |
|
|
283 |
|
sub analyzer { |
284 |
|
return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); |
285 |
|
} |
286 |
|
|
287 |
1; |
1; |