--- lib/Grep/Search.pm 2007/02/21 03:04:48 47 +++ lib/Grep/Search.pm 2007/02/21 20:22:07 64 @@ -11,25 +11,48 @@ my ( $analyzer, $store, $writer ); -my $debug = 0; +my $debug = 1; +my $create; -sub reopen_index { - my $self = shift; +sub create { + + if (defined( $create )) { + Jifty->log->debug("using previous create $create"); + return $create; + } - $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); - my $create = 0; if (! -e "$index_path/segments") { $create = 1; - Jifty->log->debug("creating index $index_path") unless ($store); + Jifty->log->debug("create index $index_path"); } else { - Jifty->log->debug("using index: $index_path") unless ($store); + $create = 0; + Jifty->log->debug("open index: $index_path"); } - $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $create ); + return $create; +} + +sub analyzer { + my $self = shift; + $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); + return $analyzer; +} + +sub store { + my $self = shift; + + $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ); + return $store; +} + +sub writer { + my $self = shift; + $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ); + return $writer; } =head2 add - Grep::Search->add( $record ); + Grep::Search->add( $record, $owner_id ); =cut @@ -37,13 +60,10 @@ 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')); - $self->reopen_index; - - $writer ||= new Lucene::Index::IndexWriter( $store, $analyzer, 0 ); - my $pk = { $i->primary_keys }; my $doc = new Lucene::Document; @@ -55,11 +75,21 @@ my $v = $i->$c; if ( ref($v) ne '' ) { - if ($i->$c->can('id')) { - $v = $i->$c->id; - warn " # $c = $v [id]\n" if ($debug); - $doc->add(Lucene::Document::Field->Keyword( $c, $v )); - } elsif ($v->isa('Jifty::DateTime')) { + + 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->add(Lucene::Document::Field->Text( $col, $f_v )); + } else { + warn " . $col is NULL\n" if ($debug); + } + } + } + + if ($v->isa('Jifty::DateTime')) { warn " d $c = $v\n" if ($debug); $doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); } else { @@ -77,13 +107,17 @@ warn " * $c = $v\n" if ($debug); } else { $doc->add(Lucene::Document::Field->Text( $c, $v )); - warn " + $c = $v\n" if ($debug); + warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); } } - $writer->addDocument($doc); + # 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 )); - Jifty->log->debug("added ", $i->id, " to index"); + $self->writer->addDocument($doc); + + Jifty->log->debug("added ", $i->id, " for user $uid to index"); } =head2 @@ -97,13 +131,16 @@ my $q = shift or die "no q?"; - $self->reopen_index; + return if ( $self->create ); + + my $searcher = new Lucene::Search::IndexSearcher($self->store); + my $parser = new Lucene::QueryParser("content", $self->analyzer); + + my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; - my $searcher = new Lucene::Search::IndexSearcher($store); - my $parser = new Lucene::QueryParser("content", $analyzer); - my $query = $parser->parse( $q ); + my $query = $parser->parse( $full_q ); - Jifty->log->debug("searching for '$q'"); + Jifty->log->debug("searching for '$q' using ", $query->toString); my $hits = $searcher->search($query); my $num_hits = $hits->length(); @@ -156,8 +193,13 @@ $writer->close; } undef $writer; + undef $create; + + return; } +=for TODO + sub _signal { my $s = shift; warn "catched SIG $s\n"; @@ -169,4 +211,28 @@ $SIG{'INT'} = \&_signal; $SIG{'QUIT'} = \&_signal; +=cut + +=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; + } +} + 1;