line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plucene::TestCase; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Plucene::TestCase - Handy functions when testing Plucene |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Test::More tests => 10; |
10
|
|
|
|
|
|
|
use Plucene::TestCase; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
new_index { |
13
|
|
|
|
|
|
|
add_document( foo => "bar" ); |
14
|
|
|
|
|
|
|
}; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
re_index { |
17
|
|
|
|
|
|
|
add_document( foo => "baz" ); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
with_reader { |
21
|
|
|
|
|
|
|
$READER->whatever; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $hits = search("foo:ba*"); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 EXPORTS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
11
|
|
|
11
|
|
145162
|
use strict; |
|
11
|
|
|
|
|
25
|
|
|
11
|
|
|
|
|
414
|
|
31
|
11
|
|
|
11
|
|
57
|
use warnings; |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
308
|
|
32
|
|
|
|
|
|
|
|
33
|
11
|
|
|
11
|
|
57
|
use base 'Exporter'; |
|
11
|
|
|
|
|
18
|
|
|
11
|
|
|
|
|
1350
|
|
34
|
|
|
|
|
|
|
|
35
|
11
|
|
|
11
|
|
7530
|
use Plucene::Index::Reader; |
|
11
|
|
|
|
|
42
|
|
|
11
|
|
|
|
|
363
|
|
36
|
11
|
|
|
11
|
|
7537
|
use Plucene::Index::Writer; |
|
11
|
|
|
|
|
42
|
|
|
11
|
|
|
|
|
393
|
|
37
|
11
|
|
|
11
|
|
84
|
use Plucene::Document; |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
89
|
|
38
|
11
|
|
|
11
|
|
323
|
use Plucene::Document::Field; |
|
11
|
|
|
|
|
76
|
|
|
11
|
|
|
|
|
85
|
|
39
|
11
|
|
|
11
|
|
7054
|
use Plucene::Analysis::SimpleAnalyzer; |
|
11
|
|
|
|
|
71
|
|
|
11
|
|
|
|
|
367
|
|
40
|
11
|
|
|
11
|
|
5684
|
use Plucene::QueryParser; |
|
11
|
|
|
|
|
50
|
|
|
11
|
|
|
|
|
115
|
|
41
|
11
|
|
|
11
|
|
8170
|
use Plucene::Search::IndexSearcher; |
|
11
|
|
|
|
|
41
|
|
|
11
|
|
|
|
|
1224
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
our (@EXPORT, $DIR, $DEBUG, $WRITER, $READER, $ANALYZER); |
44
|
|
|
|
|
|
|
@EXPORT = qw($DIR $WRITER $READER $ANALYZER new_index re_index |
45
|
|
|
|
|
|
|
with_reader add_document search); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$ANALYZER = "Plucene::Analysis::SimpleAnalyzer"; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over 3 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item C<$DIR> |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
A directory which is created for the purposes of this test, in which the |
54
|
|
|
|
|
|
|
index will be placed. It will normally be cleaned up at the end of the |
55
|
|
|
|
|
|
|
test, unless C<$Plucene::TestCase::DEBUG> is set to allow you to peruse |
56
|
|
|
|
|
|
|
the entrails. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
|
60
|
11
|
|
|
11
|
|
85
|
use File::Temp qw(tempdir); |
|
11
|
|
|
|
|
25
|
|
|
11
|
|
|
|
|
6894
|
|
61
|
|
|
|
|
|
|
$DIR = tempdir(CLEANUP => !$DEBUG); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item C<$WRITER> |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
A variable holding the current C object, if there is one. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item C<$READER> |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
A variable holding the current C object, if there is one. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item C<$ANALYZER> |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
A variable holding the class name of the desired C |
74
|
|
|
|
|
|
|
class. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item new_index BLOCK (Analyzer) |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Create a new index, and do the following stuff in the block before |
79
|
|
|
|
|
|
|
closing the index writer. C<$WRITER> is set for the duration of the |
80
|
|
|
|
|
|
|
block. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
The optional parameter should be the class name of the analyzer to use; |
83
|
|
|
|
|
|
|
if not specified, the value from C<$ANALYZER>, which in turn defaults to |
84
|
|
|
|
|
|
|
C, will be used. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub new_index(&;$) { |
89
|
11
|
|
|
11
|
1
|
36699
|
my ($block, $analyzer) = @_; |
90
|
11
|
|
33
|
|
|
116
|
$analyzer ||= $ANALYZER; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# UNIVERSAL::require loads UNIVERSAL->import, which won't do. |
93
|
11
|
|
|
|
|
1290
|
eval "require $analyzer"; |
94
|
11
|
50
|
|
|
|
74
|
die "Couldn't require $analyzer" if $@; |
95
|
11
|
|
|
|
|
169
|
$WRITER = Plucene::Index::Writer->new($DIR, $analyzer->new, 1); |
96
|
11
|
|
|
|
|
60
|
$block->(); |
97
|
11
|
|
|
|
|
2916
|
undef $WRITER; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item re_index BLOCK (Analyzer) |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Same as C, but doesn't create a new index, rather re-uses an |
103
|
|
|
|
|
|
|
old one. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub re_index(&;$) { |
108
|
1
|
|
|
1
|
1
|
3
|
my ($block, $analyzer) = @_; |
109
|
1
|
|
33
|
|
|
8
|
$analyzer ||= $ANALYZER; |
110
|
1
|
|
|
|
|
153
|
eval "require $analyzer"; |
111
|
1
|
50
|
|
|
|
5
|
die "Couldn't require $analyzer" if $@; |
112
|
1
|
|
|
|
|
11
|
$WRITER = Plucene::Index::Writer->new($DIR, $analyzer->new, 0); |
113
|
1
|
|
|
|
|
5
|
$block->(); |
114
|
1
|
|
|
|
|
720
|
undef $WRITER; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item add_document( field1 => value1, ...) |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Add a new document to the index, with the given fields and values |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub add_document { |
124
|
199
|
|
|
199
|
1
|
520717
|
my @args = @_; |
125
|
199
|
|
|
|
|
1518
|
my $doc = Plucene::Document->new; |
126
|
199
|
|
|
|
|
3081
|
while (my ($k, $v) = splice(@args, 0, 2)) { |
127
|
250
|
|
|
|
|
2528
|
$doc->add(Plucene::Document::Field->Text($k, $v)); |
128
|
|
|
|
|
|
|
} |
129
|
199
|
|
|
|
|
3201
|
$WRITER->add_document($doc); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item with_reader BLOCK |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Opens an index reader in C<$READER> and runs the block. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub with_reader (&) { |
139
|
4
|
|
|
4
|
1
|
1065
|
$READER = Plucene::Index::Reader->open($DIR); |
140
|
4
|
|
|
|
|
21
|
shift->(); |
141
|
4
|
|
|
|
|
2006
|
$READER->close; |
142
|
4
|
|
|
|
|
24
|
undef $READER; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item search |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Searches for the query given. If any fields are not specified, they will |
148
|
|
|
|
|
|
|
be assumed to be the default C. Returns a C |
149
|
|
|
|
|
|
|
object. The value of C<$ANALYZER> will be used to construct an analyzer |
150
|
|
|
|
|
|
|
for the query string. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=cut |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub search { |
155
|
169
|
|
|
169
|
1
|
26265
|
eval "require $ANALYZER"; |
156
|
169
|
|
|
|
|
2104
|
my $parser = Plucene::QueryParser->new({ |
157
|
|
|
|
|
|
|
analyzer => $ANALYZER->new(), |
158
|
|
|
|
|
|
|
default => "text" |
159
|
|
|
|
|
|
|
}); |
160
|
169
|
|
|
|
|
1945
|
Plucene::Search::IndexSearcher->new($DIR)->search($parser->parse(shift)); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |