Plucene/Search/PhraseQuery.pm

filter: Statementstmt 93.0% 67 / 72 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 89.2% 100 / 112 CC 13 SCAR 13.3
CC
13
Cov
87%
_scorer
8.8
to_string
4.0
add
3.1
CRAP 3.8
« Plucene/Index/TermInfosWriter.pm ↑ Summary Plucene/Index/SegmentTermDocs.pm »
1 package Plucene::Search::PhraseQuery;
2  
3 =head1 NAME
4  
5 Plucene::Search::PhraseQuery - A query that matchs a phrase
6  
7 =head1 SYNOPSIS
8  
9 # isa Plucene::Search::Query
10  
11 =head1 DESCRIPTION
12  
13 A Query that matches documents containing a particular sequence of terms.
14  
15 A phrase query represents a query that is matched against a consecutive
16 sequence of terms in the field. For example, the phrase query 'winding road'
17 should match 'winding road' but not 'road winding' (with the exception of
18 more relaxed slop factors).
19  
20 Phrase queries are represented in Plucene's API by instances of the
21 PharseQuery class. These instances contain an ordered list of Term objects
22 that represent the terms to match. For obvious reasons, all terms in a
23 PhraseQuery must refer to the same field.
24  
25 A phrase query may have an optional boost factor and an optional slop
26 parameter (default = 0). The slop parameter can be used to relax the phrase
27 matching by accepting somewhat out of order sequences of the terms.
28  
29 =head1 METHODS
30  
31 =cut
32  
33 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 ▶use Plucene::Search::Similarity;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Plucene::Search::TermQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Plucene::Search::TermScorer;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Plucene::Search::PhraseScorer::Exact;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Plucene::Search::PhraseScorer::Sloppy;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 4 ▶use base 'Plucene::Search::Query';
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 __PACKAGE__->mk_accessors(qw(slop terms field idf weight));
47  
48 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 11 my $self = shift->SUPER::new(@_);
50 11 $self->slop(0);
51 11 $self->terms([]);
52 11 $self;
53 }
54  
55 =head2 add
56  
57 Adds a term to the end of the query phrase.
58  
59 =cut
60  
61 23 ▶sub add {
Subroutine add: called CC=3 SCAR=11.2
Pod documented
62 23 my ($self, $term) = @_;
63 23 ▶ if (@{ $self->terms } == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->terms;} == 0) { } 11 12 23
elsif ($self->field ne $term->field) { } 0 12 12
64 11 $self->field($term->field);
65 } elsif ($self->field ne $term->field) {
66 0 carp "All terms in this phrase should be in the same field: "
67 . $self->field;
68 }
69 23 push @{ $self->terms }, $term;
70 }
71  
72 =head2 sum_squared_weights
73  
74 The sum squared weights of this query.
75  
76 =cut
77  
78 7 ▶sub sum_squared_weights {
Subroutine sum_squared_weights: called CC=2 SCAR=6.9
Pod documented
79 7 my ($self, $searcher) = @_;
80 $self->{idf} += Plucene::Search::Similarity->idf($_, $searcher)
81 7 for @{ $self->terms };
82 7 $self->{weight} = $self->idf * $self->boost;
83 7 $self->boost * $self->boost;
84 }
85  
86 =head2 normalize
87  
88 Normalize the query.
89  
90 =cut
91  
92 7 ▶sub normalize {
Subroutine normalize: called CC=1 SCAR=0.0
Pod documented
93 7 my ($self, $norm) = @_;
94 7 $self->{weight} *= $norm * $self->idf;
95 }
96  
97 7 ▶sub _scorer {
Subroutine _scorer: called CC=7 SCAR=21.8
98 7 my ($self, $reader) = @_;
99 7 ▶ return unless @{ $self->{terms} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"terms"};} 0 7 7
100 7 ▶ if (@{ $self->{terms} } == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"terms"};} == 1) 0 7 7
101 0 my $term = $self->{terms}->[0];
102 0 my $docs = $reader->term_docs($term);
103 0 ▶ return unless $docs;
Branch: 1 branch
BranchTrueFalseTotal
unless $docs 0 0 0
104 0 return Plucene::Search::TermScorer->new({
105 term_docs => $docs,
106 norms => $reader->norms($term->field),
107 weight => $self->weight
108 });
109 }
110  
111 7 my @tps;
112 7 for my $term (@{ $self->terms }) {
113 15 my $tp = $reader->term_positions($term);
114 15 ▶ return unless $tp;
Branch: 1 branch
BranchTrueFalseTotal
unless $tp 0 15 15
115 15 push @tps, $tp;
116 }
117  
118 7 ▶ my $class =
Branch: 1 branch
BranchTrueFalseTotal
$self->slop == 0 ? : 4 3 7
119 "Plucene::Search::PhraseScorer::"
120 . (($self->slop == 0) ? "Exact" : "Sloppy");
121 7 $class->new({
122 tps => \@tps,
123 norms => $reader->norms($self->field),
124 weight => $self->weight,
125 slop => $self->slop
126 });
127 }
128  
129 =head2 to_string
130  
131 Prints a user-readable version of this query.
132  
133 =cut
134  
135 6 ▶sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
136 6 my ($self, $field) = @_;
137 6 my $buffer = "";
138 6 ▶ $buffer = $self->field . ":" if $field ne $self->field;
Branch: 1 branch
BranchTrueFalseTotal
if $field ne $self->field 5 1 6
139 6 $buffer .= sprintf('"%s"', join(" ", map $_->text, @{ $self->terms }));
140 6 ▶ $buffer .= "~" . $self->slop if $self->slop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->slop 1 5 6
141 6 ▶ $buffer .= "^" . $self->boost if $self->boost != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->boost != 1 0 6 6
142 6 $buffer;
143 }
144  
145 4 1;
« Plucene/Index/TermInfosWriter.pm ↑ Summary Plucene/Index/SegmentTermDocs.pm »