Toolkit/Search/Lucy.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 185 CC 10 SCAR 21.6
CC
10
Cov
0%
index_node
30.0
search_nodes
12.0
_fuzzy_match
12.0
CRAP 8.7
1 package Wiki::Toolkit::Search::Lucy;
2  
3 use strict;
4  
5 use Lucy::Analysis::PolyAnalyzer;
6 use Lucy::Index::Indexer;
7 use Lucy::Index::PolyReader;
8 use Lucy::Plan::FullTextType;
9 use Lucy::Plan::Schema;
10 use Lucy::Search::IndexSearcher;
11 use Lucy::Search::QueryParser;
12  
13 use vars qw( @ISA $VERSION );
14  
15 $VERSION = '0.03';
16 use base 'Wiki::Toolkit::Search::Base';
17  
18 =head1 NAME
19  
20 Wiki::Toolkit::Search::Lucy - Use Lucy to search your Wiki::Toolkit wiki.
21  
22 =head1 SYNOPSIS
23  
24 my $search = Wiki::Toolkit::Search::Lucy->new( path => "/var/lucy/wiki" );
25 my %wombat_nodes = $search->search_nodes( "wombat" );
26  
27 Provides L<Lucy>-based search methods for L<Wiki::Toolkit>.
28  
29 =cut
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =item B<new>
36  
37 my $search = Wiki::Toolkit::Search::Lucy->new(
38 path => "/var/lucy/wiki",
39 metadata_fields => [ "category", "locale", "address" ],
40 boost => { title => 2.5 },
41 content_munger => sub {
42 my $content = shift;
43 $content =~ s/secretword//gs;
44 return $content;
45 },
46 node_filter => sub {
47 my %args = @_;
48 return $args{content} =~ /REDIRECT/ ? 0 : 1;
49 },
50 );
51  
52 The C<path> parameter is mandatory. C<path> must be a directory
53 for storing the indexed data. It should exist and be writeable.
54  
55 The other four parameters are optional:
56  
57 C<metadata_fields> should be a reference to an array of metadata field
58 names.
59  
60 C<boost> should be a reference to a hash in which the keys are fields
61 and the values are numbers - see L<Lucy::Plan::FieldType> for more
62 info. Only C<title> is currently supported as a field value.
63  
64 C<content_munger> should be a reference to a subroutine which takes
65 the node content as a string and returns another string which will be
66 indexed in place of the original content.
67  
68 C<node_filter> should be a reference to a subroutine which takes the
69 named arguments C<node>, C<content>, and C<metadata> and returns either
70 true (yes, index this node) or false (no, don't index this node).
71  
72 Content munging takes place BEFORE node filtering.
73  
74 =cut
75  
76 sub _init {
77 my ( $self, %args ) = @_;
78  
79 # Set up the Lucy schema. Content and fuzzy title will be indexed but
80 # not stored (since we don't need to retrieve them).
81 my $schema = Lucy::Plan::Schema->new;
82 my $polyanalyzer = Lucy::Analysis::PolyAnalyzer->new( language => "en" );
83 my $stored_type = Lucy::Plan::FullTextType->new(
84 analyzer => $polyanalyzer );
85 my $unstored_type = Lucy::Plan::FullTextType->new(
86 analyzer => $polyanalyzer, stored => 0 );
87 my %title_args = ( analyzer => $polyanalyzer, stored => 1 );
88 if ( $args{boost}{title} ) {
89 $title_args{boost} = $args{boost}{title};
90 }
91 my $title_type = Lucy::Plan::FullTextType->new( %title_args );
92
93 $schema->spec_field( name => "content", type => $unstored_type );
94 $schema->spec_field( name => "fuzzy", type => $unstored_type );
95 $schema->spec_field( name => "title", type => $title_type );
96 $schema->spec_field( name => "key", type => $stored_type );
97  
98 foreach my $md_field ( @{$args{metadata_fields}} ) {
99 $schema->spec_field( name => $md_field, type => $unstored_type );
100 }
101  
102 $self->{_schema} = $schema;
103 $self->{_dir} = $args{path};
104 $self->{_metadata_fields} = $args{metadata_fields};
105 $self->{_content_munger} = $args{content_munger};
106 $self->{_node_filter} = $args{node_filter};
107 return $self;
108 }
109  
110 sub _dir { shift->{_dir} }
111 sub _schema { shift->{_schema} }
112  
113 =item B<index_node>
114  
115 $search->index_node( $node, $content, $metadata );
116  
117 Indexes or reindexes the given node in the search engine indexes.
118 You must supply both the node name and its content, but metadata is optional.
119  
120 If you do supply metadata, it should be a reference to a hash where
121 the keys are the names of the metadata fields and the values are
122 either scalars or references to arrays of scalars. For example:
123  
124 $search->index_node( "Calthorpe Arms", "Nice pub in Bloomsbury.",
125 { category => [ "Pubs", "Bloomsbury" ],
126 postcode => "WC1X 8JR" } );
127  
128 Only those metadata fields which were supplied to ->new will be taken
129 notice of - others will be silently ignored.
130  
131 If C<content_munger> has been supplied to C<new> as a subroutine
132 reference, then C<$content> will be run through this before indexing.
133  
134 If C<node_filter> has been supplied to C<new> as a subroutine reference,
135 then this will be used to check whether the node should be indexed or ignored.
136  
137 Content munging takes place BEFORE node filtering.
138  
139 =cut
140  
141 sub index_node {
142 my ( $self, $node, $content, $metadata ) = @_;
143  
144 # Delete the old version.
145 $self->_delete_node( $node );
146  
147 # See if we need to munge the content.
148 my $munger = $self->{_content_munger};
149 if ( $munger && ref $munger eq "CODE" ) {
150 $content = &$munger( $content );
151 }
152  
153 # See if this node should be ignored.
154 my $filter = $self->{_node_filter};
155 if ( $filter && ref $filter eq "CODE"
156 && ! &$filter( node => $node, content => $content,
157 metadata => $metadata ) ) {
158 return;
159 }
160  
161 my $indexer = Lucy::Index::Indexer->new(
162 index => $self->_dir,
163 schema => $self->_schema,
164 create => 1,
165 truncate => 0,
166 );
167  
168 my $key = $self->_make_key( $node );
169 my $fuzzy = $self->canonicalise_title( $node );
170  
171 my %data = (
172 content => join( " ", $node, $content ),
173 fuzzy => $fuzzy,
174 title => $node,
175 key => $key,
176 );
177  
178 my @fields = @{$self->{_metadata_fields}};
179 foreach my $field ( @fields ) {
180 my $value = $metadata->{$field};
181 if ( $value ) {
182 if ( ref $value ) {
183 $data{$field} = join( " ", @$value );
184 } else {
185 $data{$field} = $value;
186 }
187 }
188 }
189  
190 $indexer->add_doc( \%data );
191 $indexer->commit;
192 }
193  
194 sub _delete_node {
195 my ( $self, $node ) = @_;
196  
197 my $indexer = Lucy::Index::Indexer->new(
198 index => $self->_dir,
199 schema => $self->_schema,
200 create => 1,
201 truncate => 0,
202 );
203  
204 my $key = $self->_make_key( $node );
205  
206 $indexer->delete_by_term( field => "key", term => $key );
207 $indexer->commit;
208 }
209  
210 # We need to make a unique key for when we come to delete a doc - can't just
211 # delete on title as it does a search rather than an exact match on the field.
212 sub _make_key {
213 my ( $self, $node ) = @_;
214 $node =~ s/\s//g;
215 return lc( $node );
216 }
217  
218 =item B<search_nodes>
219  
220 # Find all the nodes which contain the word 'expert'.
221 my %results = $search->search_nodes( "expert" );
222  
223 Returns a (possibly empty) hash whose keys are the node names and
224 whose values are the scores.
225  
226 Defaults to AND searches (if C<$and_or> is not supplied, or is anything
227 other than C<OR> or C<or>).
228  
229 Searches are case-insensitive.
230  
231 =cut
232  
233 sub search_nodes {
234 my ( $self, $searchstring, $and_or ) = @_;
235  
236 # Bail and return empty list if nothing stored.
237 return () unless $self->_index_exists;
238  
239 $and_or = uc( $and_or || "" );
240 $and_or = "AND" unless $and_or eq "OR";
241  
242 my $queryparser = Lucy::Search::QueryParser->new(
243 schema => $self->_schema,
244 default_boolop => $and_or,
245 );
246  
247 my $query = $queryparser->parse( $searchstring );
248  
249 my $searcher = Lucy::Search::IndexSearcher->new(
250 index => $self->_dir,
251 );
252  
253 my $num_docs = $searcher->doc_max();
254 my $hits = $searcher->hits(
255 query => $query,
256 num_wanted => $num_docs,
257 );
258  
259 my %results;
260 while ( my $hit = $hits->next ) {
261 $results{ $hit->{title} } = $hit->get_score;
262 }
263 return %results;
264 }
265  
266 # Fuzzy title search - exact match has highest score.
267 sub _fuzzy_match {
268 my ( $self, $string, $canonical ) = @_;
269  
270 # Bail and return empty list if nothing stored.
271 return () unless $self->_index_exists;
272  
273 my $queryparser = Lucy::Search::QueryParser->new(
274 schema => $self->_schema,
275 default_boolop => "AND",
276 );
277  
278 my $query = $queryparser->parse( $canonical );
279  
280 my $searcher = Lucy::Search::IndexSearcher->new(
281 index => $self->_dir,
282 );
283  
284 my $num_docs = $searcher->doc_max();
285 my $hits = $searcher->hits(
286 query => $query,
287 num_wanted => $num_docs,
288 );
289  
290 my %results;
291 while ( my $hit = $hits->next ) {
292 $results{ $hit->{title} } = $hit->get_score;
293 }
294 return map { $_ => ($_ eq $string ? 2 : 1) } keys %results;
295 }
296  
297 # Returns true if and only if we have data stored.
298 sub _index_exists {
299 my $self = shift;
300 my $reader = Lucy::Index::PolyReader->open( index => $self->_dir );
301 return @{ $reader->seg_readers };
302 }
303  
304 sub supports_fuzzy_searches { 1; }
305 sub supports_phrase_searches { 1; }
306 sub supports_metadata_indexing { 1; }
307  
308 =back
309  
310 =head1 SEE ALSO
311  
312 L<Wiki::Toolkit>, L<Wiki::Toolkit::Search::Base>.
313  
314 =cut
315  
316 1;