From 9e4c681beae5a7a223cf84d411a12a1ccb3daa6f Mon Sep 17 00:00:00 2001 From: gmaurice Date: Wed, 22 Jun 2011 19:27:21 +0200 Subject: add basic support of riaksearch (need to see what is missed) --- lib/Net/Riak.pm | 2 +- lib/Net/Riak/Client.pm | 5 +++++ lib/Net/Riak/Role/REST.pm | 1 + lib/Net/Riak/Role/REST/Search.pm | 28 ++++++++++++++++++++++++++++ lib/Net/Riak/Search.pm | 15 +++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/Net/Riak/Role/REST/Search.pm create mode 100644 lib/Net/Riak/Search.pm (limited to 'lib') diff --git a/lib/Net/Riak.pm b/lib/Net/Riak.pm index 61f73aa..3210007 100644 --- a/lib/Net/Riak.pm +++ b/lib/Net/Riak.pm @@ -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/] ); sub BUILDARGS { diff --git a/lib/Net/Riak/Client.pm b/lib/Net/Riak/Client.pm index f38bec6..ad9315e 100644 --- a/lib/Net/Riak/Client.pm +++ b/lib/Net/Riak/Client.pm @@ -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', diff --git a/lib/Net/Riak/Role/REST.pm b/lib/Net/Riak/Role/REST.pm index dfab5a0..261d573 100644 --- a/lib/Net/Riak/Role/REST.pm +++ b/lib/Net/Riak/Role/REST.pm @@ -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 => ( diff --git a/lib/Net/Riak/Role/REST/Search.pm b/lib/Net/Riak/Role/REST/Search.pm new file mode 100644 index 0000000..c75b84b --- /dev/null +++ b/lib/Net/Riak/Role/REST/Search.pm @@ -0,0 +1,28 @@ +package Net::Riak::Role::REST::Search; +use Moose::Role; +use JSON; + +sub search { + my ( $self, $params) = @_; + + my $request; + $request = + $self->new_request( 'GET', + [ $self->search_prefix, "select" ], $params ) unless $params->{index}; + $request = + $self->new_request( 'GET', + [ $self->search_prefix, $params->{index}, "select" ], $params ) if $params->{index}; + + my $http_response = $self->send_request($request); + + return if (!$http_response); + + my $status = $http_response->code; + if ($status == 404) { + return; + } + JSON::decode_json($http_response->content); +}; + + +1; diff --git a/lib/Net/Riak/Search.pm b/lib/Net/Riak/Search.pm new file mode 100644 index 0000000..ac9f059 --- /dev/null +++ b/lib/Net/Riak/Search.pm @@ -0,0 +1,15 @@ +package Net::Riak::Search; + +# ABSTRACT: the riaklink object represents a link from one Riak object to another + +use Moose; + +with 'Net::Riak::Role::Base' => {classes => + [{name => 'client', required => 0},]}; + +sub search { + my ($self, $params) = @_; + $self->client->search($params); +}; + +1; -- cgit v1.2.3 From 4fe4d90d96b77a2265be5f808f9ed0c2612d28d1 Mon Sep 17 00:00:00 2001 From: gmaurice Date: Sat, 25 Jun 2011 20:52:38 +0200 Subject: remove index param from being added to query params --- lib/Net/Riak/Role/REST/Search.pm | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/Net/Riak/Role/REST/Search.pm b/lib/Net/Riak/Role/REST/Search.pm index c75b84b..161acf5 100644 --- a/lib/Net/Riak/Role/REST/Search.pm +++ b/lib/Net/Riak/Role/REST/Search.pm @@ -9,10 +9,13 @@ sub search { $request = $self->new_request( 'GET', [ $self->search_prefix, "select" ], $params ) unless $params->{index}; - $request = - $self->new_request( 'GET', - [ $self->search_prefix, $params->{index}, "select" ], $params ) if $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); @@ -21,6 +24,8 @@ sub search { if ($status == 404) { return; } +use YAML::Syck; +warn Dump $http_response; JSON::decode_json($http_response->content); }; -- cgit v1.2.3 From ade1fc13ec613d2318a9c3517bc40837d99f4d99 Mon Sep 17 00:00:00 2001 From: gmaurice Date: Sat, 2 Jul 2011 00:03:55 +0200 Subject: allow to setup indexing on a bucket via precommit add tests for riaksearch --- lib/Net/Riak.pm | 2 +- lib/Net/Riak/Role/REST/Search.pm | 59 ++++++++++++++++++++++++++++++++++------ lib/Net/Riak/Search.pm | 7 +++-- 3 files changed, 56 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/Net/Riak.pm b/lib/Net/Riak.pm index 3210007..dbfc6a5 100644 --- a/lib/Net/Riak.pm +++ b/lib/Net/Riak.pm @@ -14,7 +14,7 @@ has client => ( is => 'rw', isa => Client_T, required => 1, - handles => [qw/is_alive all_buckets server_info stats search/] + handles => [qw/is_alive all_buckets server_info stats search setup_indexing/] ); sub BUILDARGS { diff --git a/lib/Net/Riak/Role/REST/Search.pm b/lib/Net/Riak/Role/REST/Search.pm index 161acf5..642964b 100644 --- a/lib/Net/Riak/Role/REST/Search.pm +++ b/lib/Net/Riak/Role/REST/Search.pm @@ -3,17 +3,18 @@ use Moose::Role; use JSON; sub search { - my ( $self, $params) = @_; - + 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}; + [ $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 ); + [ $self->search_prefix, $index, "select" ], \%params ); } my $http_response = $self->send_request($request); @@ -24,10 +25,50 @@ sub search { if ($status == 404) { return; } -use YAML::Syck; -warn Dump $http_response; - JSON::decode_json($http_response->content); + + 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; diff --git a/lib/Net/Riak/Search.pm b/lib/Net/Riak/Search.pm index ac9f059..8cf42b7 100644 --- a/lib/Net/Riak/Search.pm +++ b/lib/Net/Riak/Search.pm @@ -1,7 +1,5 @@ package Net::Riak::Search; -# ABSTRACT: the riaklink object represents a link from one Riak object to another - use Moose; with 'Net::Riak::Role::Base' => {classes => @@ -12,4 +10,9 @@ sub search { $self->client->search($params); }; +sub setup_indexing { + my ($self, $bucket) = @_; + $self->client->setup_indexing($bucket); +}; + 1; -- cgit v1.2.3