Plucene/QueryParser.pm

filter: Statementstmt 98.5% 137 / 139 Branchbran 85.2% 58 / 68 Conditioncond 93.3% 14 / 15 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 94.4% 237 / 251 CC 43 SCAR 22.4
CC
43
Cov
94%
parse
18.1
add_clause
12.1
to_plucene
5.0
CRAP 9.4
↑ Summary Plucene/Index/SegmentMerger.pm »
1 package Plucene::QueryParser;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Balanced qw(extract_delimited extract_bracketed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 our $DefaultOperator = "OR";
12  
13 15 __PACKAGE__->mk_accessors(qw(analyzer default));
14  
15 =head1 NAME
16  
17 Plucene::QueryParser - Turn query strings into Plucene::Search::Query objects
18  
19 =head1 SYNOPSIS
20  
21 my $p = Plucene::QueryParser->new({
22 analyzer => Plucene::Analysis::Analyzer $a,
23 default => "text"
24 });
25  
26 my Plucene::Search::Query $q = $p->parse("foo bar:baz");
27  
28 =head1 DESCRIPTION
29  
30 This module is responsible for turning a query string into a
31 Plucene::Query object. It needs to have an Analyzer object to help it
32 tokenize incoming queries, and it also needs to know the default field
33 to be used if no field is given in the query string.
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 my $p = Plucene::QueryParser->new({
40 analyzer => Plucene::Analysis::Analyzer $a,
41 default => "text"
42 });
43  
44 Construct a new query parser
45  
46 =cut
47  
48 188 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
49 188 my $self = shift->SUPER::new(@_);
50 croak "You need to pass an analyzer"
51 188 ▶ unless UNIVERSAL::isa($self->{analyzer}, "Plucene::Analysis::Analyzer");
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self->{"analyzer"}, "Plucene::Analysis::Analyzer") 0 188 188
52 188 ▶ croak "No default field name supplied!" unless $self->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"default"} 0 188 188
53 188 return $self;
54 }
55  
56 =head2 parse
57  
58 my Plucene::Search::Query $q = $p->parse("foo bar:baz");
59  
60 Turns the string into a query object.
61  
62 =cut
63  
64 225 ▶sub parse {
Subroutine parse: called CC=18 SCAR=29.0
Pod documented
65 225 my $self = shift;
66 225 local $_ = shift;
67 225 my $ast = shift;
68 225 my @rv;
69 225 ▶ while ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 305 225 530
70 305 ▶ s/^\s+// and next;
Branch: 1 branch
BranchTrueFalseTotal
s/^\s+// and next 40 265 305
71 265 my $item;
72 265 $item->{conj} = "NONE";
73 265 s/^(AND|OR|\|\|)\s+//i;
74 265 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 22 243 265
75 22 $item->{conj} = uc $1;
76 $item->{conj} = "OR"
77 22 ▶ if $item->{conj} eq "||";
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"conj"} eq "||" 0 22 22
78 }
79 265 ▶ if (s/^\+//) { $item->{mods} = "REQ"; }
Branch: 2 branches
BranchTrueFalseTotal
if (s/^\+//) { } 4 261 265
elsif (s/^(-|!|NOT(?=[^\w:]))\s*//i) { } 6 255 261
80 6 elsif (s/^(-|!|NOT(?=[^\w:]))\s*//i) { $item->{mods} = "NOT"; }
81 255 else { $item->{mods} = "NONE"; }
82  
83 265 ▶ if (s/^([^\s(":]+)://) { $item->{field} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (s/^([^\s(":]+)://) 154 111 265
84  
85 # Subquery
86 265 ▶ if (/^\(/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^\(/) { } 10 255 265
elsif (/^"/) { } 10 245 255
elsif (s/^(\S+)\*//) { } 4 241 245
87 10 my ($extracted, $remainer) = extract_bracketed($_, "(");
88 0 ▶ if (!$extracted) { croak "Unbalanced subquery" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($extracted) 0 10 10
89 10 $_ = $remainer;
90 10 $extracted =~ s/^\(//;
91 10 $extracted =~ s/\)$//;
92 10 $item->{query} = "SUBQUERY";
93 10 $item->{subquery} = $self->parse($extracted, 1);
94 } elsif (/^"/) {
95 10 my ($extracted, $remainer) = extract_delimited($_, '"');
96 0 ▶ if (!$extracted) { croak "Unbalanced phrase" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($extracted) 0 10 10
97 10 $_ = $remainer;
98 10 $extracted =~ s/^"//;
99 10 $extracted =~ s/"$//;
100 10 $item->{query} = "PHRASE";
101 10 $item->{term} = $self->_tokenize($extracted);
102 } elsif (s/^(\S+)\*//) {
103 4 $item->{query} = "PREFIX";
104 4 $item->{term} = $self->_tokenize($1);
105 } else {
106 241 ▶ s/([^\s\^]+)// or croak "Malformed query";
Branch: 1 branch
BranchTrueFalseTotal
s/([^\s\^]+)// or croak("Malformed query") 241 0 241
107 241 $item->{query} = "TERM";
108 241 $item->{term} = $self->_tokenize($1);
109 241 ▶ if ($item->{term} =~ / /) { $item->{query} = "PHRASE"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"term"} =~ / /) 2 239 241
110 }
111 265 ▶ s/^~(\d+)// and $item->{slop} = $1;
Branch: 1 branch
BranchTrueFalseTotal
s/^~(\d+)// and $item->{"slop"} = $1 3 262 265
112 265 ▶ if (s/^\^(\d+(?:.\d+)?)//) { $item->{boost} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\^(\d+(?:.\d+)?)//) 2 263 265
113  
114 push @rv, bless $item,
115 265 "Plucene::QueryParser::" . ucfirst lc $item->{query};
116 }
117 225 my $obj = bless \@rv, "Plucene::QueryParser::TopLevel";
118  
119 # If we only want the AST, don't convert to a Search::Query.
120 225 ▶ if ($ast) { return $obj }
Branch: 1 branch
BranchTrueFalseTotal
if ($ast) 10 215 225
121 215 return $obj->to_plucene($self->{default});
122 }
123  
124 255 ▶sub _tokenize {
Subroutine _tokenize: called CC=2 SCAR=6.9
125 255 my ($self, $image) = @_;
126 my $stream = $self->{analyzer}->tokenstream(
127 {
128 field => $self->{default},
129 255 reader => IO::Scalar->new(\$image)
130 }
131 );
132 255 my @words;
133 255 ▶ while (my $x = $stream->next) { push @words, $x->text }
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = $stream->next) 267 255 522
134 255 join(" ", @words);
135 }
136  
137 package Plucene::QueryParser::TopLevel;
138  
139 225 ▶sub to_plucene {
Subroutine to_plucene: called CC=5 SCAR=16.1
140 225 my ($self, $field) = @_;
141 return $self->[0]->to_plucene($field)
142 if @$self == 1
143 225 ▶ and $self->[0]->{mods} eq "NONE";
Branch: 1 branch
BranchTrueFalseTotal
if @$self == 1 and $self->[0]{"mods"} eq "NONE" 184 41 225
Condition100%3/3
@$self == 1 and $self->[0]{"mods"} eq "NONE"
!ll&&!rl&&r
383184
MC/DC100%2/2
@$self == 1 and $self->[0]{"mods"} eq "NONE"
@$self == 1 $self->[0]{"mods"} eq "NONE"
Truth table100%3/3
A and B
A= @$self == 1B= $self->[0]{"mods"} eq "NONE"
ABresult
0X0
100
111
144  
145 41 my @clauses;
146 41 $self->add_clause(\@clauses, $_, $field) for @$self;
147 41 require Plucene::Search::BooleanQuery;
148 41 my $query = new Plucene::Search::BooleanQuery;
149 41 $query->add_clause($_) for @clauses;
150  
151 41 $query;
152 }
153  
154 81 ▶sub add_clause {
Subroutine add_clause: called CC=12 SCAR=25.0
155 81 my ($self, $clauses, $term, $field) = @_;
156 81 my $q = $term->to_plucene($field);
157 81 ▶ if ($term->{conj} eq "AND" and @$clauses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->{"conj"} eq "AND" and @$clauses) 12 69 81
Condition66%2/3
$term->{"conj"} eq "AND" and @$clauses
!ll&&!rl&&r
69012
MC/DC50%1/2
$term->{"conj"} eq "AND" and @$clauses
$term->{"conj"} eq "AND" @$clauses
Truth table66%2/3
A and B
A= $term->{"conj"} eq "AND"B= @$clauses
ABresult
0X0
100
111
158  
159 # The previous term needs to become required
160 12 ▶ $clauses->[-1]->required(1) unless $clauses->[-1]->prohibited;
Branch: 1 branch
BranchTrueFalseTotal
unless $clauses->[-1]->prohibited 12 0 12
161 }
162  
163 81 ▶ if ( $Plucene::QueryParser::DefaultOperator eq "AND"
Branch: 1 branch
BranchTrueFalseTotal
if ($Plucene::QueryParser::DefaultOperator eq "AND" and $term->{"conj"} eq "OR") 2 79 81
Condition100%3/3
$Plucene::QueryParser::DefaultOperator eq "AND" and $term->{"conj"} eq "OR"
!ll&&!rl&&r
61182
MC/DC100%2/2
$Plucene::QueryParser::DefaultOperator eq "AND" and $term->{"conj"} eq "OR"
$Plucene::QueryParser::DefaultOperator eq "AND" $term->{"conj"} eq "OR"
Truth table100%3/3
A and B
A= $Plucene::QueryParser::DefaultOperator eq "AND"B= $term->{"conj"} eq "OR"
ABresult
0X0
100
111
164 and $term->{conj} eq "OR") {
165 2 ▶ $clauses->[-1]->required(0) unless $clauses->[-1]->prohibited;
Branch: 1 branch
BranchTrueFalseTotal
unless $clauses->[-1]->prohibited 2 0 2
166 }
167  
168 81 ▶ return unless $q; # Shouldn't happen yet
Branch: 1 branch
BranchTrueFalseTotal
unless $q 0 81 81
169 81 my $prohibited;
170 my $required;
171 81 ▶ if ($Plucene::QueryParser::DefaultOperator eq "OR") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Plucene::QueryParser::DefaultOperator eq 'OR') { } 61 20 81
172  
173 # We set REQUIRED if we're introduced by AND or +; PROHIBITED if
174 # introduced by NOT or -; make sure not to set both.
175 61 $prohibited = ($term->{mods} eq "NOT");
176 61 $required = ($term->{mods} eq "REQ");
177  
178 61 ▶ $required = 1 if $term->{conj} eq "AND" and !$prohibited;
Branch: 1 branch
BranchTrueFalseTotal
if $term->{"conj"} eq "AND" and not $prohibited 8 53 61
Condition100%3/3
$term->{"conj"} eq "AND" and not $prohibited
!ll&&!rl&&r
5218
MC/DC100%2/2
$term->{"conj"} eq "AND" and not $prohibited
$term->{"conj"} eq "AND" not $prohibited
Truth table100%3/3
A and not(B)
A= $term->{"conj"} eq "AND"B= not $prohibited
ABresult
0X0
100
111
179 } else {
180  
181 # We set PROHIBITED if we're introduced by NOT or -; We set
182 # REQUIRED if not PROHIBITED and not introduced by OR
183 20 $prohibited = ($term->{mods} eq "NOT");
184 20 ▶ $required = (!$prohibited and $term->{conj} ne "OR");
Condition100%3/3
!$prohibited && $term->{'conj'} ne 'OR'
!ll&&!rl&&r
1217
MC/DC100%2/2
!$prohibited && $term->{'conj'} ne 'OR'
!$prohibited $term->{'conj'} ne 'OR'
Truth table100%3/3
not(A) && B
A= !$prohibitedB= $term->{'conj'} ne 'OR'
ABresult
0X0
100
111
185 }
186 81 require Plucene::Search::BooleanClause;
187 81 push @$clauses,
188 Plucene::Search::BooleanClause->new(
189 {
190 prohibited => $prohibited,
191 required => $required,
192 query => $q
193 }
194 );
195 }
196  
197 package Plucene::QueryParser::Term;
198  
199 240 ▶sub to_plucene {
Subroutine to_plucene: called CC=1 SCAR=0.0
200 240 require Plucene::Search::TermQuery;
201 240 require Plucene::Index::Term;
202 240 my ($self, $field) = @_;
203 240 $self->set_term($field);
204 240 my $q = Plucene::Search::TermQuery->new({ term => $self->{pl_term} });
205 240 $self->set_boost($q);
206 240 return $q;
207 }
208  
209 244 ▶sub set_term {
Subroutine set_term: called CC=2 SCAR=6.9
210 244 my ($self, $field) = @_;
211 $self->{pl_term} = Plucene::Index::Term->new(
212 {
213 field => (exists $self->{field} ? $self->{field} : $field),
214 text => $self->{term}
215 }
216 244 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'field'} ? : 153 91 244
217 }
218  
219 255 ▶sub set_boost {
Subroutine set_boost: called CC=2 SCAR=6.9
220 255 my ($self, $q) = @_;
221 255 ▶ $q->boost($self->{boost}) if exists $self->{boost};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"boost"} 2 253 255
222 }
223  
224 package Plucene::QueryParser::Phrase;
225 15 our @ISA = qw(Plucene::QueryParser::Term);
226  
227 # This corresponds to the rules for "PHRASE" in the Plucene grammar
228  
229 12 ▶sub to_plucene {
Subroutine to_plucene: called CC=5 SCAR=16.1
230 12 require Plucene::Search::PhraseQuery;
231 12 require Plucene::Index::Term;
232 12 my ($self, $field) = @_;
233 12 my @words = split /\s+/, $self->{term};
234 12 ▶ return $self->SUPER::to_plucene($field) if @words == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @words == 1 1 11 12
235  
236 11 my $phrase = Plucene::Search::PhraseQuery->new;
237 11 for my $word (@words) {
238 my $term = Plucene::Index::Term->new(
239 {
240 23 ▶ field => (exists $self->{field} ? $self->{field} : $field),
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'field'} ? : 2 21 23
241 text => $word
242 }
243 );
244 23 $phrase->add($term);
245 }
246 11 ▶ if (exists $self->{slop}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"slop"}) 2 9 11
247 2 $phrase->slop($self->{slop});
248 }
249 11 $self->set_boost($phrase);
250 11 return $phrase;
251 }
252  
253 package Plucene::QueryParser::Subquery;
254  
255 10 ▶sub to_plucene {
Subroutine to_plucene: called CC=2 SCAR=7.2
256 10 my ($self, $field) = @_;
257 $self->{subquery}
258 10 ▶ ->to_plucene(exists $self->{field} ? $self->{field} : $field);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'field'} ? : 0 10 10
259 }
260  
261 package Plucene::QueryParser::Prefix;
262 15 our @ISA = qw(Plucene::QueryParser::Term);
263  
264 4 ▶sub to_plucene {
Subroutine to_plucene: called CC=1 SCAR=0.0
265 4 require Plucene::Search::PrefixQuery;
266 4 my ($self, $field) = @_;
267 4 $self->set_term($field);
268 4 my $q = Plucene::Search::PrefixQuery->new({ prefix => $self->{pl_term} });
269 4 $self->set_boost($q);
270 4 return $q;
271 }
272  
273 15 1;
↑ Summary Plucene/Index/SegmentMerger.pm »