package CGI::Wiki::Search::Plucene;

use strict;
use Search::InvertedIndex;
use Carp "croak";
use Plucene::Simple;

use vars qw( @ISA $VERSION );

$VERSION = 0.08;
use base 'CGI::Wiki::Search::Base';

=head1 NAME

CGI::Wiki::Search::Plucene - Search::InvertedIndex plugin for CGI::Wiki

=head1 SYNOPSIS

  my $indexdb = Search::InvertedIndex::DB::Mysql->new( ... );
  my $search = CGI::Wiki::Search::Plucene->new( indexdb => $indexdb );
  my %wombat_nodes = $search->search_nodes("wombat");

Provides search-related methods for CGI::Wiki

See also L<CGI::Wiki::Base>

=cut

=head1 METHODS

=over 4

=item B<new>

  my $search = CGI::Wiki::Search::Plucene->new( path => "/var/plucene/wiki" );

Takes only one parameter, which is mandatory. C<path> must be a path to
store the index.

=cut

sub _init {
    my ($self, %args) = @_;
    $self->{plucene} = Plucene::Simple->open($args{path});
}

sub search_nodes {
    my ($self, $query, $and_or) = @_;
    local $Plucene::QueryParser::DefaultOperator = "AND" if $and_or eq "and";
    my $i = 1;
    return map { $_ => $i++ } $self->{plucene}->search($query);
}

sub _fuzzy_match {
    my ($self, $string, $canonical) = @_;
    return map { $_ => ($_ eq $string ? 2 : 1) } 
           $self->{plucene}->search("fuzzy:$canonical");
}

sub index_node {
    my ($self, $node, $content) = @_;
    $self->{plucene}->add(
        $node => { content => $content,
                   fuzzy   => $self->canonicalise_title( $node ),
                   title   => $node,
                 }
    );
}

sub _delete_node {
    my ($self, $node) = @_;
    $self->{plucene}->delete( $node );
}

sub supports_phrase_searches { return 1; }
sub supports_fuzzy_searches  { return 1; }

=back

=head1 SEE ALSO

L<CGI::Wiki>, L<CGI::Wiki::Base>

=cut

1;
