| 1 | package Email::Store::Plucene; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.02'; | ||
| 6 | use Plucene::Simple; # For now | ||
| 7 | our $index_path ||= "./emailstore-index"; | ||
| 8 | use Module::Pluggable::Ordered search_path => ["Email::Store"]; | ||
| 9 | |||
| 10 | sub on_store_order { 99 } | ||
| 11 | sub on_store { | ||
| 12 | my ($self, $mail) = @_; | ||
| 13 | my $hash = {}; | ||
| 14 | $self->call_plugins("on_gather_plucene_fields", $mail, $hash); | ||
| 15 | my $plucy = Plucene::Simple->open($index_path); | ||
| 16 | $plucy->add($mail->id, $hash); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub optimize { | ||
| 20 | my $plucy = Plucene::Simple->open($index_path); | ||
| 21 | $plucy->optimize; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub on_gather_plucene_fields_order { 1 } # I really am the king of this | ||
| 25 | sub on_gather_plucene_fields { | ||
| 26 | my ($self, $mail, $hash) = @_; | ||
| 27 | $hash->{list} = join " ", map {$_->name} $mail->lists; | ||
| 28 | # At some point we might want to be able to search for "from_id", | ||
| 29 | # mail from a specific entity, but that would require us to use | ||
| 30 | # tokenizers which understood numbers, so we'll leave it for a later | ||
| 31 | # release. | ||
| 32 | for (qw(From Cc To)) { | ||
| 33 | $hash->{lc $_} = join " ", map {$_->name->name} | ||
| 34 | $mail->addressings(role => $_); | ||
| 35 | } | ||
| 36 | $hash->{text} = $mail->simple->body; | ||
| 37 | } | ||
| 38 | |||
| 39 | package Email::Store::Mail; | ||
| 40 | sub plucene_search { | ||
| 41 | my ($class, $terms) = @_; | ||
| 42 | my $plucy = Plucene::Simple->open($index_path); | ||
| 43 | return $class->_ids_to_objects([map {{ message_id => $_ }} $plucy->search($terms)]); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub plucene_search_during { | ||
| 47 | my ($class, @terms) = @_; | ||
| 48 | my $plucy = Plucene::Simple->open($index_path); | ||
| 49 | return $class->_ids_to_objects([map {{ message_id => $_ }} $plucy->search_during(@terms)]); | ||
| 50 | } | ||
| 51 | |||
| 52 | 1; | ||
| 53 | __END__ |