/[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 76 by dpavlin, Fri Feb 23 17:17:50 2007 UTC revision 141 by dpavlin, Tue May 8 12:37:28 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 );
6    Grep::Search->mk_accessors( qw( invindexer create index_path hits ) );
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 $index_path = Jifty::Util->app_root . '/var/lucene';  my $debug = 0;
14    
15  my ( $analyzer, $store, $writer );  =head1 NAME
16    
17  my $debug = 1;  Grep::Search - full text search using L<KinoSearch>
 my $create;  
18    
19  sub create {  =head1 METHODS
20    
21          if (defined( $create )) {  =head2 new
                 Jifty->log->debug("using previous create $create");  
                 return $create;  
         }  
22    
23          if (! -e "$index_path/segments") {    my $search = Grep::Search->new();
                 $create = 1;  
                 Jifty->log->debug("create index $index_path");  
         } else {  
                 $create = 0;  
                 Jifty->log->debug("open index: $index_path");  
         }  
         return $create;  
 }  
24    
25  sub analyzer {  =cut
         my $self = shift;  
         $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();  
         return $analyzer;  
 }  
26    
27  sub store {  sub log { Jifty->web->log }
         my $self = shift;  
28    
29          $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );  sub new {
30          return $store;          my $class = shift;        
31  }          my $self = $class->SUPER::new(@_);
32    
33  sub writer {          my $index_path = Jifty::Util->app_root . '/var/invindex';
34          my $self = shift;  
35          $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );          $self->index_path( $index_path );
36          return $writer;  
37            if (! -e "$index_path") {
38                    $self->log->debug("Creating new index $index_path");
39                    $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) );
40            } else {
41                    $self->log->debug("Opening index: $index_path");
42                    $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) );
43            }
44    
45            return $self;
46  }  }
47    
48  =head2 add  =head2 add
49    
50    Grep::Search->add( $record, $owner_id );    $search->add( $record, $owner_id );
51    
52  =cut  =cut
53    
# Line 66  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 82  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 91  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 103  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          $self->writer->addDocument($doc);          $self->invindexer->add_doc( $doc );
114    
115          Jifty->log->debug("added ", $i->id, " for user $uid to index");          $self->log->debug("added ", $i->id, " for user $uid to index");
116  }  }
117    
118  =head2  =head2 collection
119    
120    Return C<Grep::Model::ItemCollection> which is result of C<search query>
121    
122    my $ItemCollection = Grep::Search->collection( 'search query' );    my $ItemCollection = $search->collection( 'search query' );
123    
124    =head2 hits
125    
126    Return number of results from last C<collection> call
127    
128      my $num_results = $search->hits;
129    
130  =cut  =cut
131    
# Line 131  sub collection { Line 134  sub collection {
134    
135          my $q = shift or die "no q?";          my $q = shift or die "no q?";
136    
137          return if ( $self->create );          my $searcher = KinoSearch::Searcher->new(
138                    invindex => Grep::Search::Schema->open( $self->index_path ), );
139          my $searcher = new Lucene::Search::IndexSearcher($self->store);          $self->log->debug("$searcher created");
         my $parser = new Lucene::QueryParser("content", $self->analyzer);  
140    
141          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
142    
143          my $query = $parser->parse( $full_q );          $self->log->debug("searching for '$q' using $full_q");
144            my $hits = $searcher->search(
145          Jifty->log->debug("searching for '$q' using ", $query->toString);                  query           => $full_q,
146    #               offset          => $offset,
147    #               num_wanted      => $hits_per_page,
148            );
149    
150          my $hits = $searcher->search($query);          $self->hits( $hits->total_hits );
         my $num_hits = $hits->length();  
151    
152          Jifty->log->debug("found $num_hits results");          $self->log->debug("found ", $self->hits, " results");
153    
154          my $collection = Grep::Model::ItemCollection->new();          my $collection = Grep::Model::ItemCollection->new();
155    
156          my @results;          my @results;
157    
158          for ( my $i = 0; $i < $num_hits; $i++ ) {          my $i = 0;
159            while ( my $hit = $hits->fetch_hit_hashref ) {
160    
161                  my $doc = $hits->doc( $i );                  my $score = $hit->{score};
162                    my $title = $hit->{title};
163                    my $id = $hit->{id};
164    
165                  my $score = $hits->score($i);                  $self->log->debug("result $i $score $title");
                 my $title = $doc->get("title");  
                 my $id = $doc->get("id");  
   
                 warn "## $i $score $title\n";  
166    
167                  my $item = Grep::Model::Item->new();                  my $item = Grep::Model::Item->new();
168                  my ($ok,$msg) = $item->load_by_cols( id => $id );                  my ($ok,$msg) = $item->load_by_cols( id => $id );
# Line 172  sub collection { Line 175  sub collection {
175    
176          }          }
177    
178          undef $hits;          $self->log->debug("finished search");
         undef $query;  
         undef $parser;  
         $searcher->close;  
         undef $searcher;  
179    
180          return $collection;          return $collection;
181  }  }
182    
183  =head2 finish  =head2 finish
184    
185    Grep::Search->finish    $search->finish
186    
187  =cut  =cut
188    
189  sub finish {  sub finish {
190          my $self = shift;          my $self = shift;
191          if ($writer) {          if ($self->invindexer) {
192                  warn "closing index\n";                  $self->log->debug("closing index");
193                  $writer->close;                  $self->invindexer->finish;
                 undef $writer;  
194          }          }
         undef $store;  
         undef $create;  
195    
196          return;          $self->log->debug("finish");
 }  
197    
198  =for TODO          undef $self;
199    
200  sub _signal {          return;
         my $s = shift;  
         warn "catched SIG $s\n";  
         finish();  
         exit(0);  
201  }  }
202    
 $SIG{'__DIE__'} = \&_signal;  
 $SIG{'INT'} = \&_signal;  
 $SIG{'QUIT'} = \&_signal;  
   
 =cut  
   
203  =head2 snippet  =head2 snippet
204    
205    my $short = $self->snippet( 50, $text );    my $short = $self->snippet( 50, $text );
206    
   
207  =cut  =cut
208    
209  sub snippet {  sub snippet {
# Line 237  sub snippet { Line 221  sub snippet {
221          }          }
222  }  }
223    
224    package Grep::Search::KeywordField;
225    use base qw( KinoSearch::Schema::FieldSpec );
226    sub analyzed { 0 }
227    
228    package Grep::Search::Schema;
229    
230    =head1 NAME
231    
232    Grep::Search::Schema - schema definition for full-text search
233    
234    =cut
235    
236    use base 'KinoSearch::Schema';
237    use KinoSearch::Analysis::PolyAnalyzer;
238    
239    our %fields = (
240            id                              => 'Grep::Search::KeywordField',
241    
242            in_feed_id              => 'Grep::Search::KeywordField',
243            in_feed_url             => 'Grep::Search::KeywordField',
244            in_feed_title   => 'KinoSearch::Schema::FieldSpec',
245            in_feed_owner   => 'Grep::Search::KeywordField',
246            in_feed_created_on      => 'Grep::Search::KeywordField',
247    
248            title                   => 'KinoSearch::Schema::FieldSpec',
249            link                    => 'Grep::Search::KeywordField',
250            content                 => 'KinoSearch::Schema::FieldSpec',
251            summary                 => 'KinoSearch::Schema::FieldSpec',
252            category                => 'KinoSearch::Schema::FieldSpec',
253            author                  => 'KinoSearch::Schema::FieldSpec',
254            issued                  => 'Grep::Search::KeywordField',
255            modified                => 'Grep::Search::KeywordField',
256    
257            _owner_id               => 'Grep::Search::KeywordField',
258    );
259    
260    sub analyzer {
261            return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' );
262    }
263    
264  1;  1;

Legend:
Removed from v.76  
changed lines
  Added in v.141

  ViewVC Help
Powered by ViewVC 1.1.26