Plucene/Search/BooleanQuery.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 71.4% 10 / 14 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 90.7% 88 / 97 CC 13 SCAR 11.5
CC
13
Cov
91%
_scorer
6.6
to_string
4.0
add
3.2
CRAP 3.2
« Plucene/Search/BooleanScorer.pm ↑ Summary Plucene/Search/IndexSearcher.pm »
1 package Plucene::Search::BooleanQuery;
2  
3 =head1 NAME
4  
5 Plucene::Search::BooleanQuery - a boolean query
6  
7 =head1 SYNOPSIS
8  
9 # isa Plucene::Search::Query
10  
11 $query->add(Plucene::Search::Query $query, $required, $prohibited);
12 $query->normalize($norm);
13
14 my @clauses = $query->clauses;
15 my $sum_sq_weights = $query->sum_squared_weights($searcher);
16 my $as_string = $query->to_string($field);
17
18 =head1 DESCRIPTION
19  
20 A query that matches documents matching boolean combinations of
21 other queries, typically TermQuerys or PhraseQuery
22  
23 A boolean query represents a composite query that may contains subqueries
24 of arbitrary nesting level and with composition rules such as 'and',
25 'or' or 'not'.
26  
27 Boolean queries are represented in Plucene API by instances of the
28 BooleanQuery class. Each BooleanQuery object contains a list of subqueries
29 that are linked using instances of the adaptor class BooleanClause. The
30 subqueries may be of any Query type such as term query, phrase query and
31 nested boolean queries.
32  
33 Each sub query of a boolean query has two binary qualifiers that controls
34 how its super query is matched. These qualifiers are
35  
36 =over 4
37  
38 =item * prohibited - when this flag is set, the matching status of the
39 subquery is negated such that the query is considered as a match only
40 when the sub query does not match.
41
42 =item * required - when this flag is set, the sub query is required to match
43 (or not to match if its 'prohibited' flag is set) for the super query
44 to match. This this is a necessary but not sufficient condition for the
45 super query to match.
46  
47 =back
48  
49 =head1 METHODS
50  
51 =cut
52  
53 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 3 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 3 ▶use Plucene::Search::BooleanClause;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use Plucene::Search::BooleanScorer;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 3 ▶use base "Plucene::Search::Query";
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 3 __PACKAGE__->mk_accessors(qw(clauses));
64  
65 =head2 add
66  
67 $query->add(Plucene::Search::Query $query, $required, $prohibited);
68  
69 Adds a clause to a boolean query. Clauses may be:
70  
71 =over
72  
73 =item required
74  
75 which means that documents which I<do not> match this sub-query will
76 I<not> match the boolean query;
77  
78 =item prohibited
79  
80 which means that documents which I<do> match this sub-query will I<not> match
81 the boolean query; or
82  
83 =item
84  
85 neither, in which case matched documents are neither prohibited from
86 nor required to match the sub-query.
87  
88 =back
89  
90 It is an error to specify a clause as both required and prohibited.
91  
92 =cut
93  
94 9 ▶sub add {
Subroutine add: called CC=3 SCAR=11.7
Pod documented
95 9 my ($self, $query, $required, $prohibited) = @_;
96 9 ▶ push @{ $self->{clauses} },
Condition50%2/4
$required || 0
l!l
09
$prohibited || 0
l!l
09
MC/DC0%0/1
$required || 0
$required
MC/DC0%0/1
$prohibited || 0
$prohibited
Truth table50%1/2
A
A= $required
Aresult
11
00
Truth table50%1/2
A
A= $prohibited
Aresult
11
00
97 Plucene::Search::BooleanClause->new({
98 query => $query,
99 required => ($required || 0),
100 prohibited => ($prohibited || 0) });
101 }
102  
103 =head2 add_clause
104  
105 $self->add_clause(Plucene::Search::BooleanClause $c);
106
107 Adds an already-formed clause onto the query.
108  
109 =cut
110  
111 81 ▶sub add_clause {
Subroutine add_clause: called CC=1 SCAR=0.0
Pod documented
112 81 my ($self, $clause) = @_;
113 81 push @{ $self->{clauses} }, $clause;
114 }
115  
116 =head2 clauses
117  
118 my @clauses = $query->clauses;
119  
120 =cut
121  
122 139 ▶sub clauses { @{ shift->{clauses} } }
Subroutine clauses: called CC=1 SCAR=0.0
Pod documented
123  
124 21 ▶sub prepare {
Subroutine prepare: called CC=2 SCAR=6.9
Pod documented
125 21 my ($self, $reader) = @_;
126 21 $_->query->prepare($reader) for $self->clauses;
127 }
128  
129 =head2 sum_squared_weights
130  
131 my $sum_sq_weights = $query->sum_squared_weights($searcher);
132  
133 =cut
134  
135 24 ▶sub sum_squared_weights {
Subroutine sum_squared_weights: called CC=1 SCAR=0.0
Pod documented
136 24 my ($self, $searcher) = @_;
137 24 sum map $_->query->sum_squared_weights($searcher), grep !$_->prohibited,
138 $self->clauses;
139 }
140  
141 =head2 normalize
142  
143 $query->normalize($norm);
144  
145 =cut
146  
147 24 ▶sub normalize {
Subroutine normalize: called CC=2 SCAR=6.9
Pod documented
148 24 my ($self, $norm) = @_;
149 24 $_->query->normalize($norm) for grep !$_->prohibited, $self->clauses;
150 }
151  
152 24 ▶sub _scorer {
Subroutine _scorer: called CC=6 SCAR=18.8
153 24 my ($self, $reader) = @_;
154 24 my @clauses = $self->clauses;
155 24 ▶ if (@clauses == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@clauses == 1) 1 23 24
156 1 my $c = $clauses[0];
157 1 ▶ return $c->query->_scorer($reader) unless $c->prohibited;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->prohibited 0 1 1
158 }
159  
160 24 my $result = Plucene::Search::BooleanScorer->new();
161 24 for my $c ($self->clauses) {
162 50 my $subscorer = $c->query->_scorer($reader);
163 50 ▶ if ($subscorer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subscorer) { } 50 0 50
164 50 $result->add($subscorer, $c->required, $c->prohibited);
165 } else {
166  
167 # If it was required and we didn't score, kill it.
168 0 ▶ return if $c->required;
Branch: 1 branch
BranchTrueFalseTotal
if $c->required 0 0 0
169 }
170 }
171  
172 24 return $result;
173 }
174  
175 =head2 to_string
176  
177 my $as_string = $query->to_string($field);
178  
179 =cut
180  
181 22 ▶sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
182 22 my ($self, $field) = @_;
183 join " ", map {
184 22 my $buffer;
185 44 ▶ $buffer .= "-" if $_->prohibited;
Branch: 1 branch
BranchTrueFalseTotal
if $_->prohibited 3 41 44
186 44 ▶ $buffer .= "+" if $_->required;
Branch: 1 branch
BranchTrueFalseTotal
if $_->required 23 21 44
187 44 my $q = $_->query;
188 44 ▶ if ($q->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q->isa(__PACKAGE__)) { } 4 40 44
189 4 $buffer .= "(" . $q->to_string($field) . ")";
190 } else {
191 40 $buffer .= $q->to_string($field);
192 }
193 44 $buffer;
194 } $self->clauses;
195 }
196  
197 3 1;
« Plucene/Search/BooleanScorer.pm ↑ Summary Plucene/Search/IndexSearcher.pm »