Search::OpenSearch::Response - provide search results in OpenSearch format


Search-OpenSearch documentation Contained in the Search-OpenSearch distribution.

Index


Code Index:

NAME

Top

Search::OpenSearch::Response - provide search results in OpenSearch format

SYNOPSIS

Top

 use Search::OpenSearch;
 my $engine = Search::OpenSearch->engine(
    type    => 'KSx',
    index   => [qw( path/to/index1 path/to/index2 )],
    facets  => {
        names       => [qw( color size flavor )],
        sample_size => 10_000,
    },
    fields  => [qw( color size flavor )],
 );
 my $response = $engine->search(
    q           => 'quick brown fox',   # query
    s           => 'rank desc',         # sort order
    o           => 0,                   # offset
    p           => 25,                  # page size
    h           => 1,                   # highlight query terms in results
    c           => 0,                   # return count stats only (no results)
    L           => 'field|low|high',    # limit results to inclusive range
    f           => 1,                   # include facets
    r           => 1,                   # include results
    format      => 'XML',               # or JSON
    b           => 'AND',               # or OR
 );
 print $response;

DESCRIPTION

Top

Search::OpenSearch::Response is an abstract base class with some common methods for all Response subclasses.

METHODS

Top

This class is a subclass of Rose::ObjectX::CAF. Only new or overridden methods are documented here.

init

Sets some defaults for a new Response.

The following standard get/set attribute methods are available:

debug
results

An interator object behaving like SWISH::Prog::Results.

total
offset
page_size
fields
facets
query
parsed_query

As returned by Search::Query.

json_query

Same as parsed_query, but the object tree is JSON encoded instead of stringified.

author
pps

Pages-per-section. Used by Data::Pageset. Default is "10".

title
search_time
build_time
engine

build_pager

Returns Data::Pageset object based on offset() and page_size().

as_hash

Returns the Response object as a hash ref of key/value pairs.

stringify

Returns the Response in the chosen serialization format.

Response objects are overloaded to call stringify().

add_attribute( attribute_name )

Adds get/set method attribute_name to the class and will include that attribute in as_hash(). This method is intended to make it easier to extend the basic structure without needing to subclass.

AUTHOR

Top

Peter Karman, <karman at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-search-opensearch at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Search-OpenSearch. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Search::OpenSearch::Response




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Search-OpenSearch

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Search-OpenSearch

* CPAN Ratings

http://cpanratings.perl.org/d/Search-OpenSearch

* Search CPAN

http://search.cpan.org/dist/Search-OpenSearch/

COPYRIGHT & LICENSE

Top


Search-OpenSearch documentation Contained in the Search-OpenSearch distribution.

package Search::OpenSearch::Response;
use strict;
use warnings;
use base qw( Rose::ObjectX::CAF );
use Carp;
use Data::Pageset;
use overload
    '""'     => sub { $_[0]->stringify; },
    'bool'   => sub {1},
    fallback => 1;

my @attributes = qw(
    engine
    results
    total
    offset
    page_size
    fields
    facets
    query
    parsed_query
    json_query
    title
    link
    author
    search_time
    build_time
);
__PACKAGE__->mk_accessors( @attributes, qw( debug pps ) );

our $VERSION = '0.13';

sub init {
    my $self = shift;
    $self->SUPER::init(@_);
    $self->{title}  ||= 'OpenSearch Results';
    $self->{author} ||= ref($self);
    $self->{link}   ||= '';
    $self->{pps}    ||= 10;
    return $self;
}

sub stringify { croak "$_[0] does not implement stringify()" }

sub as_hash {
    my $self = shift;
    my %hash = map { $_ => $self->$_ } @attributes;
    return \%hash;
}

sub build_pager {
    my $self      = shift;
    my $offset    = $self->offset;
    my $page_size = $self->page_size;
    my $this_page = ( $offset / $page_size ) + 1;
    my $pager     = Data::Pageset->new(
        {   total_entries    => $self->total,
            entries_per_page => $page_size,
            current_page     => $this_page,
            pages_per_set    => $self->pps,
            mode             => 'slide',
        }
    );
    return $pager;
}

sub add_attribute {
    my $self = shift;
    for my $attr (@_) {
        $self->mk_accessors($attr);
        push @attributes, $attr;
    }
}


1;

__END__