2 |
|
|
3 |
use strict; |
use strict; |
4 |
use warnings; |
use warnings; |
|
use base qw( Class::Accessor ); |
|
|
Grep::Search::KinoSearch->mk_accessors( qw( create index_path hits ) ); |
|
5 |
|
|
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
use KinoSearch::InvIndexer; |
use KinoSearch::InvIndexer; |
8 |
use KinoSearch::Searcher; |
use KinoSearch::Searcher; |
9 |
use Jifty::Util; |
use Jifty::Util; |
10 |
|
|
11 |
my $debug = 0; |
my $debug = 1; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
16 |
|
|
17 |
=head1 METHODS |
=head1 METHODS |
18 |
|
|
|
=head2 new |
|
|
|
|
|
my $search = Grep::Search::KinoSearch->new(); |
|
|
|
|
|
my $search = Grep::Search::KinoSearch->new( create => 1 ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub log { Jifty->web->log } |
|
|
|
|
|
sub new { |
|
|
my $class = shift; |
|
|
my $self = $class->SUPER::new(@_); |
|
|
|
|
|
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
|
|
|
|
|
$self->index_path( $index_path ); |
|
|
|
|
|
return $self; |
|
|
} |
|
|
|
|
19 |
=head2 invindexer |
=head2 invindexer |
20 |
|
|
21 |
Accessor to call any method defined on L<KinoSearch::InvIndexer> |
Accessor to call any method defined on L<KinoSearch::InvIndexer> |
29 |
sub invindexer { |
sub invindexer { |
30 |
my $self = shift; |
my $self = shift; |
31 |
my $invindexer; |
my $invindexer; |
32 |
my $index_path = $self->index_path or die "no index_path?"; |
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
33 |
|
|
34 |
if ( $invindexer = $indexes->{$index_path} ) { |
if ( $invindexer = $indexes->{$index_path} ) { |
35 |
$self->log->debug("Using cached index $index_path"); |
$self->log->debug("Using cached index $index_path"); |
52 |
|
|
53 |
=head2 add |
=head2 add |
54 |
|
|
55 |
$search->add( $record, $owner_id ); |
$search->add( $doc_hash ); |
56 |
|
|
57 |
=cut |
=cut |
58 |
|
|
59 |
sub add { |
sub add { |
60 |
my $self = shift; |
my $self = shift; |
61 |
|
my $doc = shift; |
|
my $i = shift or die "no record to add"; |
|
|
my $uid = shift; |
|
|
|
|
|
die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record')); |
|
|
|
|
|
my $pk = { $i->primary_keys }; |
|
|
|
|
|
my $doc; |
|
|
|
|
|
my @columns = map { $_->name } $i->columns; |
|
|
|
|
|
foreach my $c ( @columns ) { |
|
|
|
|
|
my $v = $i->$c; |
|
|
|
|
|
if ( ref($v) ne '' ) { |
|
|
|
|
|
foreach my $f_c ( qw/id name title/ ) { |
|
|
if ( $i->$c->can( $f_c ) ) { |
|
|
my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c }; |
|
|
my $col = $c . '_' . $f_c; |
|
|
if ( $f_v ) { |
|
|
warn " # $col = $f_v\n" if ($debug); |
|
|
$doc->{ $col } = $f_v; |
|
|
} else { |
|
|
warn " . $col is NULL\n" if ($debug); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if ($v->isa('Jifty::DateTime')) { |
|
|
warn " d $c = $v\n" if ($debug); |
|
|
$doc->{$c} = $v; |
|
|
} else { |
|
|
warn " s $c = $v [",ref($v),"]\n" if ($debug); |
|
|
} |
|
|
next; |
|
|
} |
|
|
|
|
|
next if (! defined($v) || $v eq ''); |
|
|
|
|
|
eval { $v =~ s/<[^>]+>/ /gs; }; |
|
|
if ($@) { |
|
|
Jifty->log->error("can't strip html from $c in item ", $i->id); |
|
|
next; |
|
|
} |
|
|
|
|
|
if ( defined( $pk->{$c} ) ) { |
|
|
$doc->{ $c } = $v; |
|
|
warn " * $c = $v\n" if ($debug); |
|
|
} else { |
|
|
$doc->{ $c } = $v; |
|
|
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
|
|
} |
|
|
} |
|
|
|
|
|
# add _owner_id to speed up filtering of search results |
|
|
$uid ||= Jifty->web->current_user->id; |
|
|
$doc->{ '_owner_id' } = $uid; |
|
|
|
|
62 |
$self->invindexer->add_doc( $doc ); |
$self->invindexer->add_doc( $doc ); |
|
|
|
|
$self->log->debug("added ", $i->id, " for user $uid to index"); |
|
|
|
|
63 |
return 1; |
return 1; |
64 |
} |
} |
65 |
|
|
66 |
=head2 collection |
=head2 search |
67 |
|
|
68 |
Return C<Grep::Model::ItemCollection> which is result of C<search query> |
my $fetch_hit_coderef = $self->search('search query'); |
|
|
|
|
my $ItemCollection = $search->collection( 'search query' ); |
|
|
|
|
|
=head2 hits |
|
|
|
|
|
Return number of results from last C<collection> call |
|
|
|
|
|
my $num_results = $search->hits; |
|
69 |
|
|
70 |
=cut |
=cut |
71 |
|
|
72 |
sub collection { |
sub search { |
73 |
my $self = shift; |
my $self = shift; |
74 |
|
|
75 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
76 |
|
|
77 |
|
my $index_path = Jifty::Util->app_root . '/var/invindex'; |
78 |
my $searcher = KinoSearch::Searcher->new( |
my $searcher = KinoSearch::Searcher->new( |
79 |
invindex => Grep::Search::KinoSearch::Schema->open( $self->index_path ), ); |
invindex => Grep::Search::KinoSearch::Schema->open( $index_path ), ); |
80 |
$self->log->debug("$searcher created"); |
$self->log->debug("$searcher created"); |
81 |
|
|
82 |
my $full_q = "($q)"; |
my $full_q = "($q)"; |
100 |
|
|
101 |
$self->hits( $hits->total_hits ); |
$self->hits( $hits->total_hits ); |
102 |
|
|
103 |
$self->log->debug("found ", $self->hits, " results"); |
return sub { |
104 |
|
return $hits->fetch_hit; |
105 |
my $collection = Grep::Model::ItemCollection->new(); |
}; |
|
|
|
|
my @results; |
|
|
|
|
|
my $i = 0; |
|
|
while ( my $hit = $hits->fetch_hit ) { |
|
|
|
|
|
my $score = $hit->{score}; |
|
|
my $title = $hit->{title}; |
|
|
my $id = $hit->{id}; |
|
|
|
|
|
$self->log->debug("result $i [$id] $title $score"); |
|
|
|
|
|
my $item = Grep::Model::Item->new(); |
|
|
my ($ok,$msg) = $item->load_by_cols( id => $id ); |
|
|
|
|
|
if ( $ok ) { |
|
|
$collection->add_record( $item ); |
|
|
} else { |
|
|
warn "can't load item $id\n"; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
$self->log->debug("finished search"); |
|
|
|
|
|
return $collection; |
|
106 |
} |
} |
107 |
|
|
108 |
=head2 finish |
=head2 finish |
125 |
return 1; |
return 1; |
126 |
} |
} |
127 |
|
|
|
=head2 snippet |
|
|
|
|
|
my $short = $self->snippet( 50, $text ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub snippet { |
|
|
my $self = shift; |
|
|
|
|
|
my $len = shift or die "no len?"; |
|
|
my $m = join(" ", @_); |
|
|
|
|
|
$m =~ s/\s+/ /gs; |
|
|
|
|
|
if (length($m) > $len) { |
|
|
return substr($m,0,$len) . '...'; |
|
|
} else { |
|
|
return $m; |
|
|
} |
|
|
} |
|
|
|
|
128 |
package Grep::Search::KinoSearch::KeywordField; |
package Grep::Search::KinoSearch::KeywordField; |
129 |
use base qw( KinoSearch::Schema::FieldSpec ); |
use base qw( KinoSearch::Schema::FieldSpec ); |
130 |
sub analyzed { 0 } |
sub analyzed { 0 } |