Counters inside Maps

Sean Cribbs sean at basho.com
Tue Aug 19 19:08:46 EDT 2014


On Tue, Aug 19, 2014 at 3:34 PM, Alex De la rosa
<alex.rosa.box at gmail.com> wrote:
> Hi Sean,
>
> I didn't created the bucket_type as a map datatype as at first i was just
> testing simple Riak Search... then it occurred to me what if I want a
> counter in the data? :)
>
> Your example is pretty straightforward to follow and simple. Just 2
> questions:
>
> 1. key.counters['number'].increment(1) => No need to define a "counters"
> data-type somewhere before putting it inside the map as we normally need in
> simple buckets? If it works automatically is great :)

Yes, it works automatically. All included datatypes are available inside maps.

>
> 2. if we use "number_counter" instead of "number_i" does Search/SOLR
> understand is an integer? in case you want to do a range... as somewhere in
> the docs I read that better to use "_s" for strings, "_b" for binary, "_i"
> for integers, etc... so SOLR knows how to treat the data... I believe there
> will be no strange behaviours for having "_register" instead of "_s" and
> "_counter" instead of "_i", right?

The default Solr schema that ships with Riak accounts for these
datatypes automatically and uses the appropriate index field type:
https://github.com/basho/yokozuna/blob/develop/priv/default_schema.xml#L96-L104

If you write your own schema, you will want to include or change the
schema fields appropriately.

>
> Thanks!
> Alex
>
>
> On Wed, Aug 20, 2014 at 12:24 AM, Sean Cribbs <sean at basho.com> wrote:
>>
>> Alex,
>>
>> Assuming you've already made your bucket-type with "map" as the
>> datatype, then "bucket.new()" will return you a Map instead of a
>> RiakObject. Translating your example above:
>>
>> key = bucket.new('lionelmessi')
>> key.registers['name'].assign('Messi')
>> key.registers['team'].assign('Barcelona')
>> key.counters['number'].increment(10)
>> key.store()
>>
>> Note that because Maps are based on mutation operations and not
>> replacing the value with new ones, you can later do this without
>> setting the entire value:
>>
>> key.counters['number'].increment(1)
>> key.store()
>>
>> This will also change your searches, however, in that the fields will
>> be suffixed with the embedded type you are using:
>>
>> r = client.fulltext_search('ix_footballers', 'team_register:Barcelona')
>>
>> Hope that helps!
>>
>> On Tue, Aug 19, 2014 at 2:59 PM, Alex De la rosa
>> <alex.rosa.box at gmail.com> wrote:
>> > Imagine I have a Riak object "footballer" with some static fields: name,
>> > team, number. I store them like this now:
>> >
>> > 1: CREATE INDEX FOR RIAK SEARCH
>> > curl -XPUT "http://148.251.140.229:8098/search/index/ix_footballers"
>> >
>> > 2: CREATE BUCKET TYPE
>> > riak-admin bucket-type create tp_footballers
>> > '{"props":{"allow_mult":false,"search_index":"ix_footballers"}}'
>> > riak-admin bucket-type activate tp_footballers
>> >
>> > 3: INSERT A PLAYER
>> > bucket = client.bucket_type('tp_footballers').bucket('footballers')
>> > key = bucket.new('lionelmessi', data={'name_s':'Messi',
>> > 'team_s':'Barcelona', 'number_i':10}, content_type='application/json')
>> > key.store()
>> >
>> > 4: SEARCH FOR BARCELONA PLAYERS
>> > r = client.fulltext_search('ix_footballers', 'team_s:Barcelona')
>> >
>> > So far so good :) BUT... what if I want to have a field "goals_i" that
>> > is a
>> > counter that will be incremented each match day with the number of goals
>> > he
>> > scored? What is the syntax/steps to do to set up "footballers" as a MAP
>> > and
>> > then put a COUNTER inside? I know is possible as I read it in some data
>> > dump
>> > some Basho employee passed me some time ago, but I can't manage to see
>> > how
>> > to do it now.
>> >
>> > Thanks!
>> > Alex
>> >
>> > _______________________________________________
>> > riak-users mailing list
>> > riak-users at lists.basho.com
>> > http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
>> >
>>
>>
>>
>> --
>> Sean Cribbs <sean at basho.com>
>> Software Engineer
>> Basho Technologies, Inc.
>> http://basho.com/
>
>



-- 
Sean Cribbs <sean at basho.com>
Software Engineer
Basho Technologies, Inc.
http://basho.com/




More information about the riak-users mailing list