Dezi-Client

 view release on metacpan or  search on metacpan

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

 # add/update an in-memory document to the index
 $client->index( \$html_doc, 'foo/bar.html' );
 
 # add/update a Dezi::Doc to the index
 $client->index( $dezi_doc );
 
 # remove a document from the index
 $client->delete( '/doc/uri/relative/to/index' );
 
 # search the index
 my $response = $client->search( q => 'foo' );
 
 # iterate over results
 for my $result (@{ $response->results }) {
     printf("--\n uri: %s\n title: %s\n score: %s\n",
        $result->uri, $result->title, $result->score);
 }
 
 # print stats
 printf("       hits: %d\n", $response->total);
 printf("search time: %s\n", $response->search_time);
 printf(" build time: %s\n", $response->build_time);
 printf("      query: %s\n", $response->query);
 
 
=head1 DESCRIPTION

Dezi::Client is a client for the Dezi search platform.

=head1 METHODS

=head2 new( I<params> )

Instantiate a Client instance. Expects the following params:

=over

=item server I<url>

The I<url> of the Dezi server. If the B<search> or B<index>
params are not passed to new(), then the server will be
interrogated at initial connect for the correct paths
for searching and indexing.

=item server_params I<params>

Passed internally to URI::Query and appended to server I<url>.

=item search I<path>

The URI path for searching. Dezi defaults to B</search>.

=item index I<path>

The URI path for indexing. Dezi defaults to B</index>.

=item username I<username>

=item password I<password>

If present, the username and password credentials will
be set in each internal HTTP::Request object for any
non-idempotent action (delete(), index(), commit(), rollback()).
 
=back

=cut

sub new {
    my $class = shift;
    my %args  = @_;
    if ( !%args or !exists $args{server} ) {
        croak "server param required";
    }
    my $self = bless { server => delete $args{server} }, $class;

    $self->{debug} = delete $args{debug} || 0;
    if ( $self->{debug} ) {
        require Data::Dump;
    }

    $self->{ua} = LWP::UserAgent->new();
    if ( $args{search} and $args{index} ) {
        $self->{search_uri} = $self->{server} . delete $args{search};
        $self->{index_uri}  = $self->{server} . delete $args{index};
        $self->{commit_uri}
            = $self->{server} . ( delete $args{commit} || 'commit' );
        $self->{rollback_uri}
            = $self->{server} . ( delete $args{rollback} || 'rollback' );
    }
    else {
        my $uri = $self->{server};
        if ( $args{server_params} ) {
            $self->{server_params}
                = URI::Query->new( delete $args{server_params} );
            $uri .= '?' . $self->{server_params};
        }
        my $resp = $self->{ua}->get($uri);
        if ( !$resp->is_success ) {
            croak $resp->status_line;
        }
        my $paths = from_json( $resp->decoded_content );
        if (   !$resp->is_success
            or !$paths
            or !$paths->{search}
            or !$paths->{index} )
        {
            croak "Bad response from server $self->{server}: "
                . $resp->status_line . " "
                . $resp->decoded_content;
        }
        $self->{search_uri}   = $paths->{search};
        $self->{index_uri}    = $paths->{index};
        $self->{commit_uri}   = $paths->{commit};
        $self->{rollback_uri} = $paths->{rollback};
        $self->{fields}       = $paths->{fields};
        $self->{facets}       = $paths->{facets};
    }

    $self->{_creds} = {
        username => delete $args{username},



( run in 0.846 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )