--- lib/Grep/Search.pm 2007/02/21 13:01:34 49 +++ lib/Grep/Search.pm 2007/02/21 19:10:20 58 @@ -11,16 +11,22 @@ my ( $analyzer, $store, $writer ); -my $debug = 0; +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; } @@ -68,11 +74,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 { @@ -90,7 +106,7 @@ 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); } } @@ -110,6 +126,8 @@ 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 ); @@ -167,8 +185,13 @@ $writer->close; } undef $writer; + undef $create; + + return; } +=for TODO + sub _signal { my $s = shift; warn "catched SIG $s\n"; @@ -180,4 +203,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;