forked from gavincarr/net-riak
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |