File Coverage

blib/lib/Plucene/Search/PrefixQuery.pm
Criterion Covered Total %
statement 38 38 100.0
branch 6 8 75.0
condition 4 6 66.6
subroutine 11 11 100.0
pod 4 4 100.0
total 63 67 94.0


line stmt bran cond sub pod time code
1             package Plucene::Search::PrefixQuery;
2              
3             =head1 NAME
4              
5             Plucene::Search::TermQuery - a query that matches terms beginning with a string
6              
7             =head1 SYNOPSIS
8              
9             # isa Plucene::Search::Query
10              
11             $prefix_query->normalize($norm);
12              
13             my $ssw = $prefix_query->sum_squared_weights($searcher);
14             my $as_string = $prefix_query->to_string($field);
15              
16             =head1 DESCRIPTION
17              
18             A query that matches a document containing terms I with the
19             given string.
20              
21             =cut
22              
23 2     2   12 use strict;
  2         4  
  2         102  
24 2     2   13 use warnings;
  2         6  
  2         82  
25 2     2   11 use base 'Plucene::Search::Query';
  2         5  
  2         230  
26 2     2   14 use Plucene::Search::BooleanQuery;
  2         5  
  2         37  
27 2     2   58 use Plucene::Search::TermQuery;
  2         4  
  2         18  
28              
29             __PACKAGE__->mk_accessors(qw/ prefix reader /);
30              
31 1     1 1 7 sub prepare { $_[0]->reader($_[1]) }
32              
33             # This returns the underlying boolean query.
34              
35             sub _query {
36 3     3   8 my $self = shift;
37 3 100       27 return $self->{query} if exists $self->{query};
38 1         9 my $q = new Plucene::Search::BooleanQuery;
39 1         16 my $prefix = $self->prefix;
40 1         10 my $enum = $self->reader->terms($prefix);
41 1         6 my ($field, $text) = ($prefix->field, $prefix->text);
42 1         11 do {
43 4         16 my $term = $enum->term;
44             goto DONE
45 4 100 66     21 unless $term
      66        
46             and $term->text =~ /^\Q$text/
47             and $term->field eq $field;
48 3         107 my $tq = Plucene::Search::TermQuery->new({ term => $term });
49 3         54 $tq->boost($self->boost);
50 3         42 $q->add($tq, 0, 0);
51             } while $enum->next;
52 1         41 DONE: $self->{query} = $q;
53             }
54              
55             =head2 to_string
56              
57             $q->to_string
58              
59             Convert the query to a readable string format
60              
61             =head2 sum_squared_weights
62              
63             The sum sqaured weights of the query.
64              
65             =head2 normalize
66              
67             Normalize the query.
68              
69             =cut
70              
71             sub to_string {
72 1     1 1 4570 my ($self, $field) = @_;
73 1         3 my $s = "";
74 1 50       6 $s = $self->prefix->field . ":" if $self->prefix->field ne $field;
75 1         32 $s .= $self->prefix->text . "*";
76 1 50       15 $s .= "^" . $self->boost unless $self->boost == 1;
77 1         12 $s;
78             }
79              
80 1     1 1 5 sub sum_squared_weights { shift->_query->sum_squared_weights(@_) }
81 1     1 1 7 sub normalize { shift->_query->normalize(@_) }
82 1     1   4 sub _scorer { shift->_query->_scorer(@_) }
83              
84             1;