--- trunk/PlusPlus.pm 2004/12/03 21:48:15 5 +++ trunk/PlusPlus.pm 2004/12/04 17:49:20 8 @@ -8,6 +8,7 @@ use Carp; use File::Temp qw/ tempdir /; +#use YAML; =head1 NAME @@ -38,6 +39,7 @@ index_dir => '/path/to/index', index => 'index++', search => 'search++', + debug => 1, ); Options to open are following: @@ -58,6 +60,11 @@ Full or partial path to SWISH++ search executable. By default, it's B. +=item C + +This option (off by default) will produce a lot of debugging output on +C prefixed by C<##>. + =back =cut @@ -79,6 +86,8 @@ $self->{'index'} ||= 'index'; $self->{'search'} ||= 'search'; + print STDERR "## open index_dir: ",$self->{'index_dir'}," index: ",$self->{'index'}, " search: ",$self->{'search'},"\n" if ($self->{'debug'}); + $self ? return $self : return undef; } @@ -146,6 +155,52 @@ return 1; } +=head2 search + +Search your index. + + my @results = $i->search("swhish query"); + +Returns array with result IDs. + +=cut + +sub search { + my $self = shift; + + my $query = shift || return; + + $self->_close_index; + + my @results; + + # escape double quotes in query for shell + $query =~ s/"/\\"/g; + + my $open_cmd = $self->{'search'}." -i ".$self->{'index_dir'}.'/index "'.$query.'" |'; + print STDERR "## search $open_cmd\n" if ($self->{'debug'}); + + CORE::open(SEARCH, $open_cmd) || confess "can't start $open_cmd: $!"; + while() { + next if (/^#/); + chomp; + print STDERR "## $_\n" if ($self->{'debug'}); + my ($rank,$path,$size,$title) = split(/ /,$_,4); + push @results, { + rank => $rank, + path => $path, + size => $size, + title => $title, + } + } + + close(SEARCH) || confess "can't close search"; + + #print STDERR "## results: ",Dump(@results),"\n" if ($self->{'debug'}); + + return @results; +} + =head1 PRIVATE METHODS Private methods implement internals for creating temporary file needed for @@ -168,7 +223,7 @@ my $opt = "-v 4"; - my $open_cmd = '| index '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; + my $open_cmd = '| '.$self->{'index'}.' '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; @@ -190,6 +245,8 @@ } ); +To delete document, just omit body and meta data. + =cut sub _create_doc { @@ -217,6 +274,27 @@ print { $self->{'index_fh'} } $arg->{'path'}."\n"; } +=head2 _close_index + +Close index after indexing. + + $i->_close_index; + +You have to close index before searching. + +=cut + +sub _close_index { + my $self = shift; + + return unless ($self->{'index_fh'}); + + print STDERR "## close index\n" if ($self->{'debug'}); + + close($self->{'index_fh'}); + undef $self->{'index_fh'}; +} + 1; __END__