Toolkit/Search/Plucene.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 191 CC 10 SCAR 17.3
CC
10
Cov
0%
_search_nodes
20.0
index_node
12.0
_writer
6.0
CRAP 5.6
1 package Wiki::Toolkit::Search::Plucene;
2  
3 use strict;
4 use Carp "croak";
5 use File::Spec::Functions qw(catfile);
6 use Plucene::Document;
7 use Plucene::Document::Field;
8 use Plucene::Index::Writer;
9 use Plucene::Analysis::SimpleAnalyzer;
10 use Plucene::QueryParser;
11 use Plucene::Search::IndexSearcher;
12 use Plucene::Search::HitCollector;
13  
14 use vars qw( @ISA $VERSION );
15  
16 $VERSION = '0.02';
17 use base 'Wiki::Toolkit::Search::Base';
18  
19 =head1 NAME
20  
21 Wiki::Toolkit::Search::Plucene - Use Plucene to search your Wiki::Toolkit wiki.
22  
23 =head1 SYNOPSIS
24  
25 my $search = Wiki::Toolkit::Search::Plucene->new( path => "/var/plucene/wiki" );
26 my %wombat_nodes = $search->search_nodes("wombat");
27  
28 Provides search-related methods for L<Wiki::Toolkit>.
29  
30 =cut
31  
32 =head1 METHODS
33  
34 =over 4
35  
36 =item B<new>
37  
38 my $search = Wiki::Toolkit::Search::Plucene->new(
39 path => "/var/plucene/wiki",
40 content_munger => sub {
41 my $content = shift;
42 $content =~ s/secretword//gs;
43 return $content;
44 },
45 node_filter => sub {
46 my %args = @_;
47 return $args{content} =~ /REDIRECT/ ? 0 : 1;
48 },
49 );
50  
51 The C<path> parameter is mandatory. C<path> must be a directory
52 for storing the indexed data. It should exist and be writeable.
53  
54 The C<content_munger> parameter is optional. It should be a reference to a
55 subroutine which takes the node content as a string and returns
56 another string which will be indexed in place of the original content.
57  
58 The C<node_filter> parameter is also optional. It should be a
59 reference to a subroutine which takes the named arguments C<node> and
60 C<content>, and returns either true (yes, index this node) or false
61 (no, don't index this node).
62  
63 Content munging takes place BEFORE node filtering.
64  
65 =cut
66  
67 sub _init {
68 my ($self, %args) = @_;
69 $self->{_dir} = $args{path};
70 $self->{_content_munger} = $args{content_munger};
71 $self->{_node_filter} = $args{node_filter};
72 return $self;
73 }
74  
75 sub _dir { shift->{_dir} }
76  
77 sub _parsed_query {
78 my ($self, $query, $default) = @_;
79 my $parser = Plucene::QueryParser->new({
80 analyzer => Plucene::Analysis::SimpleAnalyzer->new(),
81 default => $default
82 });
83 $parser->parse($query);
84 }
85  
86 # Make new searchers, readers and writers each time we're asked for
87 # one - if we made them in _init then they would always think the index
88 # has the same stuff in as it had when they were made.
89  
90 sub _searcher {
91 my $self = shift;
92 return Plucene::Search::IndexSearcher->new( $self->_dir );
93 }
94 sub _reader {
95 my $self = shift;
96 return Plucene::Index::Reader->open( $self->_dir );
97 }
98  
99 sub _writer {
100 my $self = shift;
101 return Plucene::Index::Writer->new(
102 $self->_dir,
103 Plucene::Analysis::SimpleAnalyzer->new,
104 -e catfile($self->_dir, "segments") ? 0 : 1
105 );
106 }
107  
108 sub _index_exists {
109 my $self = shift;
110 if ( -e catfile($self->_dir, "segments") ) {
111 return 1;
112 }
113 }
114  
115 sub _search_nodes {
116 my ($self, $query, $and_or) = @_;
117  
118 # Bail and return empty list if nothing stored in directory.
119 return () unless $self->_index_exists;
120  
121 local $Plucene::QueryParser::DefaultOperator = "AND"
122 unless ( $and_or and lc($and_or) eq "or" );
123 my @docs;
124 my $searcher = $self->_searcher;
125 my $hc = Plucene::Search::HitCollector->new(
126 collect => sub {
127 my ($self, $doc, $score) = @_;
128 my $res = eval { $searcher->doc($doc) };
129 push @docs, [ $res, $score ] if $res;
130 });
131 my $parsed_query = $self->_parsed_query( $query, 'text' );
132 $searcher->search_hc($parsed_query, $hc);
133 @docs = map $_->[0]->get("id")->string, sort { $b->[1] <=> $a->[1] } @docs;
134 return @docs;
135 }
136  
137 sub search_nodes {
138 my ($self, @args) = @_;
139 my @docs = $self->_search_nodes( @args );
140 my $i = 1;
141 return map { $_ => $i++ } @docs;
142 }
143  
144 sub _fuzzy_match {
145 my ($self, $string, $canonical) = @_;
146 return map { $_ => ($_ eq $string ? 2 : 1) }
147 $self->_search_nodes("fuzzy:$canonical");
148 }
149  
150 sub index_node {
151 my ($self, $node, $content) = @_;
152  
153 # Delete the old version.
154 $self->delete_node( $node );
155  
156 # See if we need to munge the content.
157 my $munger = $self->{_content_munger};
158 if ( $munger && ref $munger eq "CODE" ) {
159 $content = &$munger( $content );
160 }
161  
162 # See if this node should be ignored.
163 my $filter = $self->{_node_filter};
164 if ( $filter && ref $filter eq "CODE"
165 && ! &$filter( node => $node, content => $content ) ) {
166 return;
167 }
168  
169 my $writer = $self->_writer;
170 my $doc = Plucene::Document->new;
171 my $fuzzy = $self->canonicalise_title( $node );
172  
173 $doc->add( Plucene::Document::Field->Text( "content", join( " ", $node, $content ) ) );
174 $doc->add( Plucene::Document::Field->Text( "fuzzy", $fuzzy ) );
175 $doc->add( Plucene::Document::Field->Text( "title", $node ) );
176 $doc->add(Plucene::Document::Field->Keyword(id => $node));
177 $doc->add(Plucene::Document::Field->UnStored('text' => join( " ", $node, $content )));
178 $writer->add_document($doc);
179 }
180  
181 sub optimize { shift->_writer->optimize() }
182  
183 sub indexed {
184 my ($self, $id) = @_;
185 my $term = Plucene::Index::Term->new({ field => 'id', text => $id });
186 return $self->_reader->doc_freq($term);
187 }
188  
189 sub delete_node {
190 my ($self, $id) = @_;
191 return unless $self->_index_exists;
192 my $reader = $self->_reader;
193 $reader->delete_term(
194 Plucene::Index::Term->new({ field => "id", text => $id }));
195 $reader->close;
196 }
197  
198 sub supports_phrase_searches { return 1; }
199 sub supports_fuzzy_searches { return 1; }
200  
201 =back
202  
203 =head1 SEE ALSO
204  
205 L<Wiki::Toolkit>, L<Wiki::Toolkit::Search::Base>.
206  
207 =cut
208  
209 1;