blib/lib/KinoSearch1/QueryParser/QueryParser.pm

filter: Statementstmt 99.2% 134 / 135 Branchbran 85.0% 51 / 60 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 92.6% 213 / 230 CC 38 SCAR 24.5
CC
38
Cov
94%
parse
23.1
init_instance
7.4
_get_field_query
4.0
CRAP 11.6
« blib/lib/KinoSearch1/Search/SearchServer.pm ↑ Summary blib/lib/KinoSearch1/Store/FSInvIndex.pm »
1 package KinoSearch1::QueryParser::QueryParser;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use KinoSearch1::Util::ToolSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use base qw( KinoSearch1::Util::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 __PACKAGE__->init_instance_vars(
9 # constructor args / members
10 analyzer => undef,
11 default_boolop => 'OR',
12 default_field => undef, # back compat
13 fields => undef,
14 # members
15 bool_groups => undef,
16 phrases => undef,
17 bool_group_re => undef,
18 phrase_re => undef,
19 label_inc => 0,
20 );
21 }
22  
23 18 ▶use KinoSearch1::Analysis::TokenBatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use KinoSearch1::Analysis::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use KinoSearch1::Search::BooleanQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use KinoSearch1::Search::PhraseQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use KinoSearch1::Search::TermQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶use KinoSearch1::Index::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 45 ▶sub init_instance {
Subroutine init_instance: called CC=7 SCAR=20.0
Pod documented
31 45 my $self = shift;
32 45 $self->{bool_groups} = {};
33 45 $self->{phrases} = {};
34  
35 croak("default_boolop must be either 'AND' or 'OR'")
36 45 ▶ unless $self->{default_boolop} =~ /^(?:AND|OR)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"default_boolop"} =~ /^(?:AND|OR)$/ 0 45 45
37  
38 # create a random string that presumably won't appear in a search string
39 45 my @chars = ( 'A' .. 'Z' );
40 45 my $randstring = '';
41 45 $randstring .= $chars[ rand @chars ] for ( 1 .. 16 );
42 45 $self->{randstring} = $randstring;
43  
44 # create labels which won't appear in search strings
45 45 $self->{phrase_re} = qr/^(_phrase$randstring\d+)/;
46 45 $self->{bool_group_re} = qr/^(_boolgroup$randstring\d+)/;
47  
48 # verify fields param
49 my $fields
50 = defined $self->{fields}
51 ? $self->{fields}
52 45 ▶ : [ $self->{default_field} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'fields'} ? : 41 4 45
53 45 ▶ croak("Required parameter 'fields' not supplied as arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields and builtin::reftype($fields) eq "ARRAY" 0 45 45
Condition33%1/3
defined $fields and builtin::reftype($fields) eq "ARRAY"
!ll&&!rl&&r
0045
MC/DC0%0/2
defined $fields and builtin::reftype($fields) eq "ARRAY"
defined $fields builtin::reftype($fields) eq "ARRAY"
Truth table33%1/3
A and B
A= defined $fieldsB= builtin::reftype($fields) eq "ARRAY"
ABresult
0X0
100
111
54 unless ( defined $fields
55 and reftype($fields) eq 'ARRAY' );
56 45 $self->{fields} = $fields;
57  
58 # verify analyzer
59 croak("Missing required param 'analyzer'")
60 unless a_isa_b( $self->{analyzer},
61 45 ▶ 'KinoSearch1::Analysis::Analyzer' );
Branch: 1 branch
BranchTrueFalseTotal
unless a_isa_b($self->{"analyzer"}, "KinoSearch1::Analysis::Analyzer") 0 45 45
62 }
63  
64 # regex matching a quoted string
65 18 my $quoted_re = qr/
66 " # opening quote
67 ( # capture
68 [^"]*? # anything not a quote
69 )
70 (?:"|$) # closed by either a quote or end of string
71 /xsm;
72  
73 # regex matching a parenthetical group
74 18 my $paren_re = qr/
75 \( # opening paren
76 ( # capture
77 [^()]*? # anything not a paren
78 )
79 (?:\)|$) # closed by paren or end of string
80 /xsm;
81  
82 # regex matching a negating boolean operator
83 18 my $neg_re = qr/^(?:
84 NOT\s+ # NOT followed by space
85 |-(?=\S) # minus followed by something not-spacey
86 )/xsm;
87  
88 # regex matching a requiring boolean operator
89 18 my $req_re = qr/^
90 \+(?=\S) # plus followed by something not-spacey
91 /xsm;
92  
93 # regex matching a field indicator
94 18 my $field_re = qr/^
95 ( # capture
96 [^"(:\s]+ # non-spacey string
97 )
98 : # followed by :
99 /xsm;
100  
101 346 ▶sub parse {
Subroutine parse: called CC=23 SCAR=31.4
Pod documented
102 346 my ( $self, $qstring_orig, $default_fields ) = @_;
103 346 ▶ $qstring_orig = '' unless defined $qstring_orig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qstring_orig 0 346 346
104 346 ▶ $default_fields ||= $self->{fields};
Condition66%2/3
$default_fields ||= $self->{"fields"}
l!l&&r!l&&!r
832630
MC/DC0%0/2
$default_fields ||= $self->{"fields"}
$default_fields $self->{"fields"}
Truth table66%2/3
A ||= B
A= $default_fieldsB= $self->{"fields"}
ABresult
1X1
011
000
105 346 my $default_boolop = $self->{default_boolop};
106 346 my @clauses;
107  
108 # substitute contiguous labels for phrases and boolean groups
109 346 my $qstring = $self->_extract_phrases($qstring_orig);
110 346 $qstring = $self->_extract_boolgroups($qstring);
111  
112 346 local $_ = $qstring;
113 346 ▶ while ( bytes::length $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&bytes::length($_)) 965 346 1311
114 # fast-forward past whitespace
115 965 ▶ next if s/^\s+//;
Branch: 1 branch
BranchTrueFalseTotal
if s/^\s+// 391 574 965
116  
117 574 ▶ my $occur = $default_boolop eq 'AND' ? 'MUST' : 'SHOULD';
Branch: 1 branch
BranchTrueFalseTotal
$default_boolop eq 'AND' ? : 260 314 574
118  
119 574 ▶ if (s/^AND\s+//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^AND\s+//) { } 40 534 574
elsif (s/^OR\s+//) { } 42 492 534
120 40 ▶ if (@clauses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@clauses) 30 10 40
121 # require the previous clause (unless it's negated)
122 30 ▶ if ( $clauses[-1]{occur} eq 'SHOULD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clauses[-1]{"occur"} eq "SHOULD") 17 13 30
123 17 $clauses[-1]{occur} = 'MUST';
124 }
125 }
126 # require this clause
127 40 $occur = 'MUST';
128 }
129 elsif (s/^OR\s+//) {
130 42 ▶ if (@clauses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@clauses) 38 4 42
131 38 $clauses[-1]{occur} = 'SHOULD';
132 }
133 42 $occur = 'SHOULD';
134 }
135  
136 # detect tokens which cause this clause to be required or negated
137 574 ▶ if (s/$neg_re//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/$neg_re//) { } 55 519 574
elsif (s/$req_re//) { } 41 478 519
138 55 $occur = 'MUST_NOT';
139 }
140 elsif (s/$req_re//) {
141 41 $occur = 'MUST';
142 }
143  
144 # set the field
145 574 ▶ my $fields = s/^$field_re// ? [$1] : $default_fields;
Branch: 1 branch
BranchTrueFalseTotal
s/^$field_re// ? : 32 542 574
146  
147 # if a phrase label is detected...
148 574 ▶ if (s/$self->{phrase_re}//) {
Branch: 3 branches
BranchTrueFalseTotal
if (s/$self->{'phrase_re'}//) { } 95 479 574
elsif (s/$self->{'bool_group_re'}//) { } 83 396 479
elsif (s/([^"(\s]+)//) { } 396 0 396
149 95 my $query;
150  
151 # retreive the text and analyze it
152 95 my $orig_phrase_text = delete $self->{phrases}{$1};
153 95 my $token_texts = $self->_analyze($orig_phrase_text);
154 95 ▶ if (@$token_texts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$token_texts) 95 0 95
155 95 my $query = $self->_get_field_query( $fields, $token_texts );
156 95 ▶ push @clauses, { query => $query, occur => $occur }
Branch: 1 branch
BranchTrueFalseTotal
if defined $query 95 0 95
157 if defined $query;
158 }
159 }
160 # if a label indicating a bool group is detected...
161 elsif (s/$self->{bool_group_re}//) {
162 # parse boolean subqueries recursively
163 83 my $inner_text = delete $self->{bool_groups}{$1};
164 83 my $query = $self->parse( $inner_text, $fields );
165 83 push @clauses, { query => $query, occur => $occur };
166 }
167 # what's left is probably a term
168 elsif (s/([^"(\s]+)//) {
169 396 my $token_texts = $self->_analyze($1);
170 396 @$token_texts = grep { $_ ne '' } @$token_texts;
171 396 ▶ if (@$token_texts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$token_texts) 354 42 396
172 354 my $query = $self->_get_field_query( $fields, $token_texts );
173 354 push @clauses, { occur => $occur, query => $query };
174 }
175 }
176 }
177  
178 346 ▶ if ( @clauses == 1 and $clauses[0]{occur} ne 'MUST_NOT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@clauses == 1 and $clauses[0]{'occur'} ne 'MUST_NOT') { } 146 200 346
Condition100%3/3
@clauses == 1 and $clauses[0]{"occur"} ne "MUST_NOT"
!ll&&!rl&&r
18416146
MC/DC100%2/2
@clauses == 1 and $clauses[0]{"occur"} ne "MUST_NOT"
@clauses == 1 $clauses[0]{"occur"} ne "MUST_NOT"
Truth table100%3/3
A and B
A= @clauses == 1B= $clauses[0]{"occur"} ne "MUST_NOT"
ABresult
0X0
100
111
179 # if it's just a simple query, return it unwrapped
180 146 return $clauses[0]{query};
181 }
182 else {
183 # otherwise, build a boolean query
184 200 my $bool_query = KinoSearch1::Search::BooleanQuery->new;
185 200 for my $clause (@clauses) {
186 $bool_query->add_clause(
187 query => $clause->{query},
188 occur => $clause->{occur},
189 386 );
190 }
191 200 return $bool_query;
192 }
193 }
194  
195 # Wrap a TermQuery/PhraseQuery to deal with multiple fields.
196 449 ▶sub _get_field_query {
Subroutine _get_field_query: called CC=4 SCAR=14.0
197 449 my ( $self, $fields, $token_texts ) = @_;
198  
199 526 my @queries = grep { defined $_ }
200 449 map { $self->_gen_single_field_query( $_, $token_texts ) } @$fields;
201  
202 449 ▶ if ( @queries == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@queries == 0) { } 0 449 449
elsif (@queries == 1) { } 408 41 449
203 0 return;
204 }
205 elsif ( @queries == 1 ) {
206 408 return $queries[0];
207 }
208 else {
209 41 my $wrapper_query = KinoSearch1::Search::BooleanQuery->new;
210 41 for my $query (@queries) {
211 118 $wrapper_query->add_clause(
212 query => $query,
213 occur => 'SHOULD',
214 );
215 }
216 41 return $wrapper_query;
217 }
218 }
219  
220 # Create a TermQuery, a PhraseQuery, or nothing.
221 526 ▶sub _gen_single_field_query {
Subroutine _gen_single_field_query: called CC=4 SCAR=13.9
222 526 my ( $self, $field, $token_texts ) = @_;
223  
224 526 ▶ if ( @$token_texts == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$token_texts == 1) { } 462 64 526
elsif (@$token_texts > 1) { } 64 0 64
225 462 my $term = KinoSearch1::Index::Term->new( $field, $token_texts->[0] );
226 462 return KinoSearch1::Search::TermQuery->new( term => $term );
227 }
228 elsif ( @$token_texts > 1 ) {
229 64 my $phrase_query = KinoSearch1::Search::PhraseQuery->new;
230 64 for my $token_text (@$token_texts) {
231 134 $phrase_query->add_term(
232 KinoSearch1::Index::Term->new( $field, $token_text ),
233 );
234 }
235 64 return $phrase_query;
236 }
237 }
238  
239 # break a string into tokens
240 491 ▶sub _analyze {
Subroutine _analyze: called CC=2 SCAR=6.9
241 491 my ( $self, $string ) = @_;
242  
243 491 my $token_batch = KinoSearch1::Analysis::TokenBatch->new;
244 491 $token_batch->append( $string, 0, bytes::length($string) );
245 491 $token_batch = $self->{analyzer}->analyze($token_batch);
246 491 my @token_texts;
247 491 ▶ while ( $token_batch->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_batch->next) 559 491 1050
248 559 push @token_texts, $token_batch->get_text;
249 }
250 491 return \@token_texts;
251 }
252  
253 # replace all phrases with labels
254 346 ▶sub _extract_phrases {
Subroutine _extract_phrases: called CC=2 SCAR=6.9
255 346 my ( $self, $qstring ) = @_;
256  
257 346 ▶ while ( $qstring =~ $quoted_re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qstring =~ /$quoted_re/) 95 346 441
258 my $label
259 95 = sprintf( "_phrase$self->{randstring}%d", $self->{label_inc}++ );
260 95 $qstring =~ s/$quoted_re/$label /; # extra space for safety
261  
262 # store the phrase text for later retrieval
263 95 $self->{phrases}{$label} = $1;
264 }
265  
266 346 return $qstring;
267 }
268  
269 # recursively replace boolean groupings with labels, innermost first
270 346 ▶sub _extract_boolgroups {
Subroutine _extract_boolgroups: called CC=2 SCAR=6.9
271 346 my ( $self, $qstring ) = @_;
272  
273 346 ▶ while ( $qstring =~ $paren_re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qstring =~ /$paren_re/) 83 346 429
274 my $label = sprintf( "_boolgroup$self->{randstring}%d",
275 83 $self->{label_inc}++ );
276 83 $qstring =~ s/$paren_re/$label /; # extra space for safety
277  
278 # store the text for later retrieval
279 83 $self->{bool_groups}{$label} = $1;
280 }
281  
282 346 return $qstring;
283 }
284  
285 18 1;
286  
287 __END__
« blib/lib/KinoSearch1/Search/SearchServer.pm ↑ Summary blib/lib/KinoSearch1/Store/FSInvIndex.pm »