/[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 102 by dpavlin, Sun Mar 4 22:16:23 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';
6    
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Lucene;  use Lucene;
# Line 11  my $index_path = Jifty::Util->app_root . Line 12  my $index_path = Jifty::Util->app_root .
12    
13  my ( $analyzer, $store, $writer );  my ( $analyzer, $store, $writer );
14    
15  my $debug = 1;  my $debug = 0;
16  my $create;  my $create;
17    
18  sub create {  sub create {
19            my $self = shift;
20    
21          if (defined( $create )) {          if (defined( $create )) {
22                  Jifty->log->debug("using previous create $create");                  $self->log->debug("using previous create $create");
23                  return $create;                  return $create;
24          }          }
25    
26          if (! -e "$index_path/segments") {          if (! -e "$index_path/segments") {
27                  $create = 1;                  $create = 1;
28                  Jifty->log->debug("create index $index_path");                  $self->log->debug("create index $index_path");
29          } else {          } else {
30                  $create = 0;                  $create = 0;
31                  Jifty->log->debug("open index: $index_path");                  $self->log->debug("open index: $index_path");
32          }          }
33          return $create;          return $create;
34  }  }
35    
36  sub analyzer {  sub analyzer {
37          my $self = shift;          my $self = shift;
38          $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();          if (! defined( $analyzer )) {
39                    $analyzer = new Lucene::Analysis::Standard::StandardAnalyzer();
40                    $self->log->debug("$analyzer created");
41            }
42          return $analyzer;          return $analyzer;
43  }  }
44    
45  sub store {  sub store {
46          my $self = shift;          my $self = shift;
47            if (! defined( $store )) {
48          $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );                  $store = Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );
49                    $self->log->debug("$store created");
50            }
51          return $store;          return $store;
52  }  }
53    
54  sub writer {  sub writer {
55          my $self = shift;          my $self = shift;
56          $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );          if (! defined( $writer )) {
57                    $writer = new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );
58                    $self->log->debug("$writer created");
59            }
60          return $writer;          return $writer;
61  }  }
62    
63  =head2 add  =head2 add
64    
65    Grep::Search->add( $record );    Grep::Search->add( $record, $owner_id );
66    
67  =cut  =cut
68    
# Line 60  sub add { Line 70  sub add {
70          my $self = shift;          my $self = shift;
71    
72          my $i = shift or die "no record to add";          my $i = shift or die "no record to add";
73            my $uid = shift;
74    
75          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'));
76    
# Line 110  sub add { Line 121  sub add {
121                  }                  }
122          }          }
123    
124            # add _owner_id to speed up filtering of search results
125            $uid ||= Jifty->web->current_user->id;
126            $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));
127    
128          $self->writer->addDocument($doc);          $self->writer->addDocument($doc);
129    
130          Jifty->log->debug("added ", $i->id, " to index");          $self->log->debug("added ", $i->id, " for user $uid to index");
131  }  }
132    
133  =head2  =head2
# Line 129  sub collection { Line 144  sub collection {
144          return if ( $self->create );          return if ( $self->create );
145    
146          my $searcher = new Lucene::Search::IndexSearcher($self->store);          my $searcher = new Lucene::Search::IndexSearcher($self->store);
147            $self->log->debug("$searcher created");
148          my $parser = new Lucene::QueryParser("content", $self->analyzer);          my $parser = new Lucene::QueryParser("content", $self->analyzer);
149          my $query = $parser->parse( $q );          $self->log->debug("$parser created");
150    
151            my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
152    
153            my $query = $parser->parse( $full_q );
154    
155          Jifty->log->debug("searching for '$q' using ", $query->toString);          $self->log->debug("searching for '$q' using ", $query->toString);
156    
157          my $hits = $searcher->search($query);          my $hits = $searcher->search($query);
158          my $num_hits = $hits->length();          my $num_hits = $hits->length();
159    
160          Jifty->log->debug("found $num_hits results");          $self->log->debug("found $num_hits results");
161    
162          my $collection = Grep::Model::ItemCollection->new();          my $collection = Grep::Model::ItemCollection->new();
163    
# Line 151  sub collection { Line 171  sub collection {
171                  my $title = $doc->get("title");                  my $title = $doc->get("title");
172                  my $id = $doc->get("id");                  my $id = $doc->get("id");
173    
174                  warn "## $i $score $title\n";                  $self->log->debug("result $i $score $title");
175    
176                  my $item = Grep::Model::Item->new();                  my $item = Grep::Model::Item->new();
177                  my ($ok,$msg) = $item->load_by_cols( id => $id );                  my ($ok,$msg) = $item->load_by_cols( id => $id );
# Line 167  sub collection { Line 187  sub collection {
187          undef $hits;          undef $hits;
188          undef $query;          undef $query;
189          undef $parser;          undef $parser;
190            $searcher->close;
191          undef $searcher;          undef $searcher;
192    
193          return $collection;          return $collection;
# Line 181  sub collection { Line 202  sub collection {
202  sub finish {  sub finish {
203          my $self = shift;          my $self = shift;
204          if ($writer) {          if ($writer) {
205                  warn "closing index\n";                  $self->log->debug("closing index");
206                  $writer->close;                  $writer->close;
207          }          }
208          undef $writer;          undef $writer;
209            undef $store;
210          undef $create;          undef $create;
211            undef $analyzer;
212    
213          return;          return;
214  }  }

Legend:
Removed from v.60  
changed lines
  Added in v.102

  ViewVC Help
Powered by ViewVC 1.1.26