/[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 109 by dpavlin, Wed Mar 14 18:46:37 2007 UTC revision 136 by dpavlin, Thu May 3 15:39:52 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 );  use base qw( Class::Accessor );
6  Grep::Search->mk_accessors( qw( analyzer store writer create index_path ) );  Grep::Search->mk_accessors( qw( invindexer create index_path ) );
7    
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
9  use Lucene;  use KinoSearch::InvIndexer;
10    use KinoSearch::Searcher;
11  use Jifty::Util;  use Jifty::Util;
12    
13  my $debug = 0;  my $debug = 0;
14    
15  =head1 NAME  =head1 NAME
16    
17  Grep::Search - full text search  Grep::Search - full text search using L<KinoSearch>
18    
19  =head1 METHODS  =head1 METHODS
20    
# Line 23  Grep::Search - full text search Line 24  Grep::Search - full text search
24    
25  =cut  =cut
26    
27    sub log { Jifty->web->log }
28    
29  sub new {  sub new {
30          my $class = shift;                  my $class = shift;        
31          my $self = $class->SUPER::new(@_);          my $self = $class->SUPER::new(@_);
32    
33          my $index_path = Jifty::Util->app_root . '/var/lucene';          my $index_path = Jifty::Util->app_root . '/var/invindex';
34    
35          $self->index_path( $index_path );          $self->index_path( $index_path );
36    
37          if (! -e "$index_path/segments") {          if (! -e "$index_path") {
                 $self->create( 1 );  
38                  $self->log->debug("Creating new index $index_path");                  $self->log->debug("Creating new index $index_path");
39                    $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) );
40          } else {          } else {
                 $self->create( 0 );  
41                  $self->log->debug("Opening index: $index_path");                  $self->log->debug("Opening index: $index_path");
42                    $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) );
43          }          }
44    
         $self->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() );  
         $self->log->debug($self->analyzer . " created");  
   
         $self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) );  
         $self->log->debug($self->store, " created");  
   
45          return $self;          return $self;
46  }  }
47    
   
48  =head2 add  =head2 add
49    
50    $search->add( $record, $owner_id );    $search->add( $record, $owner_id );
# Line 65  sub add { Line 61  sub add {
61    
62          my $pk = { $i->primary_keys };          my $pk = { $i->primary_keys };
63    
64          my $doc = new Lucene::Document;          my $doc;
65    
66          my @columns = map { $_->name } $i->columns;          my @columns = map { $_->name } $i->columns;
67    
# Line 81  sub add { Line 77  sub add {
77                                          my $col = $c . '_' . $f_c;                                          my $col = $c . '_' . $f_c;
78                                          if ( $f_v ) {                                          if ( $f_v ) {
79                                                  warn "  # $col = $f_v\n" if ($debug);                                                  warn "  # $col = $f_v\n" if ($debug);
80                                                  $doc->add(Lucene::Document::Field->Text( $col, $f_v ));                                                  $doc->{ $col } = $f_v;
81                                          } else {                                          } else {
82                                                  warn "  . $col is NULL\n" if ($debug);                                                  warn "  . $col is NULL\n" if ($debug);
83                                          }                                          }
# Line 90  sub add { Line 86  sub add {
86    
87                          if ($v->isa('Jifty::DateTime')) {                          if ($v->isa('Jifty::DateTime')) {
88                                  warn "  d $c = $v\n" if ($debug);                                  warn "  d $c = $v\n" if ($debug);
89                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));                                  $doc->{$c} = $v;
90                          } else {                          } else {
91                                  warn "  s $c = $v [",ref($v),"]\n" if ($debug);                                  warn "  s $c = $v [",ref($v),"]\n" if ($debug);
92                          }                          }
# Line 102  sub add { Line 98  sub add {
98                  $v =~ s/<[^>]+>/ /gs;                  $v =~ s/<[^>]+>/ /gs;
99    
100                  if ( defined( $pk->{$c} ) ) {                  if ( defined( $pk->{$c} ) ) {
101                          $doc->add(Lucene::Document::Field->Keyword( $c, $v ));                          $doc->{ $c } = $v;
102                          warn "  * $c = $v\n" if ($debug);                          warn "  * $c = $v\n" if ($debug);
103                  } else {                  } else {
104                          $doc->add(Lucene::Document::Field->Text( $c, $v ));                          $doc->{ $c } = $v;
105                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
106                  }                  }
107          }          }
108    
109          # add _owner_id to speed up filtering of search results          # add _owner_id to speed up filtering of search results
110          $uid ||= Jifty->web->current_user->id;          $uid ||= Jifty->web->current_user->id;
111          $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));          $doc->{ '_owner_id' } = $uid;
112    
113          if (! defined( $self->writer )) {          $self->invindexer->add_doc( $doc );
                 $self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) );  
                 $self->log->debug($self->writer, " created");  
         }  
   
         $self->writer->addDocument($doc);  
114    
115          $self->log->debug("added ", $i->id, " for user $uid to index");          $self->log->debug("added ", $i->id, " for user $uid to index");
116  }  }
# Line 135  sub collection { Line 126  sub collection {
126    
127          my $q = shift or die "no q?";          my $q = shift or die "no q?";
128    
129          return if ( $self->create );          my $searcher = KinoSearch::Searcher->new(
130                    invindex => Grep::Search::Schema->open( $self->index_path ), );
         my $searcher = new Lucene::Search::IndexSearcher($self->store);  
131          $self->log->debug("$searcher created");          $self->log->debug("$searcher created");
         my $parser = new Lucene::QueryParser("content", $self->analyzer);  
         $self->log->debug("$parser created");  
132    
133          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
134    
135          my $query = $parser->parse( $full_q );          $self->log->debug("searching for '$q' using $full_q");
136            my $hits = $searcher->search(
137          $self->log->debug("searching for '$q' using ", $query->toString);                  query           => $full_q,
138    #               offset          => $offset,
139    #               num_wanted      => $hits_per_page,
140            );
141    
142          my $hits = $searcher->search($query);          my $num_hits = $hits->total_hits;
         my $num_hits = $hits->length();  
143    
144          $self->log->debug("found $num_hits results");          $self->log->debug("found $num_hits results");
145    
# Line 157  sub collection { Line 147  sub collection {
147    
148          my @results;          my @results;
149    
150          for ( my $i = 0; $i < $num_hits; $i++ ) {          my $i = 0;
151            while ( my $hit = $hits->fetch_hit_hashref ) {
152    
153                  my $doc = $hits->doc( $i );                  my $score = $hit->{score};
154                    my $title = $hit->{title};
155                  my $score = $hits->score($i);                  my $id = $hit->{id};
                 my $title = $doc->get("title");  
                 my $id = $doc->get("id");  
156    
157                  $self->log->debug("result $i $score $title");                  $self->log->debug("result $i $score $title");
158    
# Line 178  sub collection { Line 167  sub collection {
167    
168          }          }
169    
170          undef $hits;          $self->log->debug("finished search");
         undef $query;  
         undef $parser;  
         $searcher->close;  
         undef $searcher;  
171    
172          return $collection;          return $collection;
173  }  }
# Line 195  sub collection { Line 180  sub collection {
180    
181  sub finish {  sub finish {
182          my $self = shift;          my $self = shift;
183          if ($self->writer) {          if ($self->invindexer) {
184                  $self->log->debug("closing index");                  $self->log->debug("closing index");
185                  $self->writer->close;                  $self->invindexer->finish;
186          }          }
187    
188  #       $self->writer( undef );          $self->log->debug("finish");
 #       $self->store( undef );  
 #       $self->create( undef );  
 #       $self->analyzer( undef );  
   
         return;  
 }  
189    
190  =for TODO          undef $self;
191    
192  sub _signal {          return;
         my $s = shift;  
         warn "catched SIG $s\n";  
         finish();  
         exit(0);  
193  }  }
194    
 $SIG{'__DIE__'} = \&_signal;  
 $SIG{'INT'} = \&_signal;  
 $SIG{'QUIT'} = \&_signal;  
   
 =cut  
   
195  =head2 snippet  =head2 snippet
196    
197    my $short = $self->snippet( 50, $text );    my $short = $self->snippet( 50, $text );
# Line 244  sub snippet { Line 213  sub snippet {
213          }          }
214  }  }
215    
216    package Grep::Search::KeywordField;
217    use base qw( KinoSearch::Schema::FieldSpec );
218    sub analyzed { 0 }
219    
220    package Grep::Search::Schema;
221    
222    =head1 NAME
223    
224    Grep::Search::Schema - schema definition for full-text search
225    
226    =cut
227    
228    use base 'KinoSearch::Schema';
229    use KinoSearch::Analysis::PolyAnalyzer;
230    
231    our %fields = (
232            id                              => 'Grep::Search::KeywordField',
233    
234            in_feed_id              => 'Grep::Search::KeywordField',
235            in_feed_url             => 'Grep::Search::KeywordField',
236            in_feed_title   => 'KinoSearch::Schema::FieldSpec',
237            in_feed_owner   => 'Grep::Search::KeywordField',
238            in_feed_created_on      => 'Grep::Search::KeywordField',
239    
240            title                   => 'KinoSearch::Schema::FieldSpec',
241            link                    => 'Grep::Search::KeywordField',
242            content                 => 'KinoSearch::Schema::FieldSpec',
243            summary                 => 'KinoSearch::Schema::FieldSpec',
244            category                => 'KinoSearch::Schema::FieldSpec',
245            author                  => 'KinoSearch::Schema::FieldSpec',
246            issued                  => 'Grep::Search::KeywordField',
247            modified                => 'Grep::Search::KeywordField',
248    
249            _owner_id               => 'Grep::Search::KeywordField',
250    );
251    
252    sub analyzer {
253            return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' );
254    }
255    
256  1;  1;

Legend:
Removed from v.109  
changed lines
  Added in v.136

  ViewVC Help
Powered by ViewVC 1.1.26