11 |
|
|
12 |
my ( $analyzer, $store, $writer ); |
my ( $analyzer, $store, $writer ); |
13 |
|
|
14 |
my $debug = 0; |
my $debug = 1; |
15 |
|
|
16 |
sub reopen_index { |
sub create { |
|
my $self = shift; |
|
17 |
|
|
|
$analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); |
|
18 |
my $create = 0; |
my $create = 0; |
19 |
if (! -e "$index_path/segments") { |
if (! -e "$index_path/segments") { |
20 |
$create = 1; |
$create = 1; |
21 |
Jifty->log->debug("creating index $index_path") unless ($store); |
Jifty->log->debug("create index $index_path") unless ($store); |
22 |
} else { |
} else { |
23 |
Jifty->log->debug("using index: $index_path") unless ($store); |
Jifty->log->debug("open index: $index_path") unless ($store); |
24 |
} |
} |
25 |
$store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $create ); |
return $create; |
26 |
|
} |
27 |
|
|
28 |
|
sub analyzer { |
29 |
|
my $self = shift; |
30 |
|
$analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer(); |
31 |
|
return $analyzer; |
32 |
|
} |
33 |
|
|
34 |
|
sub store { |
35 |
|
my $self = shift; |
36 |
|
|
37 |
|
$store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ); |
38 |
|
return $store; |
39 |
|
} |
40 |
|
|
41 |
|
sub writer { |
42 |
|
my $self = shift; |
43 |
|
$writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ); |
44 |
|
return $writer; |
45 |
} |
} |
46 |
|
|
47 |
=head2 add |
=head2 add |
57 |
|
|
58 |
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')); |
59 |
|
|
|
$self->reopen_index; |
|
|
|
|
|
$writer ||= new Lucene::Index::IndexWriter( $store, $analyzer, 0 ); |
|
|
|
|
60 |
my $pk = { $i->primary_keys }; |
my $pk = { $i->primary_keys }; |
61 |
|
|
62 |
my $doc = new Lucene::Document; |
my $doc = new Lucene::Document; |
68 |
my $v = $i->$c; |
my $v = $i->$c; |
69 |
|
|
70 |
if ( ref($v) ne '' ) { |
if ( ref($v) ne '' ) { |
71 |
if ($i->$c->can('id')) { |
|
72 |
$v = $i->$c->id; |
foreach my $f_c ( qw/id name title/ ) { |
73 |
warn " # $c = $v [id]\n" if ($debug); |
if ( $i->$c->can( $f_c ) ) { |
74 |
$doc->add(Lucene::Document::Field->Keyword( $c, $v )); |
my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c }; |
75 |
} elsif ($v->isa('Jifty::DateTime')) { |
my $col = $c . '_' . $f_c; |
76 |
|
if ( $f_v ) { |
77 |
|
warn " # $col = $f_v\n" if ($debug); |
78 |
|
$doc->add(Lucene::Document::Field->Text( $col, $f_v )); |
79 |
|
} else { |
80 |
|
warn " . $col is NULL\n" if ($debug); |
81 |
|
} |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
if ($v->isa('Jifty::DateTime')) { |
86 |
warn " d $c = $v\n" if ($debug); |
warn " d $c = $v\n" if ($debug); |
87 |
$doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); |
$doc->add(Lucene::Document::Field->Keyword( $c, "$v" )); |
88 |
} else { |
} else { |
100 |
warn " * $c = $v\n" if ($debug); |
warn " * $c = $v\n" if ($debug); |
101 |
} else { |
} else { |
102 |
$doc->add(Lucene::Document::Field->Text( $c, $v )); |
$doc->add(Lucene::Document::Field->Text( $c, $v )); |
103 |
warn " + $c = $v\n" if ($debug); |
warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug); |
104 |
} |
} |
105 |
} |
} |
106 |
|
|
107 |
$writer->addDocument($doc); |
$self->writer->addDocument($doc); |
108 |
|
|
109 |
Jifty->log->debug("added ", $i->id, " to index"); |
Jifty->log->debug("added ", $i->id, " to index"); |
110 |
} |
} |
120 |
|
|
121 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
122 |
|
|
123 |
$self->reopen_index; |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
124 |
|
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
|
my $searcher = new Lucene::Search::IndexSearcher($store); |
|
|
my $parser = new Lucene::QueryParser("content", $analyzer); |
|
125 |
my $query = $parser->parse( $q ); |
my $query = $parser->parse( $q ); |
126 |
|
|
127 |
Jifty->log->debug("searching for '$q'"); |
Jifty->log->debug("searching for '$q'"); |
179 |
undef $writer; |
undef $writer; |
180 |
} |
} |
181 |
|
|
182 |
|
=for TODO |
183 |
|
|
184 |
sub _signal { |
sub _signal { |
185 |
my $s = shift; |
my $s = shift; |
186 |
warn "catched SIG $s\n"; |
warn "catched SIG $s\n"; |
192 |
$SIG{'INT'} = \&_signal; |
$SIG{'INT'} = \&_signal; |
193 |
$SIG{'QUIT'} = \&_signal; |
$SIG{'QUIT'} = \&_signal; |
194 |
|
|
195 |
|
=cut |
196 |
|
|
197 |
|
=head2 snippet |
198 |
|
|
199 |
|
my $short = $self->snippet( 50, $text ); |
200 |
|
|
201 |
|
|
202 |
|
=cut |
203 |
|
|
204 |
|
sub snippet { |
205 |
|
my $self = shift; |
206 |
|
|
207 |
|
my $len = shift or die "no len?"; |
208 |
|
my $m = join(" ", @_); |
209 |
|
|
210 |
|
$m =~ s/\s+/ /gs; |
211 |
|
|
212 |
|
if (length($m) > $len) { |
213 |
|
return substr($m,0,$len) . '...'; |
214 |
|
} else { |
215 |
|
return $m; |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
1; |
1; |