--- lib/Grep/Search.pm 2007/03/14 21:10:53 112 +++ lib/Grep/Search.pm 2007/06/10 11:52:24 153 @@ -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 hits ) ); 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 @@ -21,6 +22,14 @@ my $search = Grep::Search->new(); + my $search = Grep::Search->new( create => 1 ); + +=head2 invindexer + +Accessor to call any method defined on L + + $search->invindexer->delete_by_term( 'id', 42 ); + =cut sub log { Jifty->web->log } @@ -29,24 +38,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->create ) { $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 +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,32 +106,37 @@ $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"); + + return 1; } =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 $num_results = $search->hits; + =cut sub collection { @@ -136,37 +144,45 @@ 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 $full_q = "($q)"; - my $query = $parser->parse( $full_q ); + my $uid = Jifty->web->current_user->id; + $full_q .= ' AND _owner_id:' . $uid if (defined $uid); - $self->log->debug("searching for '$q' using ", $query->toString); + $self->log->debug("searching for '$q' using $full_q"); - my $hits = $searcher->search($query); - my $num_hits = $hits->length(); + my $query_parser = KinoSearch::QueryParser->new( + schema => Grep::Search::Schema->new, + fields => [ qw/ title link content summary category author / ], + ); + $query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax + my $query = $query_parser->parse( $full_q ); + my $hits = $searcher->search( + query => $query, +# offset => $offset, +# num_wanted => $hits_per_page, + ); - $self->log->debug("found $num_hits results"); + $self->hits( $hits->total_hits ); + + $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}; - $self->log->debug("result $i $score $title"); + $self->log->debug("result $i [$id] $title $score"); my $item = Grep::Model::Item->new(); my ($ok,$msg) = $item->load_by_cols( id => $id ); @@ -179,11 +195,7 @@ } - undef $hits; - undef $query; - undef $parser; - $searcher->close; - undef $searcher; + $self->log->debug("finished search"); return $collection; } @@ -196,33 +208,18 @@ 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"); undef $self; - return; + return 1; } -=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 my $short = $self->snippet( 50, $text ); @@ -244,4 +241,47 @@ } } +package Grep::Search::KeywordField; +use base qw( KinoSearch::Schema::FieldSpec ); +sub analyzed { 0 } +#sub indexed { 1 } +#sub stored { 1 } +sub vectorized { 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;