Reputation: 49
Is it possible to use primary key along with secondary key. I want to get all records on the basis of primary key and then apply secondary index over it? Is it the correct way to do it or some other approach should be used?
I have a bin with primary key. Once i fetch the record using primary key, i want to get only the subset of the record. Below is the record example. Also is there a better way to organise the record? Like keeping manager, supervisor and otherStaff as the key and then querying.
{
"name" : "test",
"companyName" : "company1",
"details" : [{
"salary" : 1000,
"designation" : "manager"
},
{
"salary" : 2000,
"designation" : "supervisor"
},
{
"staff" : 500,
"designation" : "otherStaff"
}]
}
Upvotes: 1
Views: 498
Reputation: 3567
It is definitely beneficial if you can query by primary key first. It will be very efficient as the request will land on a single node (as opposed to all nodes in case of secondary index query). And on that node, the primary key index lookup is going to be fast.
When you say secondary index, looks like your requirement is to do additional filteration on the data. If my understanding is correct, you can use the Map API to fetch the keys (or values) that you are explicitly looking for. For this you may need to tweak your data model accordingly. In your example, if you are going to fetch different salaries, may be you should create a map of salaries (instead of "salary" being a key in map). Something like this:
{
"name" : "test",
"companyName" : "company1",
"salaries" : {
"manager" : 1000,
"supervisor" : 2000,
"otherStaff" : 500
}
}
You can then use the getByKeyList()
map API to fetch multiple values by specifying the keys (in the map) that you are interested in (say 'manager' and 'otherStaff').
Upvotes: 1
Reputation: 5415
Use a Secondary Index query on the bin containing the primary search data and then use Predicate Filters to further select the records returned. Example construct - record has country and city bins in namespace test, set testset, secondary index = equality string on country.
// Java statement
Record record = null;
Statement stmt = new Statement();
stmt.setSetName("testset");
stmt.setNamespace("test");
stmt.setIndexName("country_idx");
stmt.setFilter(Filter.equal("country","USA"));
//Additional Predicate Filtering
stmt.setPredExp(
PredExp.stringBin("city"),
PredExp.stringValue(".*Diego"),
//prefix-suffix match: prefix.*suffix, ignore case or newline
PredExp.stringRegex(RegexFlag.ICASE | RegexFlag.NEWLINE)
);
Upvotes: 1