8 |
|
|
9 |
use Carp; |
use Carp; |
10 |
use File::Temp qw/ tempdir /; |
use File::Temp qw/ tempdir /; |
11 |
|
#use YAML; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
39 |
index_dir => '/path/to/index', |
index_dir => '/path/to/index', |
40 |
index => 'index++', |
index => 'index++', |
41 |
search => 'search++', |
search => 'search++', |
42 |
|
debug => 1, |
43 |
); |
); |
44 |
|
|
45 |
Options to open are following: |
Options to open are following: |
60 |
|
|
61 |
Full or partial path to SWISH++ search executable. By default, it's B<search>. |
Full or partial path to SWISH++ search executable. By default, it's B<search>. |
62 |
|
|
63 |
|
=item C<debug> |
64 |
|
|
65 |
|
This option (off by default) will produce a lot of debugging output on |
66 |
|
C<STDERR> prefixed by C<##>. |
67 |
|
|
68 |
=back |
=back |
69 |
|
|
70 |
=cut |
=cut |
86 |
$self->{'index'} ||= 'index'; |
$self->{'index'} ||= 'index'; |
87 |
$self->{'search'} ||= 'search'; |
$self->{'search'} ||= 'search'; |
88 |
|
|
89 |
|
print STDERR "## open index_dir: ",$self->{'index_dir'}," index: ",$self->{'index'}, " search: ",$self->{'search'},"\n" if ($self->{'debug'}); |
90 |
|
|
91 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
92 |
} |
} |
93 |
|
|
155 |
return 1; |
return 1; |
156 |
} |
} |
157 |
|
|
158 |
|
=head2 search |
159 |
|
|
160 |
|
Search your index. |
161 |
|
|
162 |
|
my @results = $i->search("swhish query"); |
163 |
|
|
164 |
|
Returns array with result IDs. |
165 |
|
|
166 |
|
=cut |
167 |
|
|
168 |
|
sub search { |
169 |
|
my $self = shift; |
170 |
|
|
171 |
|
my $query = shift || return; |
172 |
|
|
173 |
|
$self->_close_index; |
174 |
|
|
175 |
|
my @results; |
176 |
|
|
177 |
|
# escape double quotes in query for shell |
178 |
|
$query =~ s/"/\\"/g; |
179 |
|
|
180 |
|
my $open_cmd = $self->{'search'}." -i ".$self->{'index_dir'}.'/index "'.$query.'" |'; |
181 |
|
print STDERR "## search $open_cmd\n" if ($self->{'debug'}); |
182 |
|
|
183 |
|
CORE::open(SEARCH, $open_cmd) || confess "can't start $open_cmd: $!"; |
184 |
|
while(<SEARCH>) { |
185 |
|
next if (/^#/); |
186 |
|
chomp; |
187 |
|
print STDERR "## $_\n" if ($self->{'debug'}); |
188 |
|
my ($rank,$path,$size,$title) = split(/ /,$_,4); |
189 |
|
push @results, { |
190 |
|
rank => $rank, |
191 |
|
path => $path, |
192 |
|
size => $size, |
193 |
|
title => $title, |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
|
close(SEARCH) || confess "can't close search"; |
198 |
|
|
199 |
|
#print STDERR "## results: ",Dump(@results),"\n" if ($self->{'debug'}); |
200 |
|
|
201 |
|
return @results; |
202 |
|
} |
203 |
|
|
204 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
205 |
|
|
206 |
Private methods implement internals for creating temporary file needed for |
Private methods implement internals for creating temporary file needed for |
223 |
|
|
224 |
my $opt = "-v 4"; |
my $opt = "-v 4"; |
225 |
|
|
226 |
my $open_cmd = '| index '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; |
my $open_cmd = '| '.$self->{'index'}.' '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; |
227 |
|
|
228 |
chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; |
chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; |
229 |
|
|
245 |
} |
} |
246 |
); |
); |
247 |
|
|
248 |
|
To delete document, just omit body and meta data. |
249 |
|
|
250 |
=cut |
=cut |
251 |
|
|
252 |
sub _create_doc { |
sub _create_doc { |
274 |
print { $self->{'index_fh'} } $arg->{'path'}."\n"; |
print { $self->{'index_fh'} } $arg->{'path'}."\n"; |
275 |
} |
} |
276 |
|
|
277 |
|
=head2 _close_index |
278 |
|
|
279 |
|
Close index after indexing. |
280 |
|
|
281 |
|
$i->_close_index; |
282 |
|
|
283 |
|
You have to close index before searching. |
284 |
|
|
285 |
|
=cut |
286 |
|
|
287 |
|
sub _close_index { |
288 |
|
my $self = shift; |
289 |
|
|
290 |
|
return unless ($self->{'index_fh'}); |
291 |
|
|
292 |
|
print STDERR "## close index\n" if ($self->{'debug'}); |
293 |
|
|
294 |
|
close($self->{'index_fh'}); |
295 |
|
undef $self->{'index_fh'}; |
296 |
|
} |
297 |
|
|
298 |
1; |
1; |
299 |
__END__ |
__END__ |
300 |
|
|