Hey all,
I’m in the process of writing a “reports” page for my app that filters and then aggregates data. More specifically, I want to design a view/query combination that first filters data in a view by date range, and then groups/reduces based on a secondary key. Is this possible? Here is my attempt so far:
Map Function:
String docType = SaleItem.class.getSimpleName();
String type = (String) document.get(DatabaseUtils.type);
if (docType.equals(type)) {
List<Object> keys = new ArrayList<>();
keys.add(document.get("sale_date"));
keys.add(document.get("supply_item_id"));
Map<String, Object> totals = new HashMap<>(3);
totals.put("total_price", new BigDecimal(document.get("total_price").toString()));
totals.put("quantity_sold", document.get("quantity_sold"));
totals.put("supply_item_snapshot", document.get("supply_item_snapshot"));
emitter.emit(keys, totals);
}
}
and my reduce function:
SaleItemAggregate saleItemAggregate = new SaleItemAggregate();
for (Object value : values) {
LazyJsonObject jsonValue = (LazyJsonObject) value;
if (value != null) {
saleItemAggregate.addValues(jsonValue);
}
}
return saleItemAggregate;
Here’s my query code:
mQuery = SaleItem.getQuery(getMyActivity().getDatabase(), SaleItem.AGGREGATE_VIEW);
mQuery.setDescending(true);
mQuery.setStartKey(Arrays.asList(new ArrayList<>(), DateTime.now().getMillis()));
mQuery.setEndKey(Arrays.asList(DateTime.now().withDayOfMonth(1).withTimeAtStartOfDay()));
mQuery.setGroupLevel(2);
I’m obviously leaving a few things out, but I think I’ve got the essentials in here, and the stuff I left is working for every other query.
My hunch is that this isn’t possible because grouping attempt to match both of the first two keys, but I’d like that confirmed