Plucene::TestCase - Handy functions when testing Plucene


Plucene documentation Contained in the Plucene distribution.

Index


Code Index:

NAME

Top

Plucene::TestCase - Handy functions when testing Plucene

SYNOPSIS

Top

	use Test::More tests => 10;
	use Plucene::TestCase;

	new_index {
		add_document( foo => "bar" );
	};

	re_index {
		add_document( foo => "baz" );
	}

	with_reader {
		$READER->whatever;
	}

	my $hits = search("foo:ba*");

EXPORTS

Top

$DIR

A directory which is created for the purposes of this test, in which the index will be placed. It will normally be cleaned up at the end of the test, unless $Plucene::TestCase::DEBUG is set to allow you to peruse the entrails.

$WRITER

A variable holding the current Index::Writer object, if there is one.

$READER

A variable holding the current Index::Reader object, if there is one.

$ANALYZER

A variable holding the class name of the desired Analysis::Analyzer class.

new_index BLOCK (Analyzer)

Create a new index, and do the following stuff in the block before closing the index writer. $WRITER is set for the duration of the block.

The optional parameter should be the class name of the analyzer to use; if not specified, the value from $ANALYZER, which in turn defaults to Plucene::Analysis::SimpleAnalyzer, will be used.

re_index BLOCK (Analyzer)

Same as new_index, but doesn't create a new index, rather re-uses an old one.

add_document( field1 => value1, ...)

Add a new document to the index, with the given fields and values

with_reader BLOCK

Opens an index reader in $READER and runs the block.

Searches for the query given. If any fields are not specified, they will be assumed to be the default text. Returns a Plucene::Search::Hits object. The value of $ANALYZER will be used to construct an analyzer for the query string.


Plucene documentation Contained in the Plucene distribution.
package Plucene::TestCase;

use strict;
use warnings;

use base 'Exporter';

use Plucene::Index::Reader;
use Plucene::Index::Writer;
use Plucene::Document;
use Plucene::Document::Field;
use Plucene::Analysis::SimpleAnalyzer;
use Plucene::QueryParser;
use Plucene::Search::IndexSearcher;

our (@EXPORT, $DIR, $DEBUG, $WRITER, $READER, $ANALYZER);
@EXPORT = qw($DIR $WRITER $READER $ANALYZER new_index re_index
	with_reader add_document search);

$ANALYZER = "Plucene::Analysis::SimpleAnalyzer";

use File::Temp qw(tempdir);
$DIR = tempdir(CLEANUP => !$DEBUG);

sub new_index(&;$) {
	my ($block, $analyzer) = @_;
	$analyzer ||= $ANALYZER;

	# UNIVERSAL::require loads UNIVERSAL->import, which won't do.
	eval "require $analyzer";
	die "Couldn't require $analyzer" if $@;
	$WRITER = Plucene::Index::Writer->new($DIR, $analyzer->new, 1);
	$block->();
	undef $WRITER;
}

sub re_index(&;$) {
	my ($block, $analyzer) = @_;
	$analyzer ||= $ANALYZER;
	eval "require $analyzer";
	die "Couldn't require $analyzer" if $@;
	$WRITER = Plucene::Index::Writer->new($DIR, $analyzer->new, 0);
	$block->();
	undef $WRITER;
}

sub add_document {
	my @args = @_;
	my $doc  = Plucene::Document->new;
	while (my ($k, $v) = splice(@args, 0, 2)) {
		$doc->add(Plucene::Document::Field->Text($k, $v));
	}
	$WRITER->add_document($doc);
}

sub with_reader (&) {
	$READER = Plucene::Index::Reader->open($DIR);
	shift->();
	$READER->close;
	undef $READER;
}

sub search {
	eval "require $ANALYZER";
	my $parser = Plucene::QueryParser->new({
			analyzer => $ANALYZER->new(),
			default  => "text"
		});
	Plucene::Search::IndexSearcher->new($DIR)->search($parser->parse(shift));
}

1;