/[Grep]/lib/Grep/Search.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Grep/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 108 by dpavlin, Sun Mar 4 22:16:23 2007 UTC revision 109 by dpavlin, Wed Mar 14 18:46:37 2007 UTC
# Line 2  package Grep::Search; Line 2  package Grep::Search;
2    
3  use strict;  use strict;
4  use warnings;  use warnings;
5  use base 'Jifty::Object';  use base qw( Class::Accessor Jifty::Object );
6    Grep::Search->mk_accessors( qw( analyzer store writer create index_path ) );
7    
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
9  use Lucene;  use Lucene;
10  use Jifty::Util;  use Jifty::Util;
11    
12  my $index_path = Jifty::Util->app_root . '/var/lucene';  my $debug = 0;
13    
14  my ( $analyzer, $store, $writer );  =head1 NAME
15    
16  my $debug = 0;  Grep::Search - full text search
 my $create;  
17    
18  sub create {  =head1 METHODS
         my $self = shift;  
19    
20          if (defined( $create )) {  =head2 new
21                  $self->log->debug("using previous create $create");  
22                  return $create;    my $search = Grep::Search->new();
23          }  
24    =cut
25    
26    sub new {
27            my $class = shift;        
28            my $self = $class->SUPER::new(@_);
29    
30            my $index_path = Jifty::Util->app_root . '/var/lucene';
31    
32            $self->index_path( $index_path );
33    
34          if (! -e "$index_path/segments") {          if (! -e "$index_path/segments") {
35                  $create = 1;                  $self->create( 1 );
36                  $self->log->debug("create index $index_path");                  $self->log->debug("Creating new index $index_path");
37          } else {          } else {
38                  $create = 0;                  $self->create( 0 );
39                  $self->log->debug("open index: $index_path");                  $self->log->debug("Opening index: $index_path");
40          }          }
         return $create;  
 }  
41    
42  sub analyzer {          $self->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() );
43          my $self = shift;          $self->log->debug($self->analyzer . " created");
         if (! defined( $analyzer )) {  
                 $analyzer = new Lucene::Analysis::Standard::StandardAnalyzer();  
                 $self->log->debug("$analyzer created");  
         }  
         return $analyzer;  
 }  
44    
45  sub store {          $self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) );
46          my $self = shift;          $self->log->debug($self->store, " created");
         if (! defined( $store )) {  
                 $store = Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );  
                 $self->log->debug("$store created");  
         }  
         return $store;  
 }  
47    
48  sub writer {          return $self;
         my $self = shift;  
         if (! defined( $writer )) {  
                 $writer = new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );  
                 $self->log->debug("$writer created");  
         }  
         return $writer;  
49  }  }
50    
51    
52  =head2 add  =head2 add
53    
54    Grep::Search->add( $record, $owner_id );    $search->add( $record, $owner_id );
55    
56  =cut  =cut
57    
# Line 125  sub add { Line 114  sub add {
114          $uid ||= Jifty->web->current_user->id;          $uid ||= Jifty->web->current_user->id;
115          $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));          $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));
116    
117            if (! defined( $self->writer )) {
118                    $self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) );
119                    $self->log->debug($self->writer, " created");
120            }
121    
122          $self->writer->addDocument($doc);          $self->writer->addDocument($doc);
123    
124          $self->log->debug("added ", $i->id, " for user $uid to index");          $self->log->debug("added ", $i->id, " for user $uid to index");
125  }  }
126    
127  =head2  =head2 collection
128    
129    my $ItemCollection = Grep::Search->collection( 'search query' );    my $ItemCollection = $search->collection( 'search query' );
130    
131  =cut  =cut
132    
# Line 195  sub collection { Line 189  sub collection {
189    
190  =head2 finish  =head2 finish
191    
192    Grep::Search->finish    $search->finish
193    
194  =cut  =cut
195    
196  sub finish {  sub finish {
197          my $self = shift;          my $self = shift;
198          if ($writer) {          if ($self->writer) {
199                  $self->log->debug("closing index");                  $self->log->debug("closing index");
200                  $writer->close;                  $self->writer->close;
201          }          }
202          undef $writer;  
203          undef $store;  #       $self->writer( undef );
204          undef $create;  #       $self->store( undef );
205          undef $analyzer;  #       $self->create( undef );
206    #       $self->analyzer( undef );
207    
208          return;          return;
209  }  }
# Line 232  $SIG{'QUIT'} = \&_signal; Line 227  $SIG{'QUIT'} = \&_signal;
227    
228    my $short = $self->snippet( 50, $text );    my $short = $self->snippet( 50, $text );
229    
   
230  =cut  =cut
231    
232  sub snippet {  sub snippet {

Legend:
Removed from v.108  
changed lines
  Added in v.109

  ViewVC Help
Powered by ViewVC 1.1.26