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( invindexer create index_path ) ); |
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 = 0; |
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 |
|
|
30 |
my $class = shift; |
my $class = shift; |
31 |
my $self = $class->SUPER::new(@_); |
my $self = $class->SUPER::new(@_); |
32 |
|
|
33 |
my $index_path = Jifty::Util->app_root . '/var/lucene'; |
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
34 |
|
|
35 |
$self->index_path( $index_path ); |
$self->index_path( $index_path ); |
36 |
|
|
37 |
if (! -e "$index_path/segments") { |
if (! -e "$index_path") { |
|
$self->create( 1 ); |
|
38 |
$self->log->debug("Creating new index $index_path"); |
$self->log->debug("Creating new index $index_path"); |
39 |
|
$self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) ); |
40 |
} else { |
} else { |
|
$self->create( 0 ); |
|
41 |
$self->log->debug("Opening index: $index_path"); |
$self->log->debug("Opening index: $index_path"); |
42 |
|
$self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) ); |
43 |
} |
} |
44 |
|
|
|
$self->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() ); |
|
|
$self->log->debug($self->analyzer . " created"); |
|
|
|
|
|
$self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) ); |
|
|
$self->log->debug($self->store, " created"); |
|
|
|
|
45 |
return $self; |
return $self; |
46 |
} |
} |
47 |
|
|
61 |
|
|
62 |
my $pk = { $i->primary_keys }; |
my $pk = { $i->primary_keys }; |
63 |
|
|
64 |
my $doc = new Lucene::Document; |
my $doc; |
65 |
|
|
66 |
my @columns = map { $_->name } $i->columns; |
my @columns = map { $_->name } $i->columns; |
67 |
|
|
77 |
my $col = $c . '_' . $f_c; |
my $col = $c . '_' . $f_c; |
78 |
if ( $f_v ) { |
if ( $f_v ) { |
79 |
warn " # $col = $f_v\n" if ($debug); |
warn " # $col = $f_v\n" if ($debug); |
80 |
$doc->add(Lucene::Document::Field->Text( $col, $f_v )); |
$doc->{ $col } = $f_v; |
81 |
} else { |
} else { |
82 |
warn " . $col is NULL\n" if ($debug); |
warn " . $col is NULL\n" if ($debug); |
83 |
} |
} |
86 |
|
|
87 |
if ($v->isa('Jifty::DateTime')) { |
if ($v->isa('Jifty::DateTime')) { |
88 |
warn " d $c = $v\n" if ($debug); |
warn " d $c = $v\n" if ($debug); |
89 |
$doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); |
$doc->{$c} = $v; |
90 |
} else { |
} else { |
91 |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
92 |
} |
} |
98 |
$v =~ s/<[^>]+>/ /gs; |
$v =~ s/<[^>]+>/ /gs; |
99 |
|
|
100 |
if ( defined( $pk->{$c} ) ) { |
if ( defined( $pk->{$c} ) ) { |
101 |
$doc->add(Lucene::Document::Field->Keyword( $c, $v )); |
$doc->{ $c } = $v; |
102 |
warn " * $c = $v\n" if ($debug); |
warn " * $c = $v\n" if ($debug); |
103 |
} else { |
} else { |
104 |
$doc->add(Lucene::Document::Field->Text( $c, $v )); |
$doc->{ $c } = $v; |
105 |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
106 |
} |
} |
107 |
} |
} |
108 |
|
|
109 |
# add _owner_id to speed up filtering of search results |
# add _owner_id to speed up filtering of search results |
110 |
$uid ||= Jifty->web->current_user->id; |
$uid ||= Jifty->web->current_user->id; |
111 |
$doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid )); |
$doc->{ '_owner_id' } = $uid; |
|
|
|
|
if (! defined( $self->writer )) { |
|
|
$self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) ); |
|
|
$self->log->debug($self->writer, " created"); |
|
|
} |
|
112 |
|
|
113 |
$self->writer->addDocument($doc); |
$self->invindexer->add_doc( $doc ); |
114 |
|
|
115 |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
116 |
} |
} |
126 |
|
|
127 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
128 |
|
|
129 |
return if ( $self->create ); |
my $searcher = KinoSearch::Searcher->new( |
130 |
|
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
|
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
|
131 |
$self->log->debug("$searcher created"); |
$self->log->debug("$searcher created"); |
|
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
|
|
$self->log->debug("$parser created"); |
|
132 |
|
|
133 |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
134 |
|
|
135 |
my $query = $parser->parse( $full_q ); |
$self->log->debug("searching for '$q' using $full_q"); |
136 |
|
my $hits = $searcher->search( |
137 |
$self->log->debug("searching for '$q' using ", $query->toString); |
query => $full_q, |
138 |
|
# offset => $offset, |
139 |
|
# num_wanted => $hits_per_page, |
140 |
|
); |
141 |
|
|
142 |
my $hits = $searcher->search($query); |
my $num_hits = $hits->total_hits; |
|
my $num_hits = $hits->length(); |
|
143 |
|
|
144 |
$self->log->debug("found $num_hits results"); |
$self->log->debug("found $num_hits results"); |
145 |
|
|
147 |
|
|
148 |
my @results; |
my @results; |
149 |
|
|
150 |
for ( my $i = 0; $i < $num_hits; $i++ ) { |
my $i = 0; |
151 |
|
while ( my $hit = $hits->fetch_hit_hashref ) { |
|
my $doc = $hits->doc( $i ); |
|
152 |
|
|
153 |
my $score = $hits->score($i); |
my $score = $hit->{score}; |
154 |
my $title = $doc->get("title"); |
my $title = $hit->{title}; |
155 |
my $id = $doc->get("id"); |
my $id = $hit->{id}; |
156 |
|
|
157 |
$self->log->debug("result $i $score $title"); |
$self->log->debug("result $i $score $title"); |
158 |
|
|
167 |
|
|
168 |
} |
} |
169 |
|
|
170 |
undef $hits; |
$self->log->debug("finished search"); |
|
undef $query; |
|
|
undef $parser; |
|
|
undef $searcher; |
|
|
|
|
|
$self->log->debug("finished Lucene search"); |
|
171 |
|
|
172 |
return $collection; |
return $collection; |
173 |
} |
} |
180 |
|
|
181 |
sub finish { |
sub finish { |
182 |
my $self = shift; |
my $self = shift; |
183 |
if ($self->writer) { |
if ($self->invindexer) { |
184 |
$self->log->debug("closing index"); |
$self->log->debug("closing index"); |
185 |
$self->writer->close; |
$self->invindexer->finish; |
186 |
} |
} |
187 |
|
|
188 |
$self->log->debug("finish"); |
$self->log->debug("finish"); |
192 |
return; |
return; |
193 |
} |
} |
194 |
|
|
|
=for TODO |
|
|
|
|
|
sub _signal { |
|
|
my $s = shift; |
|
|
warn "catched SIG $s\n"; |
|
|
finish(); |
|
|
exit(0); |
|
|
} |
|
|
|
|
|
$SIG{'__DIE__'} = \&_signal; |
|
|
$SIG{'INT'} = \&_signal; |
|
|
$SIG{'QUIT'} = \&_signal; |
|
|
|
|
195 |
=cut |
=cut |
196 |
|
|
197 |
=head2 snippet |
=head2 snippet |
215 |
} |
} |
216 |
} |
} |
217 |
|
|
218 |
|
package Grep::Search::KeywordField; |
219 |
|
use base qw( KinoSearch::Schema::FieldSpec ); |
220 |
|
sub analyzed { 0 } |
221 |
|
|
222 |
|
package Grep::Search::Schema; |
223 |
|
|
224 |
|
=head1 NAME |
225 |
|
|
226 |
|
Grep::Search::Schema - data definition |
227 |
|
|
228 |
|
=cut |
229 |
|
|
230 |
|
use base 'KinoSearch::Schema'; |
231 |
|
use KinoSearch::Analysis::PolyAnalyzer; |
232 |
|
|
233 |
|
our %fields = ( |
234 |
|
id => 'Grep::Search::KeywordField', |
235 |
|
|
236 |
|
in_feed_id => 'Grep::Search::KeywordField', |
237 |
|
in_feed_url => 'Grep::Search::KeywordField', |
238 |
|
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
239 |
|
in_feed_owner => 'Grep::Search::KeywordField', |
240 |
|
in_feed_created_on => 'Grep::Search::KeywordField', |
241 |
|
|
242 |
|
title => 'KinoSearch::Schema::FieldSpec', |
243 |
|
link => 'Grep::Search::KeywordField', |
244 |
|
content => 'KinoSearch::Schema::FieldSpec', |
245 |
|
summary => 'KinoSearch::Schema::FieldSpec', |
246 |
|
category => 'KinoSearch::Schema::FieldSpec', |
247 |
|
author => 'KinoSearch::Schema::FieldSpec', |
248 |
|
issued => 'Grep::Search::KeywordField', |
249 |
|
modified => 'Grep::Search::KeywordField', |
250 |
|
|
251 |
|
_owner_id => 'Grep::Search::KeywordField', |
252 |
|
); |
253 |
|
|
254 |
|
sub analyzer { |
255 |
|
return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); |
256 |
|
} |
257 |
|
|
258 |
1; |
1; |