/[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 112 by dpavlin, Wed Mar 14 21:10:53 2007 UTC revision 180 by dpavlin, Fri Feb 1 20:22:14 2008 UTC
# Line 3  package Grep::Search; Line 3  package Grep::Search;
3  use strict;  use strict;
4  use warnings;  use warnings;
5  use base qw( Class::Accessor );  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 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 $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 21  Grep::Search - full text search Line 22  Grep::Search - full text search
22    
23    my $search = Grep::Search->new();    my $search = Grep::Search->new();
24    
25      my $search = Grep::Search->new( create => 1 );
26    
27    =head2 invindexer
28    
29    Accessor to call any method defined on L<KinoSearch::InvIndexer>
30    
31      $search->invindexer->delete_by_term( 'id', 42 );
32    
33  =cut  =cut
34    
35  sub log { Jifty->web->log }  sub log { Jifty->web->log }
# Line 29  sub new { Line 38  sub new {
38          my $class = shift;                  my $class = shift;        
39          my $self = $class->SUPER::new(@_);          my $self = $class->SUPER::new(@_);
40    
41          my $index_path = Jifty::Util->app_root . '/var/lucene';          my $index_path = Jifty::Util->app_root . '/var/invindex';
42    
43          $self->index_path( $index_path );          $self->index_path( $index_path );
44    
45          if (! -e "$index_path/segments") {          if ( ! -e "$index_path" || $self->create ) {
                 $self->create( 1 );  
46                  $self->log->debug("Creating new index $index_path");                  $self->log->debug("Creating new index $index_path");
47                    $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) );
48          } else {          } else {
                 $self->create( 0 );  
49                  $self->log->debug("Opening index: $index_path");                  $self->log->debug("Opening index: $index_path");
50                    $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) );
51          }          }
52    
         $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");  
   
53          return $self;          return $self;
54  }  }
55    
# Line 66  sub add { Line 69  sub add {
69    
70          my $pk = { $i->primary_keys };          my $pk = { $i->primary_keys };
71    
72          my $doc = new Lucene::Document;          my $doc;
73    
74          my @columns = map { $_->name } $i->columns;          my @columns = map { $_->name } $i->columns;
75    
# Line 82  sub add { Line 85  sub add {
85                                          my $col = $c . '_' . $f_c;                                          my $col = $c . '_' . $f_c;
86                                          if ( $f_v ) {                                          if ( $f_v ) {
87                                                  warn "  # $col = $f_v\n" if ($debug);                                                  warn "  # $col = $f_v\n" if ($debug);
88                                                  $doc->add(Lucene::Document::Field->Text( $col, $f_v ));                                                  $doc->{ $col } = $f_v;
89                                          } else {                                          } else {
90                                                  warn "  . $col is NULL\n" if ($debug);                                                  warn "  . $col is NULL\n" if ($debug);
91                                          }                                          }
# Line 91  sub add { Line 94  sub add {
94    
95                          if ($v->isa('Jifty::DateTime')) {                          if ($v->isa('Jifty::DateTime')) {
96                                  warn "  d $c = $v\n" if ($debug);                                  warn "  d $c = $v\n" if ($debug);
97                                  $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));                                  $doc->{$c} = $v;
98                          } else {                          } else {
99                                  warn "  s $c = $v [",ref($v),"]\n" if ($debug);                                  warn "  s $c = $v [",ref($v),"]\n" if ($debug);
100                          }                          }
# Line 100  sub add { Line 103  sub add {
103    
104                  next if (! defined($v) || $v eq '');                  next if (! defined($v) || $v eq '');
105    
106                  $v =~ s/<[^>]+>/ /gs;                  eval { $v =~ s/<[^>]+>/ /gs; };
107                    if ($@) {
108                            Jifty->log->error("can't strip html from $c in item ", $i->id);
109                            next;
110                    }
111    
112                  if ( defined( $pk->{$c} ) ) {                  if ( defined( $pk->{$c} ) ) {
113                          $doc->add(Lucene::Document::Field->Keyword( $c, $v ));                          $doc->{ $c } = $v;
114                          warn "  * $c = $v\n" if ($debug);                          warn "  * $c = $v\n" if ($debug);
115                  } else {                  } else {
116                          $doc->add(Lucene::Document::Field->Text( $c, $v ));                          $doc->{ $c } = $v;
117                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);                          warn "  + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
118                  }                  }
119          }          }
120    
121          # add _owner_id to speed up filtering of search results          # add _owner_id to speed up filtering of search results
122          $uid ||= Jifty->web->current_user->id;          $uid ||= Jifty->web->current_user->id;
123          $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));          $doc->{ '_owner_id' } = $uid;
   
         if (! defined( $self->writer )) {  
                 $self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) );  
                 $self->log->debug($self->writer, " created");  
         }  
124    
125          $self->writer->addDocument($doc);          $self->invindexer->add_doc( $doc );
126    
127          $self->log->debug("added ", $i->id, " for user $uid to index");          $self->log->debug("added ", $i->id, " for user $uid to index");
128    
129            return 1;
130  }  }
131    
132  =head2 collection  =head2 collection
133    
134    Return C<Grep::Model::ItemCollection> which is result of C<search query>
135    
136    my $ItemCollection = $search->collection( 'search query' );    my $ItemCollection = $search->collection( 'search query' );
137    
138    =head2 hits
139    
140    Return number of results from last C<collection> call
141    
142      my $num_results = $search->hits;
143    
144  =cut  =cut
145    
146  sub collection {  sub collection {
# Line 136  sub collection { Line 148  sub collection {
148    
149          my $q = shift or die "no q?";          my $q = shift or die "no q?";
150    
151          return if ( $self->create );          my $searcher = KinoSearch::Searcher->new(
152                    invindex => Grep::Search::Schema->open( $self->index_path ), );
         my $searcher = new Lucene::Search::IndexSearcher($self->store);  
153          $self->log->debug("$searcher created");          $self->log->debug("$searcher created");
         my $parser = new Lucene::QueryParser("content", $self->analyzer);  
         $self->log->debug("$parser created");  
154    
155          my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;          my $full_q = "($q)";
156    
157            my $uid = Jifty->web->current_user->id;
158            $full_q .= ' AND _owner_id:' . $uid if (defined $uid);
159    
160          my $query = $parser->parse( $full_q );          $self->log->debug("searching for '$q' using $full_q");
161    
162          $self->log->debug("searching for '$q' using ", $query->toString);          my $query_parser = KinoSearch::QueryParser->new(
163                    schema => Grep::Search::Schema->new,
164                    fields => [ qw/ title link content summary category author / ],
165            );
166            $query_parser->set_heed_colons(1);       # enable field:value AND/OR/NOT syntax
167            my $query = $query_parser->parse( $full_q );
168            my $hits = $searcher->search(
169                    query           => $query,
170    #               offset          => $offset,
171    #               num_wanted      => $hits_per_page,
172            );
173    
174          my $hits = $searcher->search($query);          $self->hits( $hits->total_hits );
         my $num_hits = $hits->length();  
175    
176          $self->log->debug("found $num_hits results");          $self->log->debug("found ", $self->hits, " results");
177    
178          my $collection = Grep::Model::ItemCollection->new();          my $collection = Grep::Model::ItemCollection->new();
179    
180          my @results;          my @results;
181    
182          for ( my $i = 0; $i < $num_hits; $i++ ) {          my $i = 0;
183            while ( my $hit = $hits->fetch_hit ) {
184    
185                  my $doc = $hits->doc( $i );                  my $score = $hit->{score};
186                    my $title = $hit->{title};
187                    my $id = $hit->{id};
188    
189                  my $score = $hits->score($i);                  $self->log->debug("result $i [$id] $title $score");
                 my $title = $doc->get("title");  
                 my $id = $doc->get("id");  
   
                 $self->log->debug("result $i $score $title");  
190    
191                  my $item = Grep::Model::Item->new();                  my $item = Grep::Model::Item->new();
192                  my ($ok,$msg) = $item->load_by_cols( id => $id );                  my ($ok,$msg) = $item->load_by_cols( id => $id );
# Line 179  sub collection { Line 199  sub collection {
199    
200          }          }
201    
202          undef $hits;          $self->log->debug("finished search");
         undef $query;  
         undef $parser;  
         $searcher->close;  
         undef $searcher;  
203    
204          return $collection;          return $collection;
205  }  }
# Line 196  sub collection { Line 212  sub collection {
212    
213  sub finish {  sub finish {
214          my $self = shift;          my $self = shift;
215          if ($self->writer) {          if ($self->invindexer) {
216                  $self->log->debug("closing index");                  $self->log->debug("closing index");
217                  $self->writer->close;                  $self->invindexer->finish;
218          }          }
219    
220          $self->log->debug("finish");          $self->log->debug("finish");
221    
222          undef $self;          undef $self;
223    
224          return;          return 1;
225  }  }
226    
 =for TODO  
   
 sub _signal {  
         my $s = shift;  
         warn "catched SIG $s\n";  
         finish();  
         exit(0);  
 }  
   
 $SIG{'__DIE__'} = \&_signal;  
 $SIG{'INT'} = \&_signal;  
 $SIG{'QUIT'} = \&_signal;  
   
 =cut  
   
227  =head2 snippet  =head2 snippet
228    
229    my $short = $self->snippet( 50, $text );    my $short = $self->snippet( 50, $text );
# Line 244  sub snippet { Line 245  sub snippet {
245          }          }
246  }  }
247    
248    package Grep::Search::KeywordField;
249    use base qw( KinoSearch::Schema::FieldSpec );
250    sub analyzed { 0 }
251    #sub indexed { 1 }
252    #sub stored { 1 }
253    sub vectorized { 0 }
254    
255    package Grep::Search::Schema;
256    
257    =head1 NAME
258    
259    Grep::Search::Schema - schema definition for full-text search
260    
261    =cut
262    
263    use base 'KinoSearch::Schema';
264    use KinoSearch::Analysis::PolyAnalyzer;
265    
266    our %fields = (
267            id                              => 'Grep::Search::KeywordField',
268    
269            in_feed_id              => 'Grep::Search::KeywordField',
270            in_feed_url             => 'Grep::Search::KeywordField',
271            in_feed_title   => 'KinoSearch::Schema::FieldSpec',
272            in_feed_owner   => 'Grep::Search::KeywordField',
273            in_feed_created_on      => 'Grep::Search::KeywordField',
274    
275            title                   => 'KinoSearch::Schema::FieldSpec',
276            link                    => 'Grep::Search::KeywordField',
277            content                 => 'KinoSearch::Schema::FieldSpec',
278            summary                 => 'KinoSearch::Schema::FieldSpec',
279            category                => 'KinoSearch::Schema::FieldSpec',
280            author                  => 'KinoSearch::Schema::FieldSpec',
281            created_on              => 'Grep::Search::KeywordField',
282            last_update             => 'Grep::Search::KeywordField',
283    
284            _owner_id               => 'Grep::Search::KeywordField',
285    );
286    
287    sub analyzer {
288            return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' );
289    }
290    
291  1;  1;

Legend:
Removed from v.112  
changed lines
  Added in v.180

  ViewVC Help
Powered by ViewVC 1.1.26