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 = 0; |
Grep::Search - full text search using L<KinoSearch> |
18 |
|
|
19 |
sub create { |
=head1 METHODS |
20 |
|
|
21 |
my $create = 0; |
=head2 new |
|
if (! -e "$index_path/segments") { |
|
|
$create = 1; |
|
|
Jifty->log->debug("create index $index_path") unless ($store); |
|
|
} else { |
|
|
Jifty->log->debug("open index: $index_path") unless ($store); |
|
|
} |
|
|
return $create; |
|
|
} |
|
22 |
|
|
23 |
sub analyzer { |
my $search = Grep::Search->new(); |
|
my $self = shift; |
|
|
$analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); |
|
|
return $analyzer; |
|
|
} |
|
24 |
|
|
25 |
sub store { |
my $search = Grep::Search->new( create => 1 ); |
|
my $self = shift; |
|
26 |
|
|
27 |
$store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ); |
=head2 invindexer |
|
return $store; |
|
|
} |
|
28 |
|
|
29 |
sub writer { |
Accessor to call any method defined on L<KinoSearch::InvIndexer> |
30 |
my $self = shift; |
|
31 |
$writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ); |
$search->invindexer->delete_by_term( 'id', 42 ); |
32 |
return $writer; |
|
33 |
|
=cut |
34 |
|
|
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 ); |
$search->add( $record, $owner_id ); |
59 |
|
|
60 |
=cut |
=cut |
61 |
|
|
63 |
my $self = shift; |
my $self = shift; |
64 |
|
|
65 |
my $i = shift or die "no record to add"; |
my $i = shift or die "no record to add"; |
66 |
|
my $uid = shift; |
67 |
|
|
68 |
die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record')); |
die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record')); |
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 |
|
|
78 |
my $v = $i->$c; |
my $v = $i->$c; |
79 |
|
|
80 |
if ( ref($v) ne '' ) { |
if ( ref($v) ne '' ) { |
81 |
if ($i->$c->can('id')) { |
|
82 |
$v = $i->$c->id; |
foreach my $f_c ( qw/id name title/ ) { |
83 |
warn " # $c = $v [id]\n" if ($debug); |
if ( $i->$c->can( $f_c ) ) { |
84 |
$doc->add(Lucene::Document::Field->Keyword( $c, $v )); |
my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c }; |
85 |
} elsif ($v->isa('Jifty::DateTime')) { |
my $col = $c . '_' . $f_c; |
86 |
|
if ( $f_v ) { |
87 |
|
warn " # $col = $f_v\n" if ($debug); |
88 |
|
$doc->{ $col } = $f_v; |
89 |
|
} else { |
90 |
|
warn " . $col is NULL\n" if ($debug); |
91 |
|
} |
92 |
|
} |
93 |
|
} |
94 |
|
|
95 |
|
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 = $v\n" if ($debug); |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
114 |
} |
} |
115 |
} |
} |
116 |
|
|
117 |
$self->writer->addDocument($doc); |
# add _owner_id to speed up filtering of search results |
118 |
|
$uid ||= Jifty->web->current_user->id; |
119 |
|
$doc->{ '_owner_id' } = $uid; |
120 |
|
|
121 |
|
$self->invindexer->add_doc( $doc ); |
122 |
|
|
123 |
Jifty->log->debug("added ", $i->id, " to index"); |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
124 |
} |
} |
125 |
|
|
126 |
=head2 |
=head2 collection |
127 |
|
|
128 |
|
Return C<Grep::Model::ItemCollection> which is result of C<search query> |
129 |
|
|
130 |
|
my $ItemCollection = $search->collection( 'search query' ); |
131 |
|
|
132 |
|
=head2 hits |
133 |
|
|
134 |
my $ItemCollection = Grep::Search->collection( 'search query' ); |
Return number of results from last C<collection> call |
135 |
|
|
136 |
|
my $num_results = $search->hits; |
137 |
|
|
138 |
=cut |
=cut |
139 |
|
|
142 |
|
|
143 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
144 |
|
|
145 |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
my $searcher = KinoSearch::Searcher->new( |
146 |
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
147 |
my $query = $parser->parse( $q ); |
$self->log->debug("$searcher created"); |
148 |
|
|
149 |
Jifty->log->debug("searching for '$q'"); |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
150 |
|
|
151 |
|
$self->log->debug("searching for '$q' using $full_q"); |
152 |
|
my $hits = $searcher->search( |
153 |
|
query => $full_q, |
154 |
|
# offset => $offset, |
155 |
|
# num_wanted => $hits_per_page, |
156 |
|
); |
157 |
|
|
158 |
my $hits = $searcher->search($query); |
$self->hits( $hits->total_hits ); |
|
my $num_hits = $hits->length(); |
|
159 |
|
|
160 |
Jifty->log->debug("found $num_hits results"); |
$self->log->debug("found ", $self->hits, " results"); |
161 |
|
|
162 |
my $collection = Grep::Model::ItemCollection->new(); |
my $collection = Grep::Model::ItemCollection->new(); |
163 |
|
|
164 |
my @results; |
my @results; |
165 |
|
|
166 |
for ( my $i = 0; $i < $num_hits; $i++ ) { |
my $i = 0; |
167 |
|
while ( my $hit = $hits->fetch_hit_hashref ) { |
168 |
|
|
169 |
my $doc = $hits->doc( $i ); |
my $score = $hit->{score}; |
170 |
|
my $title = $hit->{title}; |
171 |
|
my $id = $hit->{id}; |
172 |
|
|
173 |
my $score = $hits->score($i); |
$self->log->debug("result $i [$id] $title $score"); |
|
my $title = $doc->get("title"); |
|
|
my $id = $doc->get("id"); |
|
|
|
|
|
warn "## $i $score $title\n"; |
|
174 |
|
|
175 |
my $item = Grep::Model::Item->new(); |
my $item = Grep::Model::Item->new(); |
176 |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
183 |
|
|
184 |
} |
} |
185 |
|
|
186 |
undef $hits; |
$self->log->debug("finished search"); |
|
undef $query; |
|
|
undef $parser; |
|
|
undef $searcher; |
|
187 |
|
|
188 |
return $collection; |
return $collection; |
189 |
} |
} |
190 |
|
|
191 |
=head2 finish |
=head2 finish |
192 |
|
|
193 |
Grep::Search->finish |
$search->finish |
194 |
|
|
195 |
=cut |
=cut |
196 |
|
|
197 |
sub finish { |
sub finish { |
198 |
my $self = shift; |
my $self = shift; |
199 |
if ($writer) { |
if ($self->invindexer) { |
200 |
warn "closing index\n"; |
$self->log->debug("closing index"); |
201 |
$writer->close; |
$self->invindexer->finish; |
202 |
} |
} |
203 |
undef $writer; |
|
204 |
|
$self->log->debug("finish"); |
205 |
|
|
206 |
|
undef $self; |
207 |
|
|
208 |
|
return; |
209 |
} |
} |
210 |
|
|
211 |
sub _signal { |
=head2 snippet |
212 |
my $s = shift; |
|
213 |
warn "catched SIG $s\n"; |
my $short = $self->snippet( 50, $text ); |
214 |
finish(); |
|
215 |
exit(0); |
=cut |
216 |
|
|
217 |
|
sub snippet { |
218 |
|
my $self = shift; |
219 |
|
|
220 |
|
my $len = shift or die "no len?"; |
221 |
|
my $m = join(" ", @_); |
222 |
|
|
223 |
|
$m =~ s/\s+/ /gs; |
224 |
|
|
225 |
|
if (length($m) > $len) { |
226 |
|
return substr($m,0,$len) . '...'; |
227 |
|
} else { |
228 |
|
return $m; |
229 |
|
} |
230 |
} |
} |
231 |
|
|
232 |
$SIG{'__DIE__'} = \&_signal; |
package Grep::Search::KeywordField; |
233 |
$SIG{'INT'} = \&_signal; |
use base qw( KinoSearch::Schema::FieldSpec ); |
234 |
$SIG{'QUIT'} = \&_signal; |
sub analyzed { 0 } |
235 |
|
|
236 |
|
package Grep::Search::Schema; |
237 |
|
|
238 |
|
=head1 NAME |
239 |
|
|
240 |
|
Grep::Search::Schema - schema definition for full-text search |
241 |
|
|
242 |
|
=cut |
243 |
|
|
244 |
|
use base 'KinoSearch::Schema'; |
245 |
|
use KinoSearch::Analysis::PolyAnalyzer; |
246 |
|
|
247 |
|
our %fields = ( |
248 |
|
id => 'Grep::Search::KeywordField', |
249 |
|
|
250 |
|
in_feed_id => 'Grep::Search::KeywordField', |
251 |
|
in_feed_url => 'Grep::Search::KeywordField', |
252 |
|
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
253 |
|
in_feed_owner => 'Grep::Search::KeywordField', |
254 |
|
in_feed_created_on => 'Grep::Search::KeywordField', |
255 |
|
|
256 |
|
title => 'KinoSearch::Schema::FieldSpec', |
257 |
|
link => 'Grep::Search::KeywordField', |
258 |
|
content => 'KinoSearch::Schema::FieldSpec', |
259 |
|
summary => 'KinoSearch::Schema::FieldSpec', |
260 |
|
category => 'KinoSearch::Schema::FieldSpec', |
261 |
|
author => 'KinoSearch::Schema::FieldSpec', |
262 |
|
issued => 'Grep::Search::KeywordField', |
263 |
|
modified => 'Grep::Search::KeywordField', |
264 |
|
|
265 |
|
_owner_id => 'Grep::Search::KeywordField', |
266 |
|
); |
267 |
|
|
268 |
|
sub analyzer { |
269 |
|
return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); |
270 |
|
} |
271 |
|
|
272 |
1; |
1; |