diff options
author | Mainframe2008 <thg.bot@gmail.com> | 2013-02-01 12:46:04 +0100 |
---|---|---|
committer | Mainframe2008 <thg.bot@gmail.com> | 2013-02-01 12:46:04 +0100 |
commit | 2fd683a151a9a4bdaf64d44468fbde4fa5226602 (patch) | |
tree | 30828b26fec5018a0185b08cd44660a8a2dfbc2f | |
parent | Added comments for searching using secondary indexes (diff) | |
download | net-riak-2fd683a151a9a4bdaf64d44468fbde4fa5226602.tar.gz |
Added support for searching with secondary indexes
-rw-r--r-- | lib/Net/Riak.pm | 2 | ||||
-rw-r--r-- | lib/Net/Riak/Object.pm | 29 | ||||
-rw-r--r-- | lib/Net/Riak/Role/REST.pm | 2 | ||||
-rw-r--r-- | lib/Net/Riak/Role/REST/Object.pm | 20 | ||||
-rw-r--r-- | lib/Net/Riak/Search.pm | 16 |
5 files changed, 65 insertions, 4 deletions
diff --git a/lib/Net/Riak.pm b/lib/Net/Riak.pm index d4f8ef9..06caba8 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 setup_indexing/] + handles => [qw/is_alive all_buckets server_info stats search i2search setup_indexing/] ); sub BUILDARGS { diff --git a/lib/Net/Riak/Object.pm b/lib/Net/Riak/Object.pm index 7148d4f..8e48dcb 100644 --- a/lib/Net/Riak/Object.pm +++ b/lib/Net/Riak/Object.pm @@ -5,6 +5,7 @@ package Net::Riak::Object; use Moose; use Scalar::Util; use Net::Riak::Link; +use Data::Dumper; with 'Net::Riak::Role::Replica' => {keys => [qw/r w dw/]}; with 'Net::Riak::Role::Base' => {classes => @@ -23,6 +24,9 @@ has vtag => (is => 'rw', isa => 'Str'); has content_type => (is => 'rw', isa => 'Str', default => 'application/json'); has location => ( is => 'rw', isa => 'Str' ); has _jsonize => (is => 'rw', isa => 'Bool', lazy => 1, default => 1); + +has i2indexes => ( is => 'rw', isa => 'HashRef' ); + has links => ( traits => ['Array'], is => 'rw', @@ -69,6 +73,31 @@ sub store { $self->client->store_object($w, $dw, $self); } +sub i2index { + my($self, $args) = @_; + + if ( defined($args) ) { + my %args = %{$args}; + my $ref = undef; + if ( defined($self->i2indexes) ) { $ref = $self->i2indexes; } + foreach my $i (keys %args) + { + + #$i = lc($i); + print $i,"\n"; + if ( defined($args{$i}) && length($args{$i}) > 0 ) + { + $ref->{$i} = $args{$i}; + + } else { + delete $ref->{$i}; + } + } + $self->i2indexes($ref); + } + $self->i2indexes; +} + sub status { my ($self) = @_; warn "DEPRECATED: status method will be removed in the 0.17 release, please use ->client->status."; diff --git a/lib/Net/Riak/Role/REST.pm b/lib/Net/Riak/Role/REST.pm index 92ea3ae..dc8222a 100644 --- a/lib/Net/Riak/Role/REST.pm +++ b/lib/Net/Riak/Role/REST.pm @@ -70,7 +70,7 @@ sub new_request { # makes a HTTP::Request returns and stores a HTTP::Response sub send_request { my ($self, $req) = @_; - + $self->http_request($req); my $r = $self->useragent->request($req); diff --git a/lib/Net/Riak/Role/REST/Object.pm b/lib/Net/Riak/Role/REST/Object.pm index fc45f3c..859e06b 100644 --- a/lib/Net/Riak/Role/REST/Object.pm +++ b/lib/Net/Riak/Role/REST/Object.pm @@ -1,6 +1,7 @@ package Net::Riak::Role::REST::Object; use Moose::Role; +use Data::Dumper; use JSON; sub store_object { @@ -32,13 +33,20 @@ sub store_object { $request->header('link' => $self->_links_to_header($object)); } + if ( $object->i2indexes) { + + foreach (keys %{$object->i2indexes}) { + $request->header(':x-riak-index-' . lc($_) => $object->i2indexes->{$_}); + } + } + if (ref $object->data && $object->content_type eq 'application/json') { $request->content(JSON::encode_json($object->data)); } else { $request->content($object->data); } - + my $response = $self->send_request($request); $self->populate_object($object, $response, [200, 201, 204, 300]); return $object; @@ -75,7 +83,8 @@ sub populate_object { $obj->exists(0); return if (!$http_response); - + + my $status = $http_response->code; $obj->data($http_response->content) @@ -91,6 +100,13 @@ sub populate_object { . (join(', ', @$expected)) . ", received: ".$http_response->status_line } + + $HTTP::Headers::TRANSLATE_UNDERSCORE = 0; + foreach ( $http_response->header_field_names ) { + next unless /^X-Riak-Index-(.+_bin)/; + $obj->i2index({ lc($1) => $http_response->header($_) }) + } + $HTTP::Headers::TRANSLATE_UNDERSCORE = 1; if ($status == 404) { $obj->clear; diff --git a/lib/Net/Riak/Search.pm b/lib/Net/Riak/Search.pm index ee2ed57..646d4d3 100644 --- a/lib/Net/Riak/Search.pm +++ b/lib/Net/Riak/Search.pm @@ -33,6 +33,13 @@ sub setup_indexing { $bucket->delete_object($key, 3); # optional w val + # Secondary index setup + my $obj3 = $bucket->new_object('foo3', {...}); + $obj3->i2index({ myindex_bin => 'myvalue' }); + $obj3->store; + + my @keys = $client->i2search(bucket => 'foo', index => 'myindex_bin', key => 'myvalue' ); + =head1 DESCRIPTION L<Net::Riak::Search> allows you to enable indexing documents for a given bucket and querying/searching the index. @@ -80,6 +87,15 @@ is the default index you want to query, if no index is provided you have to add is the number of documents you want to be returned in the response +=item i2index + +add secondary index to object + +=item i2search + +Find keys via secondary index. + + =back More parameters are available, just check at L<http://wiki.basho.com/Riak-Search---Querying.html#Querying-via-the-Solr-Interface> |