| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | 11 | ▶ | use Plucene::Index::Reader; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 11 | ▶ | use Plucene::Search::Similarity; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 11 | ▶ | use Plucene::Search::TermScorer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 11 | ▶ | use base 'Plucene::Search::Query'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | =head2 term / idf / weight | ||||||||||
| 47 | |||||||||||
| 48 | Get / set these attributes | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 11 | __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 | 207 | ▶ | sub sum_squared_weights { | ||||||||
|
Subroutine
sum_squared_weights: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | 207 | my ($self, $searcher) = @_; | |||||||||
| 64 | 207 | $self->idf(Plucene::Search::Similarity->idf($self->term, $searcher)); | |||||||||
| 65 | 207 | $self->weight($self->idf * $self->boost); | |||||||||
| 66 | 207 | return $self->{weight}**2; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =head2 normalize | ||||||||||
| 70 | |||||||||||
| 71 | $term_query->normalize($norm); | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 207 | ▶ | sub normalize { | ||||||||
|
Subroutine
normalize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 76 | 207 | my ($self, $norm) = @_; | |||||||||
| 77 | 207 | $self->{weight} *= $norm; | |||||||||
| 78 | 207 | $self->{weight} *= $self->{idf}; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 211 | ▶ | sub _scorer { | ||||||||
|
Subroutine
_scorer: called CC=3 SCAR=11.2
| |||||||||||
| 82 | 211 | my ($self, $reader) = @_; | |||||||||
| 83 | 211 | my $term_docs = $reader->term_docs($self->term); | |||||||||
| 84 | 211 | ▶ | return unless $term_docs; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 211 | my $norms = $reader->norms($self->term->field); | |||||||||
| 86 | 211 | ▶ | return unless $norms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | return Plucene::Search::TermScorer->new({ | ||||||||||
| 88 | term_docs => $term_docs, | ||||||||||
| 89 | norms => $norms, | ||||||||||
| 90 | 211 | weight => $self->{weight} }); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | =head2 to_string | ||||||||||
| 94 | |||||||||||
| 95 | my $as_string = $term_query->as_string($field); | ||||||||||
| 96 | |||||||||||
| 97 | =cut | ||||||||||
| 98 | |||||||||||
| 99 | 44 | ▶ | sub to_string { | ||||||||
|
Subroutine
to_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 100 | 44 | my ($self, $field) = @_; | |||||||||
| 101 | 44 | my $rv = ""; | |||||||||
| 102 | 44 | ▶ | $rv = $self->term->field . ":" if $field ne $self->term->field; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 44 | $rv .= $self->term->text; | |||||||||
| 104 | 44 | ▶ | $rv .= "^" . $self->boost unless $self->boost == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 44 | return $rv; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 11 | 1; | |||||||||