--- lib/Grep/Search.pm 2007/02/21 20:22:07 64 +++ lib/Grep/Search.pm 2007/05/08 14:28:14 145 @@ -2,57 +2,60 @@ use strict; use warnings; +use base qw( Class::Accessor ); +Grep::Search->mk_accessors( qw( invindexer create index_path hits ) ); use Data::Dump qw/dump/; -use Lucene; +use KinoSearch::InvIndexer; +use KinoSearch::Searcher; use Jifty::Util; -my $index_path = Jifty::Util->app_root . '/var/lucene'; +my $debug = 0; -my ( $analyzer, $store, $writer ); +=head1 NAME -my $debug = 1; -my $create; +Grep::Search - full text search using L -sub create { +=head1 METHODS - if (defined( $create )) { - Jifty->log->debug("using previous create $create"); - return $create; - } +=head2 new - if (! -e "$index_path/segments") { - $create = 1; - Jifty->log->debug("create index $index_path"); - } else { - $create = 0; - Jifty->log->debug("open index: $index_path"); - } - return $create; -} + my $search = Grep::Search->new(); -sub analyzer { - my $self = shift; - $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); - return $analyzer; -} + my $search = Grep::Search->new( create => 1 ); -sub store { - my $self = shift; +=head2 invindexer - $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ); - return $store; -} +Accessor to call any method defined on L -sub writer { - my $self = shift; - $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ); - return $writer; + $search->invindexer->delete_by_term( 'id', 42 ); + +=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 ); + + if ( ! -e "$index_path" || $self->create ) { + $self->log->debug("Creating new index $index_path"); + $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) ); + } else { + $self->log->debug("Opening index: $index_path"); + $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) ); + } + + return $self; } =head2 add - Grep::Search->add( $record, $owner_id ); + $search->add( $record, $owner_id ); =cut @@ -66,7 +69,7 @@ my $pk = { $i->primary_keys }; - my $doc = new Lucene::Document; + my $doc; my @columns = map { $_->name } $i->columns; @@ -82,7 +85,7 @@ my $col = $c . '_' . $f_c; if ( $f_v ) { warn " # $col = $f_v\n" if ($debug); - $doc->add(Lucene::Document::Field->Text( $col, $f_v )); + $doc->{ $col } = $f_v; } else { warn " . $col is NULL\n" if ($debug); } @@ -91,7 +94,7 @@ if ($v->isa('Jifty::DateTime')) { warn " d $c = $v\n" if ($debug); - $doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); + $doc->{$c} = $v; } else { warn " s $c = $v [",ref($v),"]\n" if ($debug); } @@ -103,26 +106,34 @@ $v =~ s/<[^>]+>/ /gs; if ( defined( $pk->{$c} ) ) { - $doc->add(Lucene::Document::Field->Keyword( $c, $v )); + $doc->{ $c } = $v; warn " * $c = $v\n" if ($debug); } else { - $doc->add(Lucene::Document::Field->Text( $c, $v )); + $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->add(Lucene::Document::Field->Keyword( '_owner_id', $uid )); + $doc->{ '_owner_id' } = $uid; - $self->writer->addDocument($doc); + $self->invindexer->add_doc( $doc ); - Jifty->log->debug("added ", $i->id, " for user $uid to index"); + $self->log->debug("added ", $i->id, " for user $uid to index"); } -=head2 +=head2 collection + +Return C which is result of C + + my $ItemCollection = $search->collection( 'search query' ); + +=head2 hits + +Return number of results from last C call - my $ItemCollection = Grep::Search->collection( 'search query' ); + my $num_results = $search->hits; =cut @@ -131,35 +142,35 @@ my $q = shift or die "no q?"; - return if ( $self->create ); - - my $searcher = new Lucene::Search::IndexSearcher($self->store); - my $parser = new Lucene::QueryParser("content", $self->analyzer); + my $searcher = KinoSearch::Searcher->new( + invindex => Grep::Search::Schema->open( $self->index_path ), ); + $self->log->debug("$searcher created"); my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; - my $query = $parser->parse( $full_q ); - - Jifty->log->debug("searching for '$q' using ", $query->toString); + $self->log->debug("searching for '$q' using $full_q"); + my $hits = $searcher->search( + query => $full_q, +# offset => $offset, +# num_wanted => $hits_per_page, + ); - my $hits = $searcher->search($query); - my $num_hits = $hits->length(); + $self->hits( $hits->total_hits ); - Jifty->log->debug("found $num_hits results"); + $self->log->debug("found ", $self->hits, " results"); my $collection = Grep::Model::ItemCollection->new(); my @results; - for ( my $i = 0; $i < $num_hits; $i++ ) { - - my $doc = $hits->doc( $i ); + my $i = 0; + while ( my $hit = $hits->fetch_hit_hashref ) { - my $score = $hits->score($i); - my $title = $doc->get("title"); - my $id = $doc->get("id"); + my $score = $hit->{score}; + my $title = $hit->{title}; + my $id = $hit->{id}; - warn "## $i $score $title\n"; + $self->log->debug("result $i [$id] $title $score"); my $item = Grep::Model::Item->new(); my ($ok,$msg) = $item->load_by_cols( id => $id ); @@ -172,52 +183,35 @@ } - undef $hits; - undef $query; - undef $parser; - undef $searcher; + $self->log->debug("finished search"); return $collection; } =head2 finish - Grep::Search->finish + $search->finish =cut sub finish { my $self = shift; - if ($writer) { - warn "closing index\n"; - $writer->close; + if ($self->invindexer) { + $self->log->debug("closing index"); + $self->invindexer->finish; } - undef $writer; - undef $create; - return; -} + $self->log->debug("finish"); -=for TODO + undef $self; -sub _signal { - my $s = shift; - warn "catched SIG $s\n"; - finish(); - exit(0); + return; } -$SIG{'__DIE__'} = \&_signal; -$SIG{'INT'} = \&_signal; -$SIG{'QUIT'} = \&_signal; - -=cut - =head2 snippet my $short = $self->snippet( 50, $text ); - =cut sub snippet { @@ -235,4 +229,44 @@ } } +package Grep::Search::KeywordField; +use base qw( KinoSearch::Schema::FieldSpec ); +sub analyzed { 0 } + +package Grep::Search::Schema; + +=head1 NAME + +Grep::Search::Schema - schema definition for full-text search + +=cut + +use base 'KinoSearch::Schema'; +use KinoSearch::Analysis::PolyAnalyzer; + +our %fields = ( + id => 'Grep::Search::KeywordField', + + in_feed_id => 'Grep::Search::KeywordField', + in_feed_url => 'Grep::Search::KeywordField', + in_feed_title => 'KinoSearch::Schema::FieldSpec', + in_feed_owner => 'Grep::Search::KeywordField', + in_feed_created_on => 'Grep::Search::KeywordField', + + title => 'KinoSearch::Schema::FieldSpec', + link => 'Grep::Search::KeywordField', + content => 'KinoSearch::Schema::FieldSpec', + summary => 'KinoSearch::Schema::FieldSpec', + category => 'KinoSearch::Schema::FieldSpec', + author => 'KinoSearch::Schema::FieldSpec', + issued => 'Grep::Search::KeywordField', + modified => 'Grep::Search::KeywordField', + + _owner_id => 'Grep::Search::KeywordField', +); + +sub analyzer { + return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); +} + 1;