Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #13 from gmaurice/master
Basic support of Riaksearch
  • Loading branch information
Robin Edwards committed Jul 5, 2011
2 parents c7e0900 + 99d770f commit b972db8
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Net/Riak.pm
Expand Up @@ -14,7 +14,7 @@ has client => (
is => 'rw',
isa => Client_T,
required => 1,
handles => [qw/is_alive all_buckets server_info stats/]
handles => [qw/is_alive all_buckets server_info stats search setup_indexing/]
);

sub BUILDARGS {
Expand Down
5 changes: 5 additions & 0 deletions lib/Net/Riak/Client.pm
Expand Up @@ -15,6 +15,11 @@ has mapred_prefix => (
isa => 'Str',
default => 'mapred'
);
has search_prefix => (
is => 'rw',
isa => 'Str',
default => 'solr'
);
has [qw/r w dw/] => (
is => 'rw',
isa => 'Int',
Expand Down
1 change: 1 addition & 0 deletions lib/Net/Riak/Role/REST.pm
Expand Up @@ -12,6 +12,7 @@ with qw/Net::Riak::Role::REST::Bucket
Net::Riak::Role::REST::Object
Net::Riak::Role::REST::Link
Net::Riak::Role::REST::MapReduce
Net::Riak::Role::REST::Search
/;

has http_request => (
Expand Down
74 changes: 74 additions & 0 deletions lib/Net/Riak/Role/REST/Search.pm
@@ -0,0 +1,74 @@
package Net::Riak::Role::REST::Search;
use Moose::Role;
use JSON;

sub search {
my $self = shift;
my %params = @_;
my $request;

$request =
$self->new_request( 'GET',
[ $self->search_prefix, "select" ], \%params ) unless $params{index};
if ( $params{index} ){
my $index = delete $params{index};
$request =
$self->new_request( 'GET',
[ $self->search_prefix, $index, "select" ], \%params );
}

my $http_response = $self->send_request($request);

return if (!$http_response);

my $status = $http_response->code;
if ($status == 404) {
return;
}

return JSON::decode_json($http_response->content) if $params{wt} =~ /json/i;
$http_response->content;
};

sub setup_indexing {
my ( $self, $bucket ) = @_;
my $request =
$self->new_request( 'GET',
[ $self->prefix, $bucket ] );

my $http_response = $self->send_request($request);

return if (!$http_response);
my $status = $http_response->code;
if ($status == 404) {
return;
}

my $precommits = JSON::decode_json($http_response->content)->{props}->{precommit};

for (@$precommits){
return JSON::decode_json($http_response->content) if $_->{mod} eq "riak_search_kv_hook";
}
push ( @$precommits, { mod => "riak_search_kv_hook" , fun => "precommit" } );

$request = $self->new_request( 'PUT', [ $self->prefix, $bucket ] );
$request->content( JSON::encode_json({ props => { precommit => $precommits } } ) );
$request->header('Content-Type' => "application/json" );

$http_response = $self->send_request($request);

return if (!$http_response);
$status = $http_response->code;
if ($status == 404) {
return;
}
$request =
$self->new_request( 'GET',
[ $self->prefix, $bucket ] );

$http_response = $self->send_request($request);

JSON::decode_json($http_response->content);
}

1;
18 changes: 18 additions & 0 deletions lib/Net/Riak/Search.pm
@@ -0,0 +1,18 @@
package Net::Riak::Search;

use Moose;

with 'Net::Riak::Role::Base' => {classes =>
[{name => 'client', required => 0},]};

sub search {
my ($self, $params) = @_;
$self->client->search($params);
};

sub setup_indexing {
my ($self, $bucket) = @_;
$self->client->setup_indexing($bucket);
};

1;
30 changes: 30 additions & 0 deletions t/20_search.t
@@ -0,0 +1,30 @@
use lib 't/lib';
use Test::More;
use Test::Riak;

test_riak_rest {
my ($client, $bucket_name) = @_;
ok $client->setup_indexing($bucket_name), 'setup indexing ok';

ok my $bucket = $client->bucket($bucket_name), 'got bucket test';
my $content = { field => "indexed" };

ok my $obj = $bucket->new_object(undef, $content),
'created a new riak object without a key';
ok $obj->store, 'store object without key';
ok $obj->key, 'key created';

is $client->search(
index => $bucket_name,
wt => "json",
q => "field:indexed")->{response}->{docs}[0]->{id},
$obj->key,
'search with index in path';

is $client->search(
wt => "json",
q => "$bucket_name.field:indexed")->{response}->{docs}[0]->{id},
$obj->key,
'search with index prefixes in query';
$obj->delete;
}

0 comments on commit b972db8

Please sign in to comment.