op:facet-by( $plan as map:map, $keys as item()+, [$counter as item()?] ) as map:map
This method counts values for multiple grouping key columns.
The method produces the same output as a op:group-to-arrays function with single-column groups and a count aggregate.
Parameters | |
---|---|
$plan | The Optic Plan. You can either use the XQuery => chaining operator or specify the variable that captures the return value from the previous operation. |
$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:named-group 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:fragment-id-col. |
The op:facet-by
function is a convenience for executing
a op:group-to-arrays equivalent to:
declare function local:facet-by($rowQuery as map:map, $keys as item()+, $counter as item()?) as map:map { $rowQuery => op:group-to-arrays($keys, op:count("count", $counter)) };
(: Calculate the total expenses separately for each category and for each location. :) xquery version "1.0-ml"; import module namespace op="http://marklogic.com/optic" at "/MarkLogic/optic.xqy"; op:from-view("main", "expenses") => op:facet-by(("Category", "Location")) => op:result()
(: Filter the category and location facets by different criteria. :) xquery version "1.0-ml"; import module namespace op="http://marklogic.com/optic" at "/MarkLogic/optic.xqy"; let $expenses := op:from-view("main", "expenses") let $catFacet := $expenses => op:where(op:eq(op:col("Location"), "New York")) => op:facet-by(op:named-group("Categories", "Category")) let $locFacet := $expenses => op:where(op:eq(op:col("Category"), "Payroll")) => op:order-by(op:desc("Amount")) => op:limit(10) => op:facet-by(op:named-group("Locations", "Location")) return $catFacet => op:join-cross-product($locFacet) => op:result()