/[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 60 by dpavlin, Wed Feb 21 19:31:26 2007 UTC revision 112 by dpavlin, Wed Mar 14 21:10:53 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 qw( Class::Accessor );
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 = 1;  Grep::Search - full text search
 my $create;  
17    
18  sub create {  =head1 METHODS
19    
20          if (defined( $create )) {  =head2 new
21                  Jifty->log->debug("using previous create $create");  
22                  return $create;    my $search = Grep::Search->new();
23          }  
24    =cut
25    
26    sub log { Jifty->web->log }
27    
28    sub new {
29            my $class = shift;        
30            my $self = $class->SUPER::new(@_);
31    
32            my $index_path = Jifty::Util->app_root . '/var/lucene';
33    
34            $self->index_path( $index_path );
35    
36          if (! -e "$index_path/segments") {          if (! -e "$index_path/segments") {
37                  $create = 1;                  $self->create( 1 );
38                  Jifty->log->debug("create index $index_path");                  $self->log->debug("Creating new index $index_path");
39          } else {          } else {
40                  $create = 0;                  $self->create( 0 );
41                  Jifty->log->debug("open index: $index_path");                  $self->log->debug("Opening index: $index_path");
42          }          }
         return $create;  
 }  
   
 sub analyzer {  
         my $self = shift;  
         $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();  
         return $analyzer;  
 }  
43    
44  sub store {          $self->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() );
45          my $self = shift;          $self->log->debug($self->analyzer . " created");
46    
47          $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );          $self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) );
48          return $store;          $self->log->debug($self->store, " created");
 }  
49    
50  sub writer {          return $self;
         my $self = shift;  
         $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );  
         return $writer;  
51  }  }
52    
53  =head2 add  =head2 add
54    
55    Grep::Search->add( $record );    $search->add( $record, $owner_id );
56    
57  =cut  =cut
58    
# Line 60  sub add { Line 60  sub add {
60          my $self = shift;          my $self = shift;
61    
62          my $i = shift or die "no record to add";          my $i = shift or die "no record to add";
63            my $uid = shift;
64    
65          die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));          die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));
66    
# Line 110  sub add { Line 111  sub add {
111                  }                  }
112          }          }
113    
114            # add _owner_id to speed up filtering of search results
115            $uid ||= Jifty->web->current_user->id;
116            $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));
117    
118            if (! defined( $self->writer )) {
119                    $self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) );
120                    $self->log->debug($self->writer, " created");
121            }
122    
123          $self->writer->addDocument($doc);          $self->writer->addDocument($doc);
124    
125          Jifty->log->debug("added ", $i->id, " to index");          $self->log->debug("added ", $i->id, " for user $uid to index");
126  }  }
127    
128  =head2  =head2 collection
129    
130    my $ItemCollection = Grep::Search->collection( 'search query' );    my $ItemCollection = $search->collection( 'search query' );
131    
132  =cut  =cut
133    
# Line 129  sub collection { Line 139  sub collection {
139          return if ( $self->create );          return if ( $self->create );
140    
141          my $searcher = new Lucene::Search::IndexSearcher($self->store);          my $searcher = new Lucene::Search::IndexSearcher($self->store);
142            $self->log->debug("$searcher created");
143          my $parser = new Lucene::QueryParser("content", $self->analyzer);          my $parser = new Lucene::QueryParser("content", $self->analyzer);
144          my $query = $parser->parse( $q );          $self->log->debug("$parser created");
145    
146          Jifty->log->debug("searching for '$q' using ", $query->toString);          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
147    
148            my $query = $parser->parse( $full_q );
149    
150            $self->log->debug("searching for '$q' using ", $query->toString);
151    
152          my $hits = $searcher->search($query);          my $hits = $searcher->search($query);
153          my $num_hits = $hits->length();          my $num_hits = $hits->length();
154    
155          Jifty->log->debug("found $num_hits results");          $self->log->debug("found $num_hits results");
156    
157          my $collection = Grep::Model::ItemCollection->new();          my $collection = Grep::Model::ItemCollection->new();
158    
# Line 151  sub collection { Line 166  sub collection {
166                  my $title = $doc->get("title");                  my $title = $doc->get("title");
167                  my $id = $doc->get("id");                  my $id = $doc->get("id");
168    
169                  warn "## $i $score $title\n";                  $self->log->debug("result $i $score $title");
170    
171                  my $item = Grep::Model::Item->new();                  my $item = Grep::Model::Item->new();
172                  my ($ok,$msg) = $item->load_by_cols( id => $id );                  my ($ok,$msg) = $item->load_by_cols( id => $id );
# Line 167  sub collection { Line 182  sub collection {
182          undef $hits;          undef $hits;
183          undef $query;          undef $query;
184          undef $parser;          undef $parser;
185            $searcher->close;
186          undef $searcher;          undef $searcher;
187    
188          return $collection;          return $collection;
# Line 174  sub collection { Line 190  sub collection {
190    
191  =head2 finish  =head2 finish
192    
193    Grep::Search->finish    $search->finish
194    
195  =cut  =cut
196    
197  sub finish {  sub finish {
198          my $self = shift;          my $self = shift;
199          if ($writer) {          if ($self->writer) {
200                  warn "closing index\n";                  $self->log->debug("closing index");
201                  $writer->close;                  $self->writer->close;
202          }          }
203          undef $writer;  
204          undef $create;          $self->log->debug("finish");
205    
206            undef $self;
207    
208          return;          return;
209  }  }
# Line 209  $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.60  
changed lines
  Added in v.112

  ViewVC Help
Powered by ViewVC 1.1.26