--- lib/Grep/Search/KinoSearch.pm 2008/05/23 18:28:19 189 +++ lib/Grep/Search/KinoSearch.pm 2008/05/23 21:05:42 190 @@ -2,15 +2,13 @@ use strict; use warnings; -use base qw( Class::Accessor ); -Grep::Search::KinoSearch->mk_accessors( qw( create index_path hits ) ); use Data::Dump qw/dump/; use KinoSearch::InvIndexer; use KinoSearch::Searcher; use Jifty::Util; -my $debug = 0; +my $debug = 1; =head1 NAME @@ -18,27 +16,6 @@ =head1 METHODS -=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; -} - =head2 invindexer Accessor to call any method defined on L @@ -52,7 +29,7 @@ sub invindexer { my $self = shift; my $invindexer; - my $index_path = $self->index_path or die "no index_path?"; + my $index_path = Jifty::Util->app_root . '/var/invindex'; if ( $invindexer = $indexes->{$index_path} ) { $self->log->debug("Using cached index $index_path"); @@ -75,101 +52,31 @@ =head2 add - $search->add( $record, $owner_id ); + $search->add( $doc_hash ); =cut sub add { my $self = 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; - + my $doc = shift; $self->invindexer->add_doc( $doc ); - - $self->log->debug("added ", $i->id, " for user $uid to index"); - return 1; } -=head2 collection +=head2 search -Return C which is result of C - - my $ItemCollection = $search->collection( 'search query' ); - -=head2 hits - -Return number of results from last C call - - my $num_results = $search->hits; + my $fetch_hit_coderef = $self->search('search query'); =cut -sub collection { +sub search { my $self = shift; my $q = shift or die "no q?"; + my $index_path = Jifty::Util->app_root . '/var/invindex'; my $searcher = KinoSearch::Searcher->new( - invindex => Grep::Search::KinoSearch::Schema->open( $self->index_path ), ); + invindex => Grep::Search::KinoSearch::Schema->open( $index_path ), ); $self->log->debug("$searcher created"); my $full_q = "($q)"; @@ -193,35 +100,9 @@ $self->hits( $hits->total_hits ); - $self->log->debug("found ", $self->hits, " results"); - - 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; + return sub { + return $hits->fetch_hit; + }; } =head2 finish @@ -244,27 +125,6 @@ return 1; } -=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; - } -} - package Grep::Search::KinoSearch::KeywordField; use base qw( KinoSearch::Schema::FieldSpec ); sub analyzed { 0 }