= Analytics Reduction Functions
:page-tocclass: right
:page-toclevels: 2
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

Reduction functions reduce the values of <<analytics-expression-sources.adoc#analytics-expression-sources,sources>>
and/or unreduced <<analytics-mapping-functions.adoc#analytics-mapping-functions,mapping functions>>
for every Solr Document to a single value.

Below is a list of all reduction functions provided by the Analytics Component.
These can be combined using mapping functions to implement more complex functionality.

== Counting Reductions

=== Count
The number of existing values for an expression. For single-valued expressions, this is equivalent to `docCount`.
If no expression is given, the number of matching documents is returned.

`count()` \=> `< _Single Long_ >`
`count(< T >)` \=> `< _Single Long_ >`

=== Doc Count
The number of documents for which an expression has existing values. For single-valued expressions, this is equivalent to `count`.
If no expression is given, the number of matching documents is returned.

`doc_count()` \=> `< _Single Long_ >`

`doc_count(< T >)` \=> `< _Single Long_ >`

=== Missing
The number of documents for which an expression has no existing value.

`missing(< T >)` \=> `< _Single Long_ >`

[[analytics-unique]]
=== Unique
The number of unique values for an expression. This function accepts `Numeric`, `Date` and `String` expressions.

`unique(< T >)` \=> `< _Single Long_ >`

== Math Reductions

=== Sum
Returns the sum of all values for the expression.

`sum(< _Double_ >)` \=> `< _Single Double_ >`

=== Variance
Returns the variance of all values for the expression.

`variance(< _Double_ >)` \=> `< _Single Double_ >`

=== Standard Deviation
Returns the standard deviation of all values for the expression.

`stddev(< _Double_ >)` \=> `< _Single Double_ >`

=== Mean
Returns the arithmetic mean of all values for the expression.

`mean(< _Double_ >)` \=> `< _Single Double_ >`

=== Weighted Mean
Returns the arithmetic mean of all values for the second expression weighted by the values of the first expression.

`wmean(< _Double_ >, < _Double_ >)` \=> `< _Single Double_ >`

NOTE: The expressions must satisfy the rules for `mult` function parameters.

== Ordering Reductions

=== Minimum
Returns the minimum value for the expression. This function accepts `Numeric`, `Date` and `String` expressions.

`min(< T >)` \=> `< _Single_ T >`

=== Maximum
Returns the maximum value for the expression. This function accepts `Numeric`, `Date` and `String` expressions.

`max(< T >)` \=> `< _Single_ T >`

=== Median
Returns the median of all values for the expression. This function accepts `Numeric` and `Date` expressions.

`median(< T >)` \=> `< _Single_ T >`

=== Percentile
Calculates the given percentile of all values for the expression.
This function accepts `Numeric`, `Date` and `String` expressions for the 2^nd^ parameter.

The percentile, given as the 1^st^ parameter, must be a <<analytics-expression-sources.adoc#numeric,constant double>> between [0, 100).

`percentile(<Constant Double>, < T >)` \=> `< _Single_ T >`

=== Ordinal
Calculates the given ordinal of all values for the expression.
This function accepts `Numeric`, `Date` and `String` expressions for the 2^nd^ parameter.
The ordinal, given as the 1^st^ parameter, must be a <<analytics-expression-sources.adoc#numeric,constant integer>>.
*0 is not accepted as an ordinal value.*

If the ordinal is positive, the returned value will be the _n_^th^ smallest value.

If the ordinal is negative, the returned value will be the _n_^th^ largest value.

`ordinal(<Constant Int>, < T >)` \=> `< _Single_ T >`
