added search indexer

This commit is contained in:
JT Smith 2006-01-19 05:15:20 +00:00
parent 150087f703
commit e6a172514d
3 changed files with 371 additions and 2 deletions

View file

@ -1,8 +1,21 @@
package WebGUI::Search;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2006 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use warnings;
use Carp;
use Plucene::Analysis::SimpleAnalyzer;
use Plucene::Analysis::WhitespaceAnalyzer;
@ -18,14 +31,37 @@ use File::Spec::Functions qw(catfile);
use WebGUI::Search::DateTimeFilter;
use WebGUI::Utility;
=head1 NAME
Package WebGUI::Search
=head1 DESCRIPTION
A package for working with the WebGUI Search Engine.
=head1 SYNOPSIS
use WebGUI::Search;
=head1 METHODS
These methods are available from this package:
=cut
#-------------------------------------------------------------------
sub open {
my ($class, $dir) = @_;
$dir or croak "No directory given";
bless { _dir => $dir }, $class;
}
#-------------------------------------------------------------------
sub _dir { shift->{_dir} }
#-------------------------------------------------------------------
sub _parsed_query {
my ($self, $query, $default) = @_;
my $parser = Plucene::QueryParser->new({
@ -35,10 +71,13 @@ sub _parsed_query {
$parser->parse($query);
}
#-------------------------------------------------------------------
sub _searcher { Plucene::Search::IndexSearcher->new(shift->_dir) }
#-------------------------------------------------------------------
sub _reader { Plucene::Index::Reader->open(shift->_dir) }
#-------------------------------------------------------------------
sub search {
my ($self, $sstring) = @_;
return () unless $sstring;
@ -50,10 +89,12 @@ sub search {
my $res = eval { $searcher->doc($doc) };
push @docs, [ $res, $score ] if $res;
});
$searcher->search_hc($self->_parsed_query($sstring, 'text'), $hc);
#$searcher->search_hc($self->_parsed_query($sstring, 'text'), $hc);
$searcher->search_hc($self->_parsed_query($sstring, '_raw_'), $hc);
return map $_->[0]->get("id")->string, sort { $b->[1] <=> $a->[1] } @docs;
}
#-------------------------------------------------------------------
sub search_during {
my ($self, $sstring, $date1, $date2) = @_;
return () unless $sstring;
@ -73,6 +114,7 @@ sub search_during {
return map $_->get("id")->string, @docs;
}
#-------------------------------------------------------------------
sub _writer {
my $self = shift;
return Plucene::Index::Writer->new(
@ -82,6 +124,7 @@ sub _writer {
);
}
#-------------------------------------------------------------------
sub add {
my ($self, @data) = @_;
my $writer = $self->_writer;
@ -106,6 +149,7 @@ use DateTime;
undef $writer;
}
#-------------------------------------------------------------------
sub index_document {
my ($self, $id, $data) = @_;
my $writer = $self->_writer;
@ -116,6 +160,7 @@ sub index_document {
undef $writer;
}
#-------------------------------------------------------------------
sub delete_document {
my ($self, $id) = @_;
my $reader = $self->_reader;
@ -124,8 +169,10 @@ sub delete_document {
$reader->close;
}
#-------------------------------------------------------------------
sub optimize { shift->_writer->optimize() }
#-------------------------------------------------------------------
sub indexed {
my ($self, $id) = @_;
my $term = Plucene::Index::Term->new({ field => 'id', text => $id });