MarkLogic Server 11.0 Product Documentation
AccessPlan.prototype.facetBy

AccessPlan.prototype.facetBy(
   keys as columnIdentifier[],
   [counter as columnIdentifier?]
) as ModifyPlan

Summary

This method counts values for multiple grouping key columns.

The method produces the same output as a prototype.groupToArrays function with single-column groups and a count aggregate.

Parameters
keys This parameter specifies the list of column keys for performing counts. For each column, the operation determines the unique values of that column and produces a separate count for the rows with that value.

A column can be named with a string or a column parameter function such as op.col or constructed from an expression with the op.as function. The facet can be named by providing a op.namedGroup that takes exactly one column.

counter Specifies what to count over the rows for each unique value of each key column.

By default, the operation counts the rows. To count the values of a column instead, specify the column to count with this parameter. To count documents, specify a fragment id column with op.fragmentIdCol.

Usage Notes

The op.facetBy function is a convenience for executing a prototype.groupToArrays equivalent to:

function facetBy(rowQuery, keys, counter) {
   return rowQuery.groupToArrays(keys, op.count('count', counter));
}
  

See Also

Example

// Calculate the total expenses separately for each category and for each location.

const op = require('/MarkLogic/optic');

op.fromView('main', 'expenses')
   .facetBy(['Category', 'Location'])
   .result();
  

Example

// Filter the category and location facets by different criteria.

const op = require('/MarkLogic/optic');

const expenses = op.fromView('main', 'expenses');
const catFacet =
   expenses
   .where(op.eq(op.col('Location'), 'New York'))
   .facetBy(op.namedGroup('Categories', 'Category'));
const locFacet =
   expenses
   .where(op.eq(op.col('Category'), 'Payroll'))
   .orderBy(op.desc('Amount'))
   .limit(10)
   .facetBy(op.namedGroup('Locations', 'Location'));
catFacet
   .joinCrossProduct(locFacet)
   .result();
  
Powered by MarkLogic Server | Terms of Use | Privacy Policy