function (doc, meta) {
if (doc && doc.docType == ‘PRODUCT’, doc.category) {
if (doc.value != null)
emit(doc.category, doc.value);
}
}
reduce:
_stats
when I do this using web console and Show Results, I get null for key and stats for value, then I select group, and I get my categories for key, and stats for value, but don’t know how to access the reduce data via SDK.
ah yes that’s with the latest SDK, worth mentioning you were using Spring Data 1.4.x since it doesn’t have any particular support for grouping and reduce…
You can execute a view query using the CouchbaseTemplate.queryView method, but the old client implies more work to achieve that because it doesn’t have any built-in JSON support, so the value it returns will be a plan raw JSON String.
Up to you to deserialize it using your JSON library of choice (or even searching in the String). Here is the example transposed to old client going through Spring Data’s template:
Query query = new Query();
query.setGroup(true);
ViewResponse result = couchbaseTemplate.queryView("myDesignDoc", "myViewName", query);
for (ViewRow viewRow : result) {
System.out.println(row.getKey() + ": " + row.getValue()); //getValue returns a String, always
}
Great thanks, I will likely upgrade to the latest, but good to see both examples.
I tried using Query object although don’t recall if I tried setting group = true or reduce either way I got an error via spring that group/reduce wasn’t supported.