0

Is there a way to get all the objects in key/value format which are under one similar secondary index value. I know we can get the list of keys for one secondary index (bucket/{{bucketName}}/index/{{index_name}}/{{index_val}}). But somehow my requirements are such that if I can get all the objects too. I don't want to perform a separate query for each key to get the object details separately if there is way around it.

I am completely new to Riak and I am totally a front-end guy, so please bear with me if something I ask is of novice level.

vWebby
  • 29
  • 1
  • 5

1 Answers1

0

In Riak, it's sometimes the case that the better way is to do separate lookups for each key. Coming from other databases this seems strange, and likely inefficient, however you may find your query will be faster over an index and a bunch of single object gets, than a map/reduce for all the objects in a single go.

Try both these approaches, and see which turns out fastest for your dataset - variables that affect this are: size of data being queried; size of each document; power of your cluster; load the cluster is under etc.

Python code demonstrating the index and separate gets (if the data you're getting is large, this method can be made memory-efficient on the client, as you don't need to store all the objects in memory):

query = riak_client.index("bucket_name", 'myindex', 1)
query.map("""
    function(v, kd, args) {
        return [v.key];
    }"""
)
results = query.run()

bucket = riak_client.bucket("bucket_name")
for key in results:
    obj = bucket.get(key)
    # .. do something with the object

Python code demonstrating a map/reduce for all objects (returns a list of {key:document} objects):

query = riak_client.index("bucket_name", 'myindex', 1)
query.map("""
    function(v, kd, args) {
        var obj = Riak.mapValuesJson(v)[0];
        return [ {
            'key': v.key,
            'data': obj,
        } ];
    }"""
)
results = query.run()
mafrosis
  • 2,720
  • 1
  • 25
  • 34