Session: Querying: How to perform dynamic aggregation?

Dynamic aggregation can be performed using aggregateBy method. Internally such aggregation is an extended faceted search.

Syntax

public DynamicAggregationQuery<T> aggregateBy(String path);

public DynamicAggregationQuery<T> aggregateBy(String path, String displayName);

public DynamicAggregationQuery<T> aggregateBy(Path<?> path);

public DynamicAggregationQuery<T> aggregateBy(Path<?> path, String displayName);
Parameters
path String or Path Path (or expression from which path will be extracted) to field on which aggregation will be performed.
displayName String User defined friendly name for aggregation. If null, field name will be used.
Return Value
DynamicAggregationQuery<TResult> Query containing aggregation methods.

Example I - summing

// sum up all order prices
// for each customer
// where price is higher than 500
QHowToPerformDynamicAggregation_Order o = QHowToPerformDynamicAggregation_Order.order;
FacetResults aggregationResults = session
  .query(Order.class, "Orders/All")
  .where(o.totalPrice.gt(500))
  .aggregateBy(o.customerId)
    .sumOn(o.totalPrice)
  .toList();

FacetResult customerAggregation = aggregationResults.getResults().get("CustomerId");
Double sum = customerAggregation.getValues().get(0).getSum();

Example II - counting

// count all orders
// for each customer
// where price is higher than 500
QHowToPerformDynamicAggregation_Order o = QHowToPerformDynamicAggregation_Order.order;
FacetResults aggregationResults = session
  .query(Order.class, "Orders/All")
  .where(o.totalPrice.gt(500))
  .aggregateBy(o.customerId)
    .countOn(o.totalPrice)
  .toList();

FacetResult customerAggregation = aggregationResults.getResults().get("CustomerId");
Integer count = customerAggregation.getValues().get(0).getCount();

Example III - average

// count price average for orders
// for each customer
// where price is higher than 500
QHowToPerformDynamicAggregation_Order o = QHowToPerformDynamicAggregation_Order.order;
FacetResults aggregationResults = session
  .query(Order.class, "Orders/All")
  .where(o.totalPrice.gt(500))
  .aggregateBy(o.customerId)
    .averageOn(o.totalPrice)
  .toList();

FacetResult customerAggregation = aggregationResults.getResults().get("CustomerId");
Double average = customerAggregation.getValues().get(0).getAverage();

Example IV - maximum and minimum

// count max and min price for orders
// for each customer
// where price is higher than 500
QHowToPerformDynamicAggregation_Order o = QHowToPerformDynamicAggregation_Order.order;
FacetResults aggregationResults = session
  .query(Order.class, "Orders/All")
  .where(o.totalPrice.gt(500))
  .aggregateBy(o.customerId)
    .minOn(o.totalPrice)
    .maxOn(o.totalPrice)
  .toList();

FacetResult customerAggregation = aggregationResults.getResults().get("CustomerId");
Double min = customerAggregation.getValues().get(0).getMin();
Double max = customerAggregation.getValues().get(0).getMax();

Example V - adding ranges

QHowToPerformDynamicAggregation_Order o = QHowToPerformDynamicAggregation_Order.order;
FacetResults aggregationResults = session.query(Order.class, "Orders/All")
  .aggregateBy(o.customerId)
    .addRanges(o.totalPrice.lt(10))
    .addRanges(o.totalPrice.goe(100).and(o.totalPrice.lt(500)))
    .addRanges(o.totalPrice.goe(500).and(o.totalPrice.lt(1000)))
    .addRanges(o.totalPrice.goe(1000))
  .toList();

FacetResult customerAggregation = aggregationResults.getResults().get("CustomerId");
List<FacetValue> values = customerAggregation.getValues();

Example VI - multiple aggregations

// sum up all order prices
// for each customer
// count price average for all orders
// for each company
QHowToPerformDynamicAggregation_Order o = QHowToPerformDynamicAggregation_Order.order;
FacetResults aggregationResults = session
  .query(Order.class, "Orders/All")
  .aggregateBy(o.customerId)
    .sumOn(o.totalPrice)
  .andAggregateOn(o.companyId)
    .averageOn(o.totalPrice)
  .toList();

FacetResult customerAggregation = aggregationResults.getResults().get("CustomerId");
FacetResult companyAggregation = aggregationResults.getResults().get("CompanyId");

Double sum = customerAggregation.getValues().get(0).getSum();
Double average = companyAggregation.getValues().get(0).getAverage();