--- lib/Grep/Search.pm 2007/02/21 03:04:48 47 +++ lib/Grep/Search.pm 2007/02/21 17:42:24 57 @@ -11,20 +11,37 @@ my ( $analyzer, $store, $writer ); -my $debug = 0; +my $debug = 1; -sub reopen_index { - my $self = shift; +sub 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") unless ($store); } else { - Jifty->log->debug("using index: $index_path") unless ($store); + Jifty->log->debug("open index: $index_path") unless ($store); } - $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 @@ -40,10 +57,6 @@ 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 +68,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,11 +100,11 @@ 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); + $self->writer->addDocument($doc); Jifty->log->debug("added ", $i->id, " to index"); } @@ -97,10 +120,8 @@ my $q = shift or die "no q?"; - $self->reopen_index; - - my $searcher = new Lucene::Search::IndexSearcher($store); - my $parser = new Lucene::QueryParser("content", $analyzer); + 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'"); @@ -158,6 +179,8 @@ undef $writer; } +=for TODO + sub _signal { my $s = shift; warn "catched SIG $s\n"; @@ -169,4 +192,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;