Plucene::Search::TermQuery - a query that contains a term


Plucene documentation Contained in the Plucene distribution.

Index


Code Index:

NAME

Top

Plucene::Search::TermQuery - a query that contains a term

SYNOPSIS

Top

	# isa Plucene::Search::Query

	$term_query->normalize($norm);

	my       $ssw = $term_query->sum_squared_weights($searcher);
	my $as_string = $term_query->as_string($field);

DESCRIPTION

Top

A query that matches a document containing a term.

Term query are the simplest possible Plucene queries and are used to match a single word. Term queries are represented by instances of the TermQuery class and contain the desired term (word) and a field name, both are case sensitive.

The field specified in a Term query must be a document field that was specified as 'indexible' during the indexing process. If the field was specified during indexing as 'tokenized' than the term will be matched against each of tokens (words) found in that field, otherwise, it will be matched against the entire content of that field.

A term query may have an optional boost factor (default = 1.0) that allows to increase or decrease the ranking of documents it matches.

METHODS

Top

term / idf / weight

Get / set these attributes

sum_squared_weights

	my $ssw = $term_query->sum_squared_weights($searcher);

This will return the sum squared weights for the passed in searcher.

normalize

	$term_query->normalize($norm);

to_string

	my $as_string = $term_query->as_string($field);


Plucene documentation Contained in the Plucene distribution.
package Plucene::Search::TermQuery;

use strict;
use warnings;

use Plucene::Index::Reader;
use Plucene::Search::Similarity;
use Plucene::Search::TermScorer;

use base 'Plucene::Search::Query';

__PACKAGE__->mk_accessors(qw(term idf weight));

sub sum_squared_weights {
	my ($self, $searcher) = @_;
	$self->idf(Plucene::Search::Similarity->idf($self->term, $searcher));
	$self->weight($self->idf * $self->boost);
	return $self->{weight}**2;
}

sub normalize {
	my ($self, $norm) = @_;
	$self->{weight} *= $norm;
	$self->{weight} *= $self->{idf};
}

sub _scorer {
	my ($self, $reader) = @_;
	my $term_docs = $reader->term_docs($self->term);
	return unless $term_docs;
	my $norms = $reader->norms($self->term->field);
	return unless $norms;
	return Plucene::Search::TermScorer->new({
			term_docs => $term_docs,
			norms     => $norms,
			weight    => $self->{weight} });
}

sub to_string {
	my ($self, $field) = @_;
	my $rv = "";
	$rv = $self->term->field . ":" if $field ne $self->term->field;
	$rv .= $self->term->text;
	$rv .= "^" . $self->boost unless $self->boost == 1;
	return $rv;
}

1;