11 |
|
|
12 |
my ( $analyzer, $store, $writer ); |
my ( $analyzer, $store, $writer ); |
13 |
|
|
14 |
my $debug = 0; |
my $debug = 1; |
15 |
|
my $create; |
16 |
|
|
17 |
sub create { |
sub create { |
18 |
|
|
19 |
my $create = 0; |
if (defined( $create )) { |
20 |
|
Jifty->log->debug("using previous create $create"); |
21 |
|
return $create; |
22 |
|
} |
23 |
|
|
24 |
if (! -e "$index_path/segments") { |
if (! -e "$index_path/segments") { |
25 |
$create = 1; |
$create = 1; |
26 |
Jifty->log->debug("create index $index_path") unless ($store); |
Jifty->log->debug("create index $index_path"); |
27 |
} else { |
} else { |
28 |
Jifty->log->debug("open index: $index_path") unless ($store); |
$create = 0; |
29 |
|
Jifty->log->debug("open index: $index_path"); |
30 |
} |
} |
31 |
return $create; |
return $create; |
32 |
} |
} |
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 { |
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 |
|
|
126 |
|
|
127 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
128 |
|
|
129 |
|
return if ( $self->create ); |
130 |
|
|
131 |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
132 |
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
133 |
my $query = $parser->parse( $q ); |
my $query = $parser->parse( $q ); |
185 |
$writer->close; |
$writer->close; |
186 |
} |
} |
187 |
undef $writer; |
undef $writer; |
188 |
|
undef $create; |
189 |
|
|
190 |
|
return; |
191 |
} |
} |
192 |
|
|
193 |
|
=for TODO |
194 |
|
|
195 |
sub _signal { |
sub _signal { |
196 |
my $s = shift; |
my $s = shift; |
197 |
warn "catched SIG $s\n"; |
warn "catched SIG $s\n"; |
203 |
$SIG{'INT'} = \&_signal; |
$SIG{'INT'} = \&_signal; |
204 |
$SIG{'QUIT'} = \&_signal; |
$SIG{'QUIT'} = \&_signal; |
205 |
|
|
206 |
|
=cut |
207 |
|
|
208 |
|
=head2 snippet |
209 |
|
|
210 |
|
my $short = $self->snippet( 50, $text ); |
211 |
|
|
212 |
|
|
213 |
|
=cut |
214 |
|
|
215 |
|
sub snippet { |
216 |
|
my $self = shift; |
217 |
|
|
218 |
|
my $len = shift or die "no len?"; |
219 |
|
my $m = join(" ", @_); |
220 |
|
|
221 |
|
$m =~ s/\s+/ /gs; |
222 |
|
|
223 |
|
if (length($m) > $len) { |
224 |
|
return substr($m,0,$len) . '...'; |
225 |
|
} else { |
226 |
|
return $m; |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
1; |
1; |