2 |
|
|
3 |
use strict; |
use strict; |
4 |
use warnings; |
use warnings; |
5 |
use base 'Jifty::Object'; |
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 |
|
my $self = shift; |
|
20 |
|
|
21 |
if (defined( $create )) { |
=head2 new |
|
$self->log->debug("using previous create $create"); |
|
|
return $create; |
|
|
} |
|
22 |
|
|
23 |
if (! -e "$index_path/segments") { |
my $search = Grep::Search->new(); |
|
$create = 1; |
|
|
$self->log->debug("create index $index_path"); |
|
|
} else { |
|
|
$create = 0; |
|
|
$self->log->debug("open index: $index_path"); |
|
|
} |
|
|
return $create; |
|
|
} |
|
24 |
|
|
25 |
sub analyzer { |
my $serach = Grep::Search->new( create => 1 ); |
|
my $self = shift; |
|
|
if (! defined( $analyzer )) { |
|
|
$analyzer = new Lucene::Analysis::Standard::StandardAnalyzer(); |
|
|
$self->log->debug("$analyzer created"); |
|
|
} |
|
|
return $analyzer; |
|
|
} |
|
26 |
|
|
27 |
sub store { |
=cut |
|
my $self = shift; |
|
|
if (! defined( $store )) { |
|
|
$store = Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ); |
|
|
$self->log->debug("$store created"); |
|
|
} |
|
|
return $store; |
|
|
} |
|
28 |
|
|
29 |
sub writer { |
sub log { Jifty->web->log } |
30 |
my $self = shift; |
|
31 |
if (! defined( $writer )) { |
sub new { |
32 |
$writer = new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ); |
my $class = shift; |
33 |
$self->log->debug("$writer created"); |
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 |
|
if ( ! -e "$index_path" || $self->create ) { |
40 |
|
$self->log->debug("Creating new index $index_path"); |
41 |
|
$self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) ); |
42 |
|
} else { |
43 |
|
$self->log->debug("Opening index: $index_path"); |
44 |
|
$self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) ); |
45 |
} |
} |
46 |
return $writer; |
|
47 |
|
return $self; |
48 |
} |
} |
49 |
|
|
50 |
=head2 add |
=head2 add |
51 |
|
|
52 |
Grep::Search->add( $record, $owner_id ); |
$search->add( $record, $owner_id ); |
53 |
|
|
54 |
=cut |
=cut |
55 |
|
|
63 |
|
|
64 |
my $pk = { $i->primary_keys }; |
my $pk = { $i->primary_keys }; |
65 |
|
|
66 |
my $doc = new Lucene::Document; |
my $doc; |
67 |
|
|
68 |
my @columns = map { $_->name } $i->columns; |
my @columns = map { $_->name } $i->columns; |
69 |
|
|
79 |
my $col = $c . '_' . $f_c; |
my $col = $c . '_' . $f_c; |
80 |
if ( $f_v ) { |
if ( $f_v ) { |
81 |
warn " # $col = $f_v\n" if ($debug); |
warn " # $col = $f_v\n" if ($debug); |
82 |
$doc->add(Lucene::Document::Field->Text( $col, $f_v )); |
$doc->{ $col } = $f_v; |
83 |
} else { |
} else { |
84 |
warn " . $col is NULL\n" if ($debug); |
warn " . $col is NULL\n" if ($debug); |
85 |
} |
} |
88 |
|
|
89 |
if ($v->isa('Jifty::DateTime')) { |
if ($v->isa('Jifty::DateTime')) { |
90 |
warn " d $c = $v\n" if ($debug); |
warn " d $c = $v\n" if ($debug); |
91 |
$doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); |
$doc->{$c} = $v; |
92 |
} else { |
} else { |
93 |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
94 |
} |
} |
100 |
$v =~ s/<[^>]+>/ /gs; |
$v =~ s/<[^>]+>/ /gs; |
101 |
|
|
102 |
if ( defined( $pk->{$c} ) ) { |
if ( defined( $pk->{$c} ) ) { |
103 |
$doc->add(Lucene::Document::Field->Keyword( $c, $v )); |
$doc->{ $c } = $v; |
104 |
warn " * $c = $v\n" if ($debug); |
warn " * $c = $v\n" if ($debug); |
105 |
} else { |
} else { |
106 |
$doc->add(Lucene::Document::Field->Text( $c, $v )); |
$doc->{ $c } = $v; |
107 |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
108 |
} |
} |
109 |
} |
} |
110 |
|
|
111 |
# add _owner_id to speed up filtering of search results |
# add _owner_id to speed up filtering of search results |
112 |
$uid ||= Jifty->web->current_user->id; |
$uid ||= Jifty->web->current_user->id; |
113 |
$doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid )); |
$doc->{ '_owner_id' } = $uid; |
114 |
|
|
115 |
$self->writer->addDocument($doc); |
$self->invindexer->add_doc( $doc ); |
116 |
|
|
117 |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
$self->log->debug("added ", $i->id, " for user $uid to index"); |
118 |
} |
} |
119 |
|
|
120 |
=head2 |
=head2 collection |
121 |
|
|
122 |
|
Return C<Grep::Model::ItemCollection> which is result of C<search query> |
123 |
|
|
124 |
|
my $ItemCollection = $search->collection( 'search query' ); |
125 |
|
|
126 |
|
=head2 hits |
127 |
|
|
128 |
|
Return number of results from last C<collection> call |
129 |
|
|
130 |
my $ItemCollection = Grep::Search->collection( 'search query' ); |
my $num_results = $search->hits; |
131 |
|
|
132 |
=cut |
=cut |
133 |
|
|
136 |
|
|
137 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
138 |
|
|
139 |
return if ( $self->create ); |
my $searcher = KinoSearch::Searcher->new( |
140 |
|
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
|
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
|
141 |
$self->log->debug("$searcher created"); |
$self->log->debug("$searcher created"); |
|
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
|
|
$self->log->debug("$parser created"); |
|
142 |
|
|
143 |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
144 |
|
|
145 |
my $query = $parser->parse( $full_q ); |
$self->log->debug("searching for '$q' using $full_q"); |
146 |
|
my $hits = $searcher->search( |
147 |
$self->log->debug("searching for '$q' using ", $query->toString); |
query => $full_q, |
148 |
|
# offset => $offset, |
149 |
|
# num_wanted => $hits_per_page, |
150 |
|
); |
151 |
|
|
152 |
my $hits = $searcher->search($query); |
$self->hits( $hits->total_hits ); |
|
my $num_hits = $hits->length(); |
|
153 |
|
|
154 |
$self->log->debug("found $num_hits results"); |
$self->log->debug("found ", $self->hits, " results"); |
155 |
|
|
156 |
my $collection = Grep::Model::ItemCollection->new(); |
my $collection = Grep::Model::ItemCollection->new(); |
157 |
|
|
158 |
my @results; |
my @results; |
159 |
|
|
160 |
for ( my $i = 0; $i < $num_hits; $i++ ) { |
my $i = 0; |
161 |
|
while ( my $hit = $hits->fetch_hit_hashref ) { |
|
my $doc = $hits->doc( $i ); |
|
162 |
|
|
163 |
my $score = $hits->score($i); |
my $score = $hit->{score}; |
164 |
my $title = $doc->get("title"); |
my $title = $hit->{title}; |
165 |
my $id = $doc->get("id"); |
my $id = $hit->{id}; |
166 |
|
|
167 |
warn "## $i $score $title\n"; |
$self->log->debug("result $i [$id] $title $score"); |
168 |
|
|
169 |
my $item = Grep::Model::Item->new(); |
my $item = Grep::Model::Item->new(); |
170 |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
177 |
|
|
178 |
} |
} |
179 |
|
|
180 |
undef $hits; |
$self->log->debug("finished search"); |
|
undef $query; |
|
|
undef $parser; |
|
|
$searcher->close; |
|
|
undef $searcher; |
|
181 |
|
|
182 |
return $collection; |
return $collection; |
183 |
} |
} |
184 |
|
|
185 |
=head2 finish |
=head2 finish |
186 |
|
|
187 |
Grep::Search->finish |
$search->finish |
188 |
|
|
189 |
=cut |
=cut |
190 |
|
|
191 |
sub finish { |
sub finish { |
192 |
my $self = shift; |
my $self = shift; |
193 |
if ($writer) { |
if ($self->invindexer) { |
194 |
warn "closing index\n"; |
$self->log->debug("closing index"); |
195 |
$writer->close; |
$self->invindexer->finish; |
196 |
} |
} |
|
undef $writer; |
|
|
undef $store; |
|
|
undef $create; |
|
|
undef $analyzer; |
|
197 |
|
|
198 |
return; |
$self->log->debug("finish"); |
|
} |
|
199 |
|
|
200 |
=for TODO |
undef $self; |
201 |
|
|
202 |
sub _signal { |
return; |
|
my $s = shift; |
|
|
warn "catched SIG $s\n"; |
|
|
finish(); |
|
|
exit(0); |
|
203 |
} |
} |
204 |
|
|
|
$SIG{'__DIE__'} = \&_signal; |
|
|
$SIG{'INT'} = \&_signal; |
|
|
$SIG{'QUIT'} = \&_signal; |
|
|
|
|
|
=cut |
|
|
|
|
205 |
=head2 snippet |
=head2 snippet |
206 |
|
|
207 |
my $short = $self->snippet( 50, $text ); |
my $short = $self->snippet( 50, $text ); |
208 |
|
|
|
|
|
209 |
=cut |
=cut |
210 |
|
|
211 |
sub snippet { |
sub snippet { |
223 |
} |
} |
224 |
} |
} |
225 |
|
|
226 |
|
package Grep::Search::KeywordField; |
227 |
|
use base qw( KinoSearch::Schema::FieldSpec ); |
228 |
|
sub analyzed { 0 } |
229 |
|
|
230 |
|
package Grep::Search::Schema; |
231 |
|
|
232 |
|
=head1 NAME |
233 |
|
|
234 |
|
Grep::Search::Schema - schema definition for full-text search |
235 |
|
|
236 |
|
=cut |
237 |
|
|
238 |
|
use base 'KinoSearch::Schema'; |
239 |
|
use KinoSearch::Analysis::PolyAnalyzer; |
240 |
|
|
241 |
|
our %fields = ( |
242 |
|
id => 'Grep::Search::KeywordField', |
243 |
|
|
244 |
|
in_feed_id => 'Grep::Search::KeywordField', |
245 |
|
in_feed_url => 'Grep::Search::KeywordField', |
246 |
|
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
247 |
|
in_feed_owner => 'Grep::Search::KeywordField', |
248 |
|
in_feed_created_on => 'Grep::Search::KeywordField', |
249 |
|
|
250 |
|
title => 'KinoSearch::Schema::FieldSpec', |
251 |
|
link => 'Grep::Search::KeywordField', |
252 |
|
content => 'KinoSearch::Schema::FieldSpec', |
253 |
|
summary => 'KinoSearch::Schema::FieldSpec', |
254 |
|
category => 'KinoSearch::Schema::FieldSpec', |
255 |
|
author => 'KinoSearch::Schema::FieldSpec', |
256 |
|
issued => 'Grep::Search::KeywordField', |
257 |
|
modified => 'Grep::Search::KeywordField', |
258 |
|
|
259 |
|
_owner_id => 'Grep::Search::KeywordField', |
260 |
|
); |
261 |
|
|
262 |
|
sub analyzer { |
263 |
|
return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); |
264 |
|
} |
265 |
|
|
266 |
1; |
1; |