/[Grep]/lib/Grep/Search/KinoSearch.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/KinoSearch.pm

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

revision 49 by dpavlin, Wed Feb 21 13:01:34 2007 UTC revision 110 by dpavlin, Wed Mar 14 20:02:19 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 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
17    
18    =head1 METHODS
19    
20  sub create {  =head2 new
21    
22      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    
         my $create = 0;  
34          if (! -e "$index_path/segments") {          if (! -e "$index_path/segments") {
35                  $create = 1;                  $self->create( 1 );
36                  Jifty->log->debug("create index $index_path") unless ($store);                  $self->log->debug("Creating new index $index_path");
37          } else {          } else {
38                  Jifty->log->debug("open index: $index_path") unless ($store);                  $self->create( 0 );
39                    $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");
         $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();  
         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");
47    
48          $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );          return $self;
         return $store;  
49  }  }
50    
 sub writer {  
         my $self = shift;  
         $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );  
         return $writer;  
 }  
51    
52  =head2 add  =head2 add
53    
54    Grep::Search->add( $record );    $search->add( $record, $owner_id );
55    
56  =cut  =cut
57    
# Line 54  sub add { Line 59  sub add {
59          my $self = shift;          my $self = shift;
60    
61          my $i = shift or die "no record to add";          my $i = shift or die "no record to add";
62            my $uid = shift;
63    
64          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'));
65    
# Line 68  sub add { Line 74  sub add {
74                  my $v = $i->$c;                  my $v = $i->$c;
75    
76                  if ( ref($v) ne '' ) {                  if ( ref($v) ne '' ) {
77                          if ($i->$c->can('id')) {  
78                                  $v = $i->$c->id;                          foreach my $f_c ( qw/id name title/ ) {
79                                  warn "  # $c = $v [id]\n" if ($debug);                                  if ( $i->$c->can( $f_c ) ) {
80                                  $doc->add(Lucene::Document::Field->Keyword( $c, $v ));                                          my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
81                          } elsif ($v->isa('Jifty::DateTime')) {                                          my $col = $c . '_' . $f_c;
82                                            if ( $f_v ) {
83                                                    warn "  # $col = $f_v\n" if ($debug);
84                                                    $doc->add(Lucene::Document::Field->Text( $col, $f_v ));
85                                            } else {
86                                                    warn "  . $col is NULL\n" if ($debug);
87                                            }
88                                    }
89                            }
90    
91                            if ($v->isa('Jifty::DateTime')) {
92                                  warn "  d $c = $v\n" if ($debug);                                  warn "  d $c = $v\n" if ($debug);
93                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));
94                          } else {                          } else {
# Line 90  sub add { Line 106  sub add {
106                          warn "  * $c = $v\n" if ($debug);                          warn "  * $c = $v\n" if ($debug);
107                  } else {                  } else {
108                          $doc->add(Lucene::Document::Field->Text( $c, $v ));                          $doc->add(Lucene::Document::Field->Text( $c, $v ));
109                          warn "  + $c = $v\n" if ($debug);                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
110                  }                  }
111          }          }
112    
113            # add _owner_id to speed up filtering of search results
114            $uid ||= Jifty->web->current_user->id;
115            $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          Jifty->log->debug("added ", $i->id, " 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 110  sub collection { Line 135  sub collection {
135    
136          my $q = shift or die "no q?";          my $q = shift or die "no q?";
137    
138            return if ( $self->create );
139    
140          my $searcher = new Lucene::Search::IndexSearcher($self->store);          my $searcher = new Lucene::Search::IndexSearcher($self->store);
141            $self->log->debug("$searcher created");
142          my $parser = new Lucene::QueryParser("content", $self->analyzer);          my $parser = new Lucene::QueryParser("content", $self->analyzer);
143          my $query = $parser->parse( $q );          $self->log->debug("$parser created");
144    
145          Jifty->log->debug("searching for '$q'");          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
146    
147            my $query = $parser->parse( $full_q );
148    
149            $self->log->debug("searching for '$q' using ", $query->toString);
150    
151          my $hits = $searcher->search($query);          my $hits = $searcher->search($query);
152          my $num_hits = $hits->length();          my $num_hits = $hits->length();
153    
154          Jifty->log->debug("found $num_hits results");          $self->log->debug("found $num_hits results");
155    
156          my $collection = Grep::Model::ItemCollection->new();          my $collection = Grep::Model::ItemCollection->new();
157    
# Line 133  sub collection { Line 165  sub collection {
165                  my $title = $doc->get("title");                  my $title = $doc->get("title");
166                  my $id = $doc->get("id");                  my $id = $doc->get("id");
167    
168                  warn "## $i $score $title\n";                  $self->log->debug("result $i $score $title");
169    
170                  my $item = Grep::Model::Item->new();                  my $item = Grep::Model::Item->new();
171                  my ($ok,$msg) = $item->load_by_cols( id => $id );                  my ($ok,$msg) = $item->load_by_cols( id => $id );
# Line 149  sub collection { Line 181  sub collection {
181          undef $hits;          undef $hits;
182          undef $query;          undef $query;
183          undef $parser;          undef $parser;
184            $searcher->close;
185          undef $searcher;          undef $searcher;
186    
187          return $collection;          return $collection;
# Line 156  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                  warn "closing index\n";                  $self->log->debug("closing index");
200                  $writer->close;                  $self->writer->close;
201          }          }
202          undef $writer;  
203            $self->writer( undef );
204            $self->store( undef );
205            $self->create( undef );
206            $self->analyzer( undef );
207    
208            $self->log->debug("finish");
209    
210            return;
211  }  }
212    
213    =for TODO
214    
215  sub _signal {  sub _signal {
216          my $s = shift;          my $s = shift;
217          warn "catched SIG $s\n";          warn "catched SIG $s\n";
# Line 180  $SIG{'__DIE__'} = \&_signal; Line 223  $SIG{'__DIE__'} = \&_signal;
223  $SIG{'INT'} = \&_signal;  $SIG{'INT'} = \&_signal;
224  $SIG{'QUIT'} = \&_signal;  $SIG{'QUIT'} = \&_signal;
225    
226    =cut
227    
228    =head2 snippet
229    
230      my $short = $self->snippet( 50, $text );
231    
232    =cut
233    
234    sub snippet {
235            my $self = shift;
236    
237            my $len = shift or die "no len?";
238            my $m = join(" ", @_);
239    
240            $m =~ s/\s+/ /gs;
241    
242            if (length($m) > $len) {
243                    return substr($m,0,$len) . '...';
244            } else {
245                    return $m;
246            }
247    }
248    
249  1;  1;

Legend:
Removed from v.49  
changed lines
  Added in v.110

  ViewVC Help
Powered by ViewVC 1.1.26