1 |
dpavlin |
12 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
|
5 |
|
|
# |
6 |
|
|
# simple implementation to use SWISH-e with queryies like |
7 |
|
|
# Lucene (subject:something) |
8 |
|
|
# |
9 |
|
|
|
10 |
|
|
use SWISH::API; |
11 |
|
|
|
12 |
|
|
sub open_index { |
13 |
|
|
my $self = shift; |
14 |
|
|
|
15 |
|
|
my $swish = $self->{index}; |
16 |
|
|
|
17 |
|
|
if (! $swish) { |
18 |
|
|
|
19 |
|
|
my $index_file = $self->{index_file} || croak "open_index needs index filename"; |
20 |
|
|
$index_file .= "/swish-e"; |
21 |
|
|
print STDERR "opening index '$index_file'\n"; |
22 |
|
|
$swish = SWISH::API->new($index_file); |
23 |
|
|
$swish->AbortLastError if $swish->Error; |
24 |
|
|
|
25 |
|
|
$self->{index} = $swish; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
return $swish; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
sub search_index { |
32 |
|
|
my $self = shift; |
33 |
|
|
|
34 |
|
|
my $s = shift || croak "search_index needs query"; |
35 |
|
|
|
36 |
|
|
my $index = $self->open_index; |
37 |
|
|
|
38 |
|
|
if ($s =~ /:/) { |
39 |
|
|
my ($fld,$val) = split(/:/,$s,2); |
40 |
|
|
$s = "$fld=($val)"; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
print STDERR "swish search: $s\n"; |
44 |
|
|
my $results = $index->Query($s); |
45 |
|
|
|
46 |
|
|
my @r; |
47 |
|
|
|
48 |
|
|
while ( my $result = $results->NextResult ) { |
49 |
dpavlin |
13 |
my $id = $result->Property( "swishdocpath" ); |
50 |
|
|
push @r, $id; |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
foreach my $p (qw(from to cc bcc)) { |
54 |
|
|
@{$self->{cache}->{$id}->{$p}} = split(/##/, $result->Property($p.'_phrase')); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
foreach my $p (qw(subject body date)) { |
58 |
|
|
$self->{cache}->{$id}->{$p} = $result->Property($p); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
$self->{cache}->{$id}->{'body'} =~ s/##lf##/\n/gs; |
62 |
|
|
|
63 |
|
|
# this is redundant, but needed for templates later... |
64 |
|
|
$self->{cache}->{$id}->{'id'} = $id; |
65 |
dpavlin |
12 |
} |
66 |
|
|
|
67 |
|
|
return @r; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
sub add_index { |
71 |
|
|
my $self = shift; |
72 |
|
|
|
73 |
|
|
croak("add_index is not implemented for swish!"); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
sub close_index { |
77 |
|
|
my $self = shift; |
78 |
|
|
|
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
1; |