Riak Data Types and Search

451 views Asked by At

I'm using Riak 2.0.2 and Riak-Erlang-Client 2.0.0 The documentation suggest that "Search is preferred for querying", here the full excerpt :

In general, you should consider Search to be the default choice for nearly all querying needs that go beyond basic CRUD/KV operations. If your use case demands some sort of querying mechanism and you're in doubt about what to use, you should assume that Search is the right tool for you.

There is extensive documentation on how to use Riak Datatype, set-up bucket type, creating search index and so on. I was hoping to see riak client example on http://docs.basho.com/riak/latest/dev/search/search-data-types/ but i found none.

I try the following path.

Creating a bucket type that both uses Riak datatype and contains search index

riak-admin bucket-type create counters '{"props":{"datatype":"counter"}}' 
riak-admin bucket-type activate counters
curl -XPUT $RIAK_HOST/search/index/scores \
  -H 'Content-Type: application/json' \
  -d '{"schema":"_yz_default"}'
riak-admin bucket-type update counters '{"props":{"search_index":"scores"}}'

Used code in app.

Counter = riakc_counter:new().
ChristopherHitchensCounter = riakc_counter:increment(5, Counter).

{ok, Pid} = riakc_pb_socket:start("127.0.0.1",8087).
ChristopherHitchens = riakc_obj:new({<<"counters">>, <<"people">>}, <<"christopher_hitchens">>,
    ChristopherHitchensCounter,
    "application/riak_counter"),
riakc_pb_socket:put(Pid, ChristopherHitchens).

At this point, i expect i could query some counter using

{ok, Results} = riakc_pb_socket:search(Pid, <<"scores">>, <<"counter:[* TO 15]">>),
io:fwrite("~p~n", [Results]),
Docs = Results#search_results.docs,
io:fwrite("~p~n", [Docs]).

But it doesn't seems to be working. Any guide on this would be appreciated.

Thanks.

UPDATE

In case someone stumbling upon similar issue (until Riak documentation includes example for erlang client on http://docs.basho.com/riak/latest/dev/search/search-data-types/), the guy from riak mailing list provides link to riak test suite, and its turned out that riakc_pb_socket:update_type/4 is the required method to associate riak data type. I modify previous used code to :

Counter = riakc_counter:new().
ChristopherHitchensCounter = riakc_counter:increment(5, Counter).

{ok, Pid} = riakc_pb_socket:start("127.0.0.1",8087).
riakc_pb_socket:update_type(Pid,{<<"counters">>,<<"people">>},<<"christopher_hitchens">>,riakc_counter:to_op(ChristopherHitchensCounter)).

And now i can perform search query on my indexes :)

1

There are 1 answers

2
seancribbs On BEST ANSWER

Counters and other data types are not manipulated via riakc_obj. Refer to the documentation page here http://docs.basho.com/riak/latest/dev/using/data-types/ and select the "Erlang" tab on examples.