Email::Store::Plucene - Search your Email::Store with Plucene


Email-Store-Plucene documentation Contained in the Email-Store-Plucene distribution.

Index


Code Index:

NAME

Top

Email::Store::Plucene - Search your Email::Store with Plucene

SYNOPSIS

Top

  use Email::Store;
  $Email::Store::Plucene::index_path = "/var/db/mailstore_index";
  Email::Store::Mail->store($_) for @mails;
  Email::Store::Plucene->optimize;

  @some_mails = 
    Email::Store::Mail->plucene_search("from:dan list:perl6-internals");

  @may_mails = Email::Store::Mail->plucene_search_during
            ("from:dan list:perl6-internals", "2004-05-01", "2004-05-31");

DESCRIPTION

Top

This module adds Plucene indexing to Email::Store's indexing. Whenever a mail is indexed, an entry will be added in the Plucene index which is located at $Email::Store::Plucene::index_path. If you don't change this variable, you'll end up with an index called emailstore-index in the current directory.

METHODS

Top

The module hooks into the store method in the usual way, and provides two new search methods:

plucene_search

This takes a query and returns a list of mails matching that query. The query terms are by default joined together with the OR operator.

plucene_search_during

As above, but also takes two ISO format dates, returning only mails in that period.

NOTES FOR PLUG-IN WRITERS

Top

This module provides a hook called on_gather_plucene_fields. You should provide methods called on_gather_plucene_fields_order (a numeric ordering) and on_gather_plucene_fields. This last should expect a Email::Store::Mail object and a hash reference. Write into this hash reference any fields you want to be searchable:

    package Email::Store::Annotations;
    sub on_gather_plucene_fields_order { 10 }
    sub on_gather_plucene_fields {
        my ($self, $mail, $hash);
        $hash->{notes} = join " ", $mail->annotations;
    }

Now you should be able to use notes:foo to search for mails with "foo" in their annotations.

COPYRIGHT AND LICENSE

Top


Email-Store-Plucene documentation Contained in the Email-Store-Plucene distribution.

package Email::Store::Plucene;
use 5.006;
use strict;
use warnings;
our $VERSION = '0.02';
use Plucene::Simple; # For now
our $index_path ||= "./emailstore-index";
use Module::Pluggable::Ordered search_path => ["Email::Store"];

sub on_store_order { 99 }
sub on_store {
    my ($self, $mail) = @_;
    my $hash = {};
    $self->call_plugins("on_gather_plucene_fields", $mail, $hash);
    my $plucy = Plucene::Simple->open($index_path);
    $plucy->add($mail->id, $hash);
}

sub optimize {
    my $plucy = Plucene::Simple->open($index_path);
    $plucy->optimize;
}
    
sub on_gather_plucene_fields_order { 1 } # I really am the king of this
sub on_gather_plucene_fields {
    my ($self, $mail, $hash) = @_;
    $hash->{list} = join " ", map {$_->name} $mail->lists;
    # At some point we might want to be able to search for "from_id",
    # mail from a specific entity, but that would require us to use
    # tokenizers which understood numbers, so we'll leave it for a later
    # release.
    for (qw(From Cc To)) {
        $hash->{lc $_} = join " ", map {$_->name->name} 
                                       $mail->addressings(role => $_);
    }
    $hash->{text} = $mail->simple->body;
}

package Email::Store::Mail;
sub plucene_search {
    my ($class, $terms) = @_;
    my $plucy = Plucene::Simple->open($index_path);
    return $class->_ids_to_objects([map {{ message_id => $_ }} $plucy->search($terms)]);
}

sub plucene_search_during {
    my ($class, @terms) = @_;
    my $plucy = Plucene::Simple->open($index_path);
    return $class->_ids_to_objects([map {{ message_id => $_ }} $plucy->search_during(@terms)]);
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!