--- lib/Grep/Search.pm 2007/02/21 16:06:25 53 +++ lib/Grep/Search.pm 2007/02/21 20:22:07 64 @@ -12,15 +12,21 @@ my ( $analyzer, $store, $writer ); my $debug = 1; +my $create; sub create { - my $create = 0; + if (defined( $create )) { + Jifty->log->debug("using previous create $create"); + return $create; + } + if (! -e "$index_path/segments") { $create = 1; - Jifty->log->debug("create index $index_path") unless ($store); + Jifty->log->debug("create index $index_path"); } else { - Jifty->log->debug("open index: $index_path") unless ($store); + $create = 0; + Jifty->log->debug("open index: $index_path"); } return $create; } @@ -46,7 +52,7 @@ =head2 add - Grep::Search->add( $record ); + Grep::Search->add( $record, $owner_id ); =cut @@ -54,6 +60,7 @@ 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')); @@ -104,9 +111,13 @@ } } + # 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 )); + $self->writer->addDocument($doc); - Jifty->log->debug("added ", $i->id, " to index"); + Jifty->log->debug("added ", $i->id, " for user $uid to index"); } =head2 @@ -120,11 +131,16 @@ 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 $query = $parser->parse( $q ); - Jifty->log->debug("searching for '$q'"); + 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); my $hits = $searcher->search($query); my $num_hits = $hits->length(); @@ -177,8 +193,13 @@ $writer->close; } undef $writer; + undef $create; + + return; } +=for TODO + sub _signal { my $s = shift; warn "catched SIG $s\n"; @@ -190,6 +211,7 @@ $SIG{'INT'} = \&_signal; $SIG{'QUIT'} = \&_signal; +=cut =head2 snippet