--- lib/Grep/Search.pm 2007/04/28 22:53:37 126 +++ lib/Grep/Search.pm 2007/04/29 00:16:05 127 @@ -3,17 +3,18 @@ use strict; use warnings; use base qw( Class::Accessor ); -Grep::Search->mk_accessors( qw( analyzer store writer create index_path ) ); +Grep::Search->mk_accessors( qw( invindexer create index_path ) ); use Data::Dump qw/dump/; -use Lucene; +use KinoSearch::InvIndexer; +use KinoSearch::Searcher; use Jifty::Util; my $debug = 0; =head1 NAME -Grep::Search - full text search +Grep::Search - full text search using L =head1 METHODS @@ -29,24 +30,18 @@ my $class = shift; my $self = $class->SUPER::new(@_); - my $index_path = Jifty::Util->app_root . '/var/lucene'; + my $index_path = Jifty::Util->app_root . '/var/invindex'; $self->index_path( $index_path ); - if (! -e "$index_path/segments") { - $self->create( 1 ); + if (! -e "$index_path") { $self->log->debug("Creating new index $index_path"); + $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) ); } else { - $self->create( 0 ); $self->log->debug("Opening index: $index_path"); + $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) ); } - $self->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() ); - $self->log->debug($self->analyzer . " created"); - - $self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) ); - $self->log->debug($self->store, " created"); - return $self; } @@ -66,7 +61,7 @@ my $pk = { $i->primary_keys }; - my $doc = new Lucene::Document; + my $doc; my @columns = map { $_->name } $i->columns; @@ -82,7 +77,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 +86,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,24 +98,19 @@ $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 )); - - if (! defined( $self->writer )) { - $self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) ); - $self->log->debug($self->writer, " created"); - } + $doc->{ '_owner_id' } = $uid; - $self->writer->addDocument($doc); + $self->invindexer->add_doc( $doc ); $self->log->debug("added ", $i->id, " for user $uid to index"); } @@ -136,21 +126,20 @@ my $q = shift or die "no q?"; - return if ( $self->create ); - - my $searcher = new Lucene::Search::IndexSearcher($self->store); + my $searcher = KinoSearch::Searcher->new( + invindex => Grep::Search::Schema->open( $self->index_path ), ); $self->log->debug("$searcher created"); - my $parser = new Lucene::QueryParser("content", $self->analyzer); - $self->log->debug("$parser created"); my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; - my $query = $parser->parse( $full_q ); - - $self->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(); + my $num_hits = $hits->total_hits; $self->log->debug("found $num_hits results"); @@ -158,13 +147,12 @@ 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}; $self->log->debug("result $i $score $title"); @@ -179,12 +167,7 @@ } - undef $hits; - undef $query; - undef $parser; - undef $searcher; - - $self->log->debug("finished Lucene search"); + $self->log->debug("finished search"); return $collection; } @@ -197,9 +180,9 @@ sub finish { my $self = shift; - if ($self->writer) { + if ($self->invindexer) { $self->log->debug("closing index"); - $self->writer->close; + $self->invindexer->finish; } $self->log->debug("finish"); @@ -209,19 +192,6 @@ return; } -=for TODO - -sub _signal { - my $s = shift; - warn "catched SIG $s\n"; - finish(); - exit(0); -} - -$SIG{'__DIE__'} = \&_signal; -$SIG{'INT'} = \&_signal; -$SIG{'QUIT'} = \&_signal; - =cut =head2 snippet @@ -245,4 +215,44 @@ } } +package Grep::Search::KeywordField; +use base qw( KinoSearch::Schema::FieldSpec ); +sub analyzed { 0 } + +package Grep::Search::Schema; + +=head1 NAME + +Grep::Search::Schema - data definition + +=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;