12 |
my ( $analyzer, $store, $writer ); |
my ( $analyzer, $store, $writer ); |
13 |
|
|
14 |
my $debug = 1; |
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 |
} |
} |
52 |
|
|
53 |
=head2 add |
=head2 add |
54 |
|
|
55 |
Grep::Search->add( $record ); |
Grep::Search->add( $record, $owner_id ); |
56 |
|
|
57 |
=cut |
=cut |
58 |
|
|
60 |
my $self = shift; |
my $self = shift; |
61 |
|
|
62 |
my $i = shift or die "no record to add"; |
my $i = shift or die "no record to add"; |
63 |
|
my $uid = shift; |
64 |
|
|
65 |
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')); |
66 |
|
|
111 |
} |
} |
112 |
} |
} |
113 |
|
|
114 |
|
# add _owner_id to speed up filtering of search results |
115 |
|
$uid ||= Jifty->web->current_user->id; |
116 |
|
$doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid )); |
117 |
|
|
118 |
$self->writer->addDocument($doc); |
$self->writer->addDocument($doc); |
119 |
|
|
120 |
Jifty->log->debug("added ", $i->id, " to index"); |
Jifty->log->debug("added ", $i->id, " for user $uid to index"); |
121 |
} |
} |
122 |
|
|
123 |
=head2 |
=head2 |
131 |
|
|
132 |
my $q = shift or die "no q?"; |
my $q = shift or die "no q?"; |
133 |
|
|
134 |
|
return if ( $self->create ); |
135 |
|
|
136 |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
my $searcher = new Lucene::Search::IndexSearcher($self->store); |
137 |
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
my $parser = new Lucene::QueryParser("content", $self->analyzer); |
|
my $query = $parser->parse( $q ); |
|
138 |
|
|
139 |
Jifty->log->debug("searching for '$q'"); |
my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id; |
140 |
|
|
141 |
|
my $query = $parser->parse( $full_q ); |
142 |
|
|
143 |
|
Jifty->log->debug("searching for '$q' using ", $query->toString); |
144 |
|
|
145 |
my $hits = $searcher->search($query); |
my $hits = $searcher->search($query); |
146 |
my $num_hits = $hits->length(); |
my $num_hits = $hits->length(); |
193 |
$writer->close; |
$writer->close; |
194 |
} |
} |
195 |
undef $writer; |
undef $writer; |
196 |
|
undef $create; |
197 |
|
|
198 |
|
return; |
199 |
} |
} |
200 |
|
|
201 |
|
=for TODO |
202 |
|
|
203 |
sub _signal { |
sub _signal { |
204 |
my $s = shift; |
my $s = shift; |
205 |
warn "catched SIG $s\n"; |
warn "catched SIG $s\n"; |
211 |
$SIG{'INT'} = \&_signal; |
$SIG{'INT'} = \&_signal; |
212 |
$SIG{'QUIT'} = \&_signal; |
$SIG{'QUIT'} = \&_signal; |
213 |
|
|
214 |
|
=cut |
215 |
|
|
216 |
=head2 snippet |
=head2 snippet |
217 |
|
|