1 |
package Grep::Search; |
package Grep::Search::KinoSearch; |
2 |
|
|
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( create index_path hits ) ); |
Grep::Search::KinoSearch->mk_accessors( qw( create index_path hits ) ); |
7 |
|
|
8 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
9 |
use KinoSearch::InvIndexer; |
use KinoSearch::InvIndexer; |
10 |
use KinoSearch::Searcher; |
use KinoSearch::Searcher; |
11 |
use Jifty::Util; |
use Jifty::Util; |
12 |
|
|
13 |
my $debug = 1; |
my $debug = 0; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
17 |
Grep::Search - full text search using L<KinoSearch> |
Grep::Search::KinoSearch - full text search using L<KinoSearch> |
18 |
|
|
19 |
=head1 METHODS |
=head1 METHODS |
20 |
|
|
21 |
=head2 new |
=head2 new |
22 |
|
|
23 |
my $search = Grep::Search->new(); |
my $search = Grep::Search::KinoSearch->new(); |
24 |
|
|
25 |
my $search = Grep::Search->new( create => 1 ); |
my $search = Grep::Search::KinoSearch->new( create => 1 ); |
26 |
|
|
27 |
=cut |
=cut |
28 |
|
|
47 |
|
|
48 |
=cut |
=cut |
49 |
|
|
50 |
my $indexes; |
our $indexes; |
51 |
|
|
52 |
sub invindexer { |
sub invindexer { |
53 |
my $self = shift; |
my $self = shift; |
57 |
if ( $invindexer = $indexes->{$index_path} ) { |
if ( $invindexer = $indexes->{$index_path} ) { |
58 |
$self->log->debug("Using cached index $index_path"); |
$self->log->debug("Using cached index $index_path"); |
59 |
} else { |
} else { |
60 |
if ( ! -e "$index_path" || $self->create ) { |
if ( $self->create || -e $index_path ) { |
61 |
$self->log->debug("Creating new index $index_path"); |
$self->log->debug("Creating new index $index_path"); |
62 |
my $invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ); |
$invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::KinoSearch::Schema->clobber( $index_path ) ) |
63 |
|
or die "can't create index $index_path: $!"; |
64 |
|
$self->create( 0 ); |
65 |
} else { |
} else { |
66 |
$self->log->debug("Opening index: $index_path"); |
$self->log->debug("Opening index: $index_path"); |
67 |
$invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ); |
$invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::KinoSearch::Schema->open( $index_path ) ) |
68 |
|
or die "can't open index $index_path: $!"; |
69 |
} |
} |
70 |
$indexes->{$index_path} = $invindexer; |
$indexes->{$index_path} = $invindexer; |
71 |
} |
} |
72 |
|
|
73 |
$self->invindexer( $invindexer ); |
return $invindexer; |
|
|
|
74 |
} |
} |
75 |
|
|
76 |
=head2 add |
=head2 add |
169 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
170 |
|
|
171 |
my $searcher = KinoSearch::Searcher->new( |
my $searcher = KinoSearch::Searcher->new( |
172 |
invindex => Grep::Search::Schema->open( $self->index_path ), ); |
invindex => Grep::Search::KinoSearch::Schema->open( $self->index_path ), ); |
173 |
$self->log->debug("$searcher created"); |
$self->log->debug("$searcher created"); |
174 |
|
|
175 |
my $full_q = "($q)"; |
my $full_q = "($q)"; |
180 |
$self->log->debug("searching for '$q' using $full_q"); |
$self->log->debug("searching for '$q' using $full_q"); |
181 |
|
|
182 |
my $query_parser = KinoSearch::QueryParser->new( |
my $query_parser = KinoSearch::QueryParser->new( |
183 |
schema => Grep::Search::Schema->new, |
schema => Grep::Search::KinoSearch::Schema->new, |
184 |
fields => [ qw/ title link content summary category author / ], |
fields => [ qw/ title link content summary category author / ], |
185 |
); |
); |
186 |
$query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax |
$query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax |
265 |
} |
} |
266 |
} |
} |
267 |
|
|
268 |
package Grep::Search::KeywordField; |
package Grep::Search::KinoSearch::KeywordField; |
269 |
use base qw( KinoSearch::Schema::FieldSpec ); |
use base qw( KinoSearch::Schema::FieldSpec ); |
270 |
sub analyzed { 0 } |
sub analyzed { 0 } |
271 |
#sub indexed { 1 } |
#sub indexed { 1 } |
272 |
#sub stored { 1 } |
#sub stored { 1 } |
273 |
sub vectorized { 0 } |
sub vectorized { 0 } |
274 |
|
|
275 |
package Grep::Search::Schema; |
package Grep::Search::KinoSearch::Schema; |
276 |
|
|
277 |
=head1 NAME |
=head1 NAME |
278 |
|
|
279 |
Grep::Search::Schema - schema definition for full-text search |
Grep::Search::KinoSearch::Schema - schema definition for full-text search |
280 |
|
|
281 |
=cut |
=cut |
282 |
|
|
284 |
use KinoSearch::Analysis::PolyAnalyzer; |
use KinoSearch::Analysis::PolyAnalyzer; |
285 |
|
|
286 |
our %fields = ( |
our %fields = ( |
287 |
id => 'Grep::Search::KeywordField', |
id => 'Grep::Search::KinoSearch::KeywordField', |
288 |
|
|
289 |
in_feed_id => 'Grep::Search::KeywordField', |
in_feed_id => 'Grep::Search::KinoSearch::KeywordField', |
290 |
in_feed_url => 'Grep::Search::KeywordField', |
in_feed_url => 'Grep::Search::KinoSearch::KeywordField', |
291 |
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
in_feed_title => 'KinoSearch::Schema::FieldSpec', |
292 |
in_feed_owner => 'Grep::Search::KeywordField', |
in_feed_owner => 'Grep::Search::KinoSearch::KeywordField', |
293 |
in_feed_created_on => 'Grep::Search::KeywordField', |
in_feed_created_on => 'Grep::Search::KinoSearch::KeywordField', |
294 |
|
|
295 |
title => 'KinoSearch::Schema::FieldSpec', |
title => 'KinoSearch::Schema::FieldSpec', |
296 |
link => 'Grep::Search::KeywordField', |
link => 'Grep::Search::KinoSearch::KeywordField', |
297 |
content => 'KinoSearch::Schema::FieldSpec', |
content => 'KinoSearch::Schema::FieldSpec', |
298 |
summary => 'KinoSearch::Schema::FieldSpec', |
summary => 'KinoSearch::Schema::FieldSpec', |
299 |
category => 'KinoSearch::Schema::FieldSpec', |
category => 'KinoSearch::Schema::FieldSpec', |
300 |
author => 'KinoSearch::Schema::FieldSpec', |
author => 'KinoSearch::Schema::FieldSpec', |
301 |
created_on => 'Grep::Search::KeywordField', |
created_on => 'Grep::Search::KinoSearch::KeywordField', |
302 |
last_update => 'Grep::Search::KeywordField', |
last_update => 'Grep::Search::KinoSearch::KeywordField', |
303 |
|
|
304 |
_owner_id => 'Grep::Search::KeywordField', |
_owner_id => 'Grep::Search::KinoSearch::KeywordField', |
305 |
); |
); |
306 |
|
|
307 |
sub analyzer { |
sub analyzer { |