Secondary indexes can be created manually by the application; there is no disadvantage, other than complexity, to doing so.However, when the secondary key can be mechanically derived from the primary key and datum that it points to, as is frequently the case, Berkeley DB can automatically and transparently manage secondary indexes.Put operations of any kind are forbidden on secondary indexes, as there is no way to specify a primary key for a newly put item.Instead, the application should use the DB-put() methods on the primary database.Once tagged, you could find all objects in a Riak bucket sharing that tag. The following example indexes four different objects.
A typical database would use the student ID number as the key; however, one might also reasonably want to be able to look up students by last name.
Note on 2i and strong consistency Secondary indexes do not currently work with the strong consistency feature introduced in Riak version 2.0. Builder(moe) Location(people Key("moe")) .build(); client.execute(store Moe); Riak Object curly = new Riak Object() Value(Binary Value.create("My name is Curly")); Indexes()Index(String Bin Index.named("FIELD1")).add("val3"); Indexes()Index(Long Int Index.named("FIELD2")).add(1003L); Store Value store Curly = new Store Value.
If you store objects in strongly consistent buckets and attach secondary index metadata to those objects, you can still perform strongly consistent operations on those objects but the secondary indexes will be ignored. Builder(curly) Location(people Key("curly")) .build(); client.execute(store Curly); Riak Object veronica = new Riak Object() Value(Binary Value.create("My name is Veronica")); Indexes()Index(String Bin Index.named("field1")) .add("val4").add("val4"); Indexes()Index(Long Int Index.named("field2")) .add(1004L).add(1005L).add(1006L).add(1004L).add(1004L).add(1007L); Store Value store Veronica = new Store Value.
Note: Riak Search preferred for querying If you’re interested in non-primary-key-based querying in Riak, i.e.
if you’re looking to go beyond straightforward K/V operations, we now recommend Riak Search rather than secondary indexes for a variety of reasons.