Dezi-App

 view release on metacpan or  search on metacpan

lib/Dezi/Searcher.pm  view on Meta::CPAN

package Dezi::Searcher;
use Moose;
use MooseX::StrictConstructor;
with 'Dezi::Role';
use Types::Standard qw( InstanceOf HashRef Int );
use Dezi::Types qw( DeziInvIndexArr );
use Dezi::Searcher::SearchOpts;
use Carp;
use Scalar::Util qw( blessed );
use Class::Load;
use Search::Query;
use Search::Query::Parser;
use namespace::autoclean;

our $VERSION = '0.016';

has 'max_hits' => ( is => 'rw', isa => Int, default => 1000 );
has 'invindex' => (
    is       => 'rw',
    isa      => DeziInvIndexArr,
    required => 1,
    coerce   => 1,
);
has 'qp_config' =>
    ( is => 'rw', isa => HashRef, builder => 'init_qp_config', lazy => 1, );
has 'qp' => (
    is      => 'rw',
    isa     => InstanceOf ['Search::Query::Parser'],
    builder => 'init_qp',
    lazy    => 1,
);
has 'property_map' => ( is => 'ro', isa => HashRef );

=head1 NAME

Dezi::Searcher - base searcher class

=head1 SYNOPSIS

 my $searcher = Dezi::Searcher->new(
                    invindex        => 'path/to/index',
                    max_hits        => 1000,
                );

 my $results = $searcher->search( 'foo bar' );
 while (my $result = $results->next) {
     printf("%4d %s\n", $result->score, $result->uri);
 }

=head1 DESCRIPTION

Dezi::Searcher is a base searcher class. It defines
the APIs that all Dezi storage backends adhere to in
returning results from a Dezi::InvIndex.

=head1 METHODS

=head2 BUILD

Build searcher object. Called internally by new().

=head2 invindex

A Dezi::InvIndex object or directory path. Required. Set in new().

May be a single value or an array ref of values (for searching multiple
indexes at once).

=head2 max_hits

The maximum number of hits to return. Optional. Default is 1000.

=head2 qp_config

Optional hashref passed to Search::Query::Parser->new().

=cut

sub BUILD {
    my $self = shift;

    for my $invindex ( @{ $self->{invindex} } ) {

        # make sure invindex is blessed into invindex_class
        # and re-bless if necessary
        if ( !blessed $invindex or !$invindex->isa( $self->invindex_class ) )
        {
            Class::Load::load_class( $self->invindex_class );
            $invindex = $self->invindex_class->new( path => "$invindex" );
        }

        $invindex->open_ro;



( run in 1.980 second using v1.01-cache-2.11-cpan-437f7b0c052 )