/[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 47 by dpavlin, Wed Feb 21 03:04:48 2007 UTC revision 64 by dpavlin, Wed Feb 21 20:22:07 2007 UTC
# Line 11  my $index_path = Jifty::Util->app_root . Line 11  my $index_path = Jifty::Util->app_root .
11    
12  my ( $analyzer, $store, $writer );  my ( $analyzer, $store, $writer );
13    
14  my $debug = 0;  my $debug = 1;
15    my $create;
16    
17  sub reopen_index {  sub create {
18          my $self = shift;  
19            if (defined( $create )) {
20                    Jifty->log->debug("using previous create $create");
21                    return $create;
22            }
23    
         $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();  
         my $create = 0;  
24          if (! -e "$index_path/segments") {          if (! -e "$index_path/segments") {
25                  $create = 1;                  $create = 1;
26                  Jifty->log->debug("creating index $index_path") unless ($store);                  Jifty->log->debug("create index $index_path");
27          } else {          } else {
28                  Jifty->log->debug("using index: $index_path") unless ($store);                  $create = 0;
29                    Jifty->log->debug("open index: $index_path");
30          }          }
31          $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $create );          return $create;
32    }
33    
34    sub analyzer {
35            my $self = shift;
36            $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();
37            return $analyzer;
38    }
39    
40    sub store {
41            my $self = shift;
42    
43            $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );
44            return $store;
45    }
46    
47    sub writer {
48            my $self = shift;
49            $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );
50            return $writer;
51  }  }
52    
53  =head2 add  =head2 add
54    
55    Grep::Search->add( $record );    Grep::Search->add( $record, $owner_id );
56    
57  =cut  =cut
58    
# Line 37  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    
         $self->reopen_index;  
   
         $writer ||= new Lucene::Index::IndexWriter( $store, $analyzer, 0 );  
   
67          my $pk = { $i->primary_keys };          my $pk = { $i->primary_keys };
68    
69          my $doc = new Lucene::Document;          my $doc = new Lucene::Document;
# Line 55  sub add { Line 75  sub add {
75                  my $v = $i->$c;                  my $v = $i->$c;
76    
77                  if ( ref($v) ne '' ) {                  if ( ref($v) ne '' ) {
78                          if ($i->$c->can('id')) {  
79                                  $v = $i->$c->id;                          foreach my $f_c ( qw/id name title/ ) {
80                                  warn "  # $c = $v [id]\n" if ($debug);                                  if ( $i->$c->can( $f_c ) ) {
81                                  $doc->add(Lucene::Document::Field->Keyword( $c, $v ));                                          my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
82                          } elsif ($v->isa('Jifty::DateTime')) {                                          my $col = $c . '_' . $f_c;
83                                            if ( $f_v ) {
84                                                    warn "  # $col = $f_v\n" if ($debug);
85                                                    $doc->add(Lucene::Document::Field->Text( $col, $f_v ));
86                                            } else {
87                                                    warn "  . $col is NULL\n" if ($debug);
88                                            }
89                                    }
90                            }
91    
92                            if ($v->isa('Jifty::DateTime')) {
93                                  warn "  d $c = $v\n" if ($debug);                                  warn "  d $c = $v\n" if ($debug);
94                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));
95                          } else {                          } else {
# Line 77  sub add { Line 107  sub add {
107                          warn "  * $c = $v\n" if ($debug);                          warn "  * $c = $v\n" if ($debug);
108                  } else {                  } else {
109                          $doc->add(Lucene::Document::Field->Text( $c, $v ));                          $doc->add(Lucene::Document::Field->Text( $c, $v ));
110                          warn "  + $c = $v\n" if ($debug);                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
111                  }                  }
112          }          }
113    
114          $writer->addDocument($doc);          # 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          Jifty->log->debug("added ", $i->id, " to index");          $self->writer->addDocument($doc);
119    
120            Jifty->log->debug("added ", $i->id, " for user $uid to index");
121  }  }
122    
123  =head2  =head2
# Line 97  sub collection { Line 131  sub collection {
131    
132          my $q = shift or die "no q?";          my $q = shift or die "no q?";
133    
134          $self->reopen_index;          return if ( $self->create );
135    
136            my $searcher = new Lucene::Search::IndexSearcher($self->store);
137            my $parser = new Lucene::QueryParser("content", $self->analyzer);
138    
139            my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
140    
141          my $searcher = new Lucene::Search::IndexSearcher($store);          my $query = $parser->parse( $full_q );
         my $parser = new Lucene::QueryParser("content", $analyzer);  
         my $query = $parser->parse( $q );  
142    
143          Jifty->log->debug("searching for '$q'");          Jifty->log->debug("searching for '$q' using ", $query->toString);
144    
145          my $hits = $searcher->search($query);          my $hits = $searcher->search($query);
146          my $num_hits = $hits->length();          my $num_hits = $hits->length();
# Line 156  sub finish { Line 193  sub finish {
193                  $writer->close;                  $writer->close;
194          }          }
195          undef $writer;          undef $writer;
196            undef $create;
197    
198            return;
199  }  }
200    
201    =for TODO
202    
203  sub _signal {  sub _signal {
204          my $s = shift;          my $s = shift;
205          warn "catched SIG $s\n";          warn "catched SIG $s\n";
# Line 169  $SIG{'__DIE__'} = \&_signal; Line 211  $SIG{'__DIE__'} = \&_signal;
211  $SIG{'INT'} = \&_signal;  $SIG{'INT'} = \&_signal;
212  $SIG{'QUIT'} = \&_signal;  $SIG{'QUIT'} = \&_signal;
213    
214    =cut
215    
216    =head2 snippet
217    
218      my $short = $self->snippet( 50, $text );
219    
220    
221    =cut
222    
223    sub snippet {
224            my $self = shift;
225    
226            my $len = shift or die "no len?";
227            my $m = join(" ", @_);
228    
229            $m =~ s/\s+/ /gs;
230    
231            if (length($m) > $len) {
232                    return substr($m,0,$len) . '...';
233            } else {
234                    return $m;
235            }
236    }
237    
238  1;  1;

Legend:
Removed from v.47  
changed lines
  Added in v.64

  ViewVC Help
Powered by ViewVC 1.1.26