File Coverage

blib/lib/Plucene/Search/TermQuery.pm
Criterion Covered Total %
statement 37 37 100.0
branch 6 8 75.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Plucene::Search::TermQuery;
2              
3             =head1 NAME
4              
5             Plucene::Search::TermQuery - a query that contains a term
6              
7             =head1 SYNOPSIS
8              
9             # isa Plucene::Search::Query
10              
11             $term_query->normalize($norm);
12              
13             my $ssw = $term_query->sum_squared_weights($searcher);
14             my $as_string = $term_query->as_string($field);
15              
16             =head1 DESCRIPTION
17              
18             A query that matches a document containing a term.
19              
20             Term query are the simplest possible Plucene queries and are used to match a
21             single word. Term queries are represented by instances of the TermQuery class
22             and contain the desired term (word) and a field name, both are case sensitive.
23              
24             The field specified in a Term query must be a document field that was specified
25             as 'indexible' during the indexing process. If the field was specified during
26             indexing as 'tokenized' than the term will be matched against each of tokens
27             (words) found in that field, otherwise, it will be matched against the entire
28             content of that field.
29              
30             A term query may have an optional boost factor (default = 1.0) that allows to
31             increase or decrease the ranking of documents it matches.
32              
33             =head1 METHODS
34              
35             =cut
36              
37 11     11   788 use strict;
  11         25  
  11         443  
38 11     11   60 use warnings;
  11         27  
  11         348  
39              
40 11     11   630 use Plucene::Index::Reader;
  11         23  
  11         441  
41 11     11   879 use Plucene::Search::Similarity;
  11         25  
  11         249  
42 11     11   8122 use Plucene::Search::TermScorer;
  11         47  
  11         137  
43              
44 11     11   397 use base 'Plucene::Search::Query';
  11         21  
  11         4548  
45              
46             =head2 term / idf / weight
47              
48             Get / set these attributes
49              
50             =cut
51              
52             __PACKAGE__->mk_accessors(qw(term idf weight));
53              
54             =head2 sum_squared_weights
55              
56             my $ssw = $term_query->sum_squared_weights($searcher);
57              
58             This will return the sum squared weights for the passed in searcher.
59              
60             =cut
61              
62             sub sum_squared_weights {
63 185     185 1 646 my ($self, $searcher) = @_;
64 185         818 $self->idf(Plucene::Search::Similarity->idf($self->term, $searcher));
65 185         1898 $self->weight($self->idf * $self->boost);
66 185         4287 return $self->{weight}**2;
67             }
68              
69             =head2 normalize
70              
71             $term_query->normalize($norm);
72              
73             =cut
74              
75             sub normalize {
76 185     185 1 696 my ($self, $norm) = @_;
77 185         433 $self->{weight} *= $norm;
78 185         687 $self->{weight} *= $self->{idf};
79             }
80              
81             sub _scorer {
82 189     189   556 my ($self, $reader) = @_;
83 189         862 my $term_docs = $reader->term_docs($self->term);
84 189 50       752 return unless $term_docs;
85 189         908 my $norms = $reader->norms($self->term->field);
86 189 50       1922 return unless $norms;
87 189         2708 return Plucene::Search::TermScorer->new({
88             term_docs => $term_docs,
89             norms => $norms,
90             weight => $self->{weight} });
91             }
92              
93             =head2 to_string
94              
95             my $as_string = $term_query->as_string($field);
96              
97             =cut
98              
99             sub to_string {
100 44     44 1 6454 my ($self, $field) = @_;
101 44         53 my $rv = "";
102 44 100       142 $rv = $self->term->field . ":" if $field ne $self->term->field;
103 44         565 $rv .= $self->term->text;
104 44 100       300 $rv .= "^" . $self->boost unless $self->boost == 1;
105 44         276 return $rv;
106             }
107              
108             1;