/[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 98 by dpavlin, Sat Feb 24 12:16:57 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 = 0;  my $debug = 1;
16    my $create;
17    
18  sub reopen_index {  sub create {
19          my $self = shift;          my $self = shift;
20    
21          $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();          if (defined( $create )) {
22          my $create = 0;                  $self->log->debug("using previous create $create");
23                    return $create;
24            }
25    
26          if (! -e "$index_path/segments") {          if (! -e "$index_path/segments") {
27                  $create = 1;                  $create = 1;
28                  Jifty->log->debug("creating index $index_path") unless ($store);                  $self->log->debug("create index $index_path");
29          } else {          } else {
30                  Jifty->log->debug("using index: $index_path") unless ($store);                  $create = 0;
31                    $self->log->debug("open index: $index_path");
32            }
33            return $create;
34    }
35    
36    sub analyzer {
37            my $self = shift;
38            if (! defined( $analyzer )) {
39                    $analyzer = new Lucene::Analysis::Standard::StandardAnalyzer();
40                    $self->log->debug("$analyzer created");
41            }
42            return $analyzer;
43    }
44    
45    sub store {
46            my $self = shift;
47            if (! defined( $store )) {
48                    $store = Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );
49                    $self->log->debug("$store created");
50          }          }
51          $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $create );          return $store;
52    }
53    
54    sub writer {
55            my $self = shift;
56            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;
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 37  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    
         $self->reopen_index;  
   
         $writer ||= new Lucene::Index::IndexWriter( $store, $analyzer, 0 );  
   
77          my $pk = { $i->primary_keys };          my $pk = { $i->primary_keys };
78    
79          my $doc = new Lucene::Document;          my $doc = new Lucene::Document;
# Line 55  sub add { Line 85  sub add {
85                  my $v = $i->$c;                  my $v = $i->$c;
86    
87                  if ( ref($v) ne '' ) {                  if ( ref($v) ne '' ) {
88                          if ($i->$c->can('id')) {  
89                                  $v = $i->$c->id;                          foreach my $f_c ( qw/id name title/ ) {
90                                  warn "  # $c = $v [id]\n" if ($debug);                                  if ( $i->$c->can( $f_c ) ) {
91                                  $doc->add(Lucene::Document::Field->Keyword( $c, $v ));                                          my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
92                          } elsif ($v->isa('Jifty::DateTime')) {                                          my $col = $c . '_' . $f_c;
93                                            if ( $f_v ) {
94                                                    warn "  # $col = $f_v\n" if ($debug);
95                                                    $doc->add(Lucene::Document::Field->Text( $col, $f_v ));
96                                            } else {
97                                                    warn "  . $col is NULL\n" if ($debug);
98                                            }
99                                    }
100                            }
101    
102                            if ($v->isa('Jifty::DateTime')) {
103                                  warn "  d $c = $v\n" if ($debug);                                  warn "  d $c = $v\n" if ($debug);
104                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));
105                          } else {                          } else {
# Line 77  sub add { Line 117  sub add {
117                          warn "  * $c = $v\n" if ($debug);                          warn "  * $c = $v\n" if ($debug);
118                  } else {                  } else {
119                          $doc->add(Lucene::Document::Field->Text( $c, $v ));                          $doc->add(Lucene::Document::Field->Text( $c, $v ));
120                          warn "  + $c = $v\n" if ($debug);                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
121                  }                  }
122          }          }
123    
124          $writer->addDocument($doc);          # 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);
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 97  sub collection { Line 141  sub collection {
141    
142          my $q = shift or die "no q?";          my $q = shift or die "no q?";
143    
144          $self->reopen_index;          return if ( $self->create );
145    
146          my $searcher = new Lucene::Search::IndexSearcher($store);          my $searcher = new Lucene::Search::IndexSearcher($self->store);
147          my $parser = new Lucene::QueryParser("content", $analyzer);          $self->log->debug("$searcher created");
148          my $query = $parser->parse( $q );          my $parser = new Lucene::QueryParser("content", $self->analyzer);
149            $self->log->debug("$parser created");
150    
151          Jifty->log->debug("searching for '$q'");          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
152    
153            my $query = $parser->parse( $full_q );
154    
155            $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 138  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 156  sub finish { Line 206  sub finish {
206                  $writer->close;                  $writer->close;
207          }          }
208          undef $writer;          undef $writer;
209            undef $store;
210            undef $create;
211            undef $analyzer;
212    
213            return;
214  }  }
215    
216    =for TODO
217    
218  sub _signal {  sub _signal {
219          my $s = shift;          my $s = shift;
220          warn "catched SIG $s\n";          warn "catched SIG $s\n";
# Line 169  $SIG{'__DIE__'} = \&_signal; Line 226  $SIG{'__DIE__'} = \&_signal;
226  $SIG{'INT'} = \&_signal;  $SIG{'INT'} = \&_signal;
227  $SIG{'QUIT'} = \&_signal;  $SIG{'QUIT'} = \&_signal;
228    
229    =cut
230    
231    =head2 snippet
232    
233      my $short = $self->snippet( 50, $text );
234    
235    
236    =cut
237    
238    sub snippet {
239            my $self = shift;
240    
241            my $len = shift or die "no len?";
242            my $m = join(" ", @_);
243    
244            $m =~ s/\s+/ /gs;
245    
246            if (length($m) > $len) {
247                    return substr($m,0,$len) . '...';
248            } else {
249                    return $m;
250            }
251    }
252    
253  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26