This page was generated
January  28,  2016
4:07  AM
XQuery & XSLT Built-In & Modules Function Reference

Built-In: Search - Math Lexicon Functions

MarkLogic Server provides aggregate math functions that make use of value lexicons to calculate results. The math lexicon functions differ from the math functions in the math: namespace in that math lexicon functions take frequency into consideration.

This section describes the math lexicon APIs.

Function Summary
cts:aggregate Executes a user-defined extension aggregate function against a value lexicon or n-way co-occurence of multiple value lexicons.
cts:avg [DEPRECATED: use cts:avg-aggregate] Returns a frequency-weighted average of a sequence.
cts:avg-aggregate Returns the average given a value lexicon.
cts:correlation Returns the frequency-weighted correlation given a 2-way co-occurrence.
cts:count [DEPRECATED: use cts:count-aggregate] Returns a frequency-weighted count of a sequence.
cts:count-aggregate Returns the count of a value lexicon.
cts:covariance Returns the frequency-weighted sample covariance given a 2-way co-occurrence.
cts:covariance-p Returns the frequency-weighted covariance of the population given a 2-way co-occurrence.
cts:linear-model Returns a linear model that fits the frequency-weighted data set.
cts:max Returns the maximal value given a value lexicon.
cts:median Returns a frequency-weighted median of a sequence.
cts:min Returns the minimal value given a value lexicon.
cts:percent-rank Returns the rank of a value in a data set as a percentage of the data set.
cts:percentile Returns a sequence of percentile(s) given a sequence of percentage(s).
cts:rank Returns the rank of a value in a data set.
cts:stddev Returns a frequency-weighted sample standard deviation given a value lexicon.
cts:stddev-p Returns a frequency-weighted standard deviation of the population given a value lexicon.
cts:sum [DEPRECATED: use cts:sum-aggregate] Returns a frequency-weighted sum of a sequence.
cts:sum-aggregate Returns the summary given a value lexicon.
cts:variance Returns a frequency-weighted sample variance given a value lexicon.
cts:variance-p Returns a frequency-weighted variance of the population given a value lexicon.
Function Detail
cts:aggregate(
$native-plugin as xs:string,
$aggregate-name as xs:string,
$range-indexes as cts:reference*,
$argument as item()*,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   item()*
Summary:

Executes a user-defined extension aggregate function against a value lexicon or n-way co-occurence of multiple value lexicons. Value lexicons are implemented using range indexes; consequently this function requires a range index for each lexicon specified in the function. If a specified range index does not exist an error is raised. If the "ordered" or "proximity=" option is specified, the range index must have range value positions set to true, otherwise an error is raised.

Parameters:
$native-plugin : The path to the native plugin library containing the implementation of the user-defined extension aggregate.
$aggregate-name : The name of the aggregate function.
$range-indexes : A sequence of references to range indexes. The first range index specified in this or any other aggregate function cannot be of type "nullable".
$argument : A sequence containing the arguments for the aggregate function. A map can be used to pass in multiple sequences of arguments.
$options (optional): options. The default is ().

Options include:

"any"
Co-occurrences from any fragment should be included.
"document"
Co-occurrences from document fragments should be included.
"properties"
Co-occurrences from properties fragments should be included.
"locks"
Co-occurrences from locks fragments should be included.
"fragment-frequency"
Frequency should be the number of fragments with an included co-occurrences. This option is used with cts:frequency.
"item-frequency"
Frequency should be the number of occurences of an included co-occurrence. This option is used with cts:frequency.
"ordered"
Include co-occurrences only when the value from the first lexicon appears before the value from the second lexicon. Requires that word positions be enabled for both lexicons.
"proximity=N"
Include co-occurrences only when the values appear within N words of each other. Requires that word positions be enabled for both lexicons.
"checked"
Word positions should be checked when resolving the query.
"unchecked"
Word positions should not be checked when resolving the query.
"concurrent"
Perform the work concurrently in another thread. This is a hint to the query optimizer to help parallelize the lexicon work, allowing the calling query to continue performing other work while the lexicon processing occurs. This is especially useful in cases where multiple lexicon calls occur in the same query (for example, resolving many facets in a single query).
$query (optional): Only include co-occurrences in fragments selected by the cts:query, and compute frequencies from this set of included co-occurrences. The co-occurrences do not need to match the query, but they must occur in fragments selected by the query. The fragments are not filtered to ensure they match the query, but instead selected in the same manner as "unfiltered" cts:search operations. If a string is entered, the string is treated as a cts:word-query of the specified string.
$forest-ids (optional): A sequence of IDs of forests to which the search will be constrained. An empty sequence means to search all forests in the database. The default is ().

Usage Notes:

Only one of "fragment-frequency" or "item-frequency" may be specified in the options parameter. If neither "fragment-frequency" nor "item-frequency" is specified, then the default is "fragment-frequency".

Only one of "any", "document", "properties", or "locks" may be specified in the options parameter. If none of "any", "document", "properties", or "locks" are specified and there is a $query parameter, then the default is "document". If there is no $query parameter then the default is "any".

Only one of the "checked" or "unchecked" options may be specified in the options parameter. If neither "checked" nor "unchecked" are specified, then the default is "checked".

For details, see 'Using Aggregate User-Defined Functions' in the Search Developer's Guide.


Example:

xquery version "1.0-ml";

  (:
     This query assumes an element range index is configured
     in the database for the 'Amount' element and that a plugin module
     installed under native/samplePlugin contains a UDF named "myAvg".
  :)

cts:aggregate("native/samplePlugin, "myAvg",
  cts:element-reference(xs:QName("Amount"), "type=decimal"),
  (),("fragment-frequency","concurrent"))

=> 1410.23914666667

cts:avg(
$arg as xs:anyAtomicType*
)  as   xs:anyAtomicType?
Summary:

[DEPRECATED: use cts:avg-aggregate]

Returns a frequency-weighted average of a sequence. This function works like fn:avg except each item in the sequence is multiplied by cts:frequency before summing.

Parameters:
$arg : The sequence of values to be averaged. The values should be the result of a lexicon lookup.

Usage Notes:

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will always be 0.


Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);

cts:avg(cts:element-values(xs:QName("int"), (),
           ("type=int", "item-frequency"))),
cts:avg(cts:element-values(xs:QName("int"), (),
           ("type=int", "fragment-frequency")))
=>
7
5.5



cts:avg-aggregate(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:anyAtomicType?
Summary:

Returns the average given a value lexicon. This function works like cts:avg except it performs the calculation in parallel in all data nodes then aggregates the values. It generally performs better than cts:avg, especially on large clusters.

Parameters:
$range-index : Reference to a range index.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

  (:
     This query assumes an element range index is configured
     in the database for the 'Amount' element.
  :)

cts:avg-aggregate(cts:element-reference(xs:QName("Amount")))

=> 122.2933333333333355

cts:correlation(
$value1 as cts:reference,
$value2 as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns the frequency-weighted correlation given a 2-way co-occurrence. The co-occurence is formed from the specified value lexicons. This function works like math:correlation except each pair in the input lexicons is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values.

Parameters:
$value1 : Reference to a range index. The type of the range index must be numeric.
$value2 : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
  xquery version "1.0-ml";

  (:
     This query assumes range indexes with positions are
     configured in the database for both xval and yval. It
     generates some sample data and then performs the
     aggregation in a separate transaction.
  :)
  for $i in 1 to 10
  let $j := 2 * $i
  return
    xdmp:document-insert(fn:concat($i, ".xml"),
      {$i}{
        for $a in 1 to $i
        return {$j}
      }
    );

  cts:correlation(
    cts:element-reference(xs:QName("yval")),
    cts:element-reference(xs:QName("xval")),
    ("item-frequency"))

  => 1

cts:count(
$arg as item()*,
[$maximum as xs:double]
)  as   xs:integer
Summary:

[DEPRECATED: use cts:count-aggregate]

Returns a frequency-weighted count of a sequence. This function works like fn:count except the count of each item is multiplied by cts:frequency.

Parameters:
$arg : The sequence of items to count. The items should be the result of a lexicon lookup.
$maximum (optional): The maximum value of the count to return. MarkLogic Server will stop counting when the $maximum value is reached and return the $maximum value.

Usage Notes:

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will always be 0.


Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);

cts:count(cts:element-values(xs:QName("int"), (),
           ("type=int", "item-frequency"))),
cts:count(cts:element-values(xs:QName("int"), (),
           ("type=int", "fragment-frequency")))
=>
55
10



cts:count-aggregate(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:integer
Summary:

Returns the count of a value lexicon. This function works like cts:count except it performs the counting in parallel in all data nodes then aggregates the values. It generally performs better than cts:count, especially on large clusters.

Parameters:
$range-index : Reference to a range index.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.


cts:covariance(
$value1 as cts:reference,
$value2 as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns the frequency-weighted sample covariance given a 2-way co-occurrence. The co-occurence is formed from the specified value lexicons. This function works like math:covariance except each pair in the input lexicons is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values.

Parameters:
$value1 : Reference to a range index. The type of the range index must be numeric.
$value2 : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
  xquery version "1.0-ml";

  (:
     This query assumes range indexes with positions are
     configured in the database for both xval and yval. It
     generates some sample data and then performs the
     aggregation in a separate transaction.
  :)
  for $i in 1 to 10
  let $j := 2 * $i
  return
    xdmp:document-insert(fn:concat($i, ".xml"),
      {$i}{
        for $a in 1 to $i
        return {$j}
      }
    );

  cts:covariance(
    cts:element-reference(xs:QName("yval")),
    cts:element-reference(xs:QName("xval")),
    ("item-frequency"))

  => 12.2222222222222

cts:covariance-p(
$value1 as cts:reference,
$value2 as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns the frequency-weighted covariance of the population given a 2-way co-occurrence. The co-occurence is formed from the specified value lexicons. This function works like math:covariance-p except each pair in the co-occurrence is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values.

Parameters:
$value1 : Reference to a range index. The type of the range index must be numeric.
$value2 : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
  xquery version "1.0-ml";

  (:
     This query assumes range indexes with positions are
     configured in the database for both xval and yval. It
     generates some sample data and then performs the
     aggregation in a separate transaction.
  :)
  for $i in 1 to 10
  let $j := 2 * $i
  return
    xdmp:document-insert(fn:concat($i, ".xml"),
      {$i}{
        for $a in 1 to $i
        return {$j}
      }
    );

  cts:covariance-p(
    cts:element-reference(xs:QName("yval")),
    cts:element-reference(xs:QName("xval")),
    ("item-frequency"))

  => 12

cts:linear-model(
$values as cts:reference*,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   math:linear-model?
Summary:

Returns a linear model that fits the frequency-weighted data set. The input data is a co-occurrence, formed from the specified value lexicons. The length of the input lexicon sequence should be 2, as currently only simple linear regression model is supported. The first lexicon should be the value of the dependent variable while the other lexicon should be the value of the independent variable. This function works like math:linear-model except each pair in the input lexicons is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values.

Parameters:
$values : References to two range indexes. The types of the range indexes must be numeric. If the size of this sequence is not 2, the function returns the empty sequence.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";
  (:
     This query assumes range indexes of type unsignedLong
     with positions are configured in the database for both
     xval and yval. It generates some sample data and then
     performs the aggregation in a separate transaction.
  :)
for $i in (1 to 10)
let $j := 2*$i + 1
let $uri := concat($i, ".xml")
return
   xdmp:document-insert($uri, <a><xval>{$i}</xval><yval>{$j}</yval></a>);

cts:linear-model(
    (cts:element-reference(xs:QName("yval"), ("type=unsignedLong")),
    cts:element-reference(xs:QName("xval"), ("type=unsignedLong"))),
    "item-frequency")
=>
math:linear-model(
  <math:linear-model intercept="1" coefficients="2" rsquared="1"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:math="http://marklogic.com/xdmp/math"/>)

cts:max(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:anyAtomicType?
Summary:

Returns the maximal value given a value lexicon. This function performs the calculation in parallel in all data nodes then aggregates the values. It generally performs better than fn:max, especially on large clusters.

Parameters:
$range-index : Reference to a range index.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

  (:
     This query assumes an element range index is configured
     in the database for the 'Amount' element.
  :)

cts:max(cts:element-reference(xs:QName("Amount")))

=> 162.9500000000000034

cts:median(
$arg as xs:double*
)  as   xs:double?
Summary:

Returns a frequency-weighted median of a sequence. This function works like math:median except each item in the sequence is repeated cts:frequency times before calculating the median. If $arg is the empty sequence, the function returns the empty sequence.

Parameters:
$arg : The sequence of values. The values should be the result of a lexicon lookup.

Usage Notes:

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will be the empty sequence.


Example:
xquery version "1.0-ml";

(:
   This query assumes an element range index of type 'int'
   is configured for the 'int' element in the database. It
   generates some sample data and then performs the aggregation
   in a separate transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);

cts:median(cts:element-values(xs:QName("int"), (),
           ("type=int", "item-frequency"))),
cts:median(cts:element-values(xs:QName("int"), (),
           ("type=int", "fragment-frequency")))
=>
7
5.5



cts:min(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:anyAtomicType?
Summary:

Returns the minimal value given a value lexicon. This function performs the calculation in parallel in all data nodes then aggregates the values. It generally performs better than fn:min, especially on large clusters.

Parameters:
$range-index : Reference to a range index.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

  (:
     This query assumes an element range index is configured
     in the database for the 'Amount' element.
  :)

cts:min(cts:element-reference(xs:QName("Amount")))

=> 59.95000000000000125

cts:percent-rank(
$arg as xs:anyAtomicType*,
$value as xs:anyAtomicType,
[$options as xs:string*]
)  as   xs:double?
Summary:

Returns the rank of a value in a data set as a percentage of the data set. This function works like math:percent-rank except each item in the sequence is repeated cts:frequency times before calculating the rank.

Parameters:
$arg : The sequence of values.
$value : The value to be "ranked".
$options (optional): Options. The default is ().

Options include:

"ascending"(default)
Rank the value as if the sequence was sorted in ascending order.
"descending"
Rank the value as if the sequence was sorted in descending order.
"collation=URI"
Applies only when $arg is of the xs:string type. If no specified, the default collation is used.
"coordinate-system=name"
Applies only when $arg is of the cts:point type. If no specified, the default coordinate system is used.

Usage Notes:

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will be the empty sequence.


Example:
  xquery version "1.0-ml";

  (:
     This query assumes an int range index
     is configured in the database. It
     generates some sample data and then
     performs the aggregation in a separate
     transaction.
  :)
  for $x in 1 to 10
  return
    xdmp:document-insert(fn:concat($x, ".xml"),
      <my-element>{
        for $y in 1 to $x
        return <int>{$x}</int>
    }</my-element>);

  cts:percent-rank(
    cts:element-values(
      xs:QName("int"), (), ("type=int", "item-frequency")), 9)

  => 0.672727272727273

cts:percentile(
$arg as xs:double*,
$p as xs:double*
)  as   xs:double*
Summary:

Returns a sequence of percentile(s) given a sequence of percentage(s). This function works like math:percentile except each item in the sequence is repeated cts:frequency times before calculating the percentiles(s). The function returns the empty sequence if either $arg or $p is the empty sequence.

Parameters:
$arg : The sequence of values. The values should be the result of a lexicon lookup.
$p : The sequence of percentage(s).

Usage Notes:

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will be the empty sequence.


Example:
xquery version "1.0-ml";

(:
   This query assumes an element range index of type 'int'
   is configured for the 'int' element in the database. It
   generates some sample data and then performs the aggregation
   in a separate transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);

cts:percentile(cts:element-values(xs:QName("int"), (),
               ("type=int", "item-frequency")), (0.25,0.5,0.75))
=>
5,7,9



cts:rank(
$arg as xs:anyAtomicType*,
$value as xs:anyAtomicType,
[$options as xs:string*]
)  as   xs:integer?
Summary:

Returns the rank of a value in a data set. This function works like math:rank except each item in the sequence is repeated cts:frequency times before calculating the rank.

Parameters:
$arg : The sequence of values.
$value : The value to be "ranked".
$options (optional): Options. The default is ().

Options include:

"ascending"(default)
Rank the value as if the sequence was sorted in ascending order.
"descending"
Rank the value as if the sequence was sorted in descending order.
"collation=URI"
Applies only when $arg is of the xs:string type. If no specified, the default collation is used.
"coordinate-system=name"
Applies only when $arg is of the cts:point type. If no specified, the default coordinate system is used.

Usage Notes:

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will be the empty sequence.


Example:
  xquery version "1.0-ml";

  (:
     This query assumes an int range index
     is configured in the database. It
     generates some sample data and then
     performs the aggregation in a separate
     transaction.
  :)
  for $x in 1 to 10
  return
    xdmp:document-insert(fn:concat($x, ".xml"),
      <my-element>{
        for $y in 1 to $x
        return <int>{$x}</int>
    }</my-element>);

  cts:rank(cts:element-values(
    xs:QName("int"), (), ("type=int", "item-frequency")), 9)

  => 37

cts:stddev(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns a frequency-weighted sample standard deviation given a value lexicon. This function works like math:stddev except each item in the lexicon is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values. The function returns the empty sequence if the lexicon contains no value.

Parameters:
$range-index : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);


cts:stddev(
  cts:element-reference(xs:QName("int"),"type=int"),
    ("item-frequency","concurrent")),
cts:stddev(
  cts:element-reference(xs:QName("int"),"type=int"),
    ("fragment-frequency","concurrent"))
=>
2.47206616236522
3.02765035409749


cts:stddev-p(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns a frequency-weighted standard deviation of the population given a value lexicon. This function works like math:stddev-p except each item in the lexicon is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values. The function returns the empty sequence if the lexicon contains no value.

Parameters:
$range-index : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);


cts:stddev-p(
   cts:element-reference(xs:QName("int"),"type=int"),
      ("item-frequency","concurrent")),
cts:stddev-p(
   cts:element-reference(xs:QName("int"),"type=int"),
      ("fragment-frequency","concurrent"))
=>
2.44948974278318
2.87228132326901


cts:sum(
$arg as xs:anyAtomicType*,
[$zero as xs:anyAtomicType?]
)  as   xs:anyAtomicType?
Summary:

[DEPRECATED: use cts:sum-aggregate]

Returns a frequency-weighted sum of a sequence. This function works like fn:sum except each item in the sequence is multiplied by cts:frequency before summing.

Parameters:
$arg : The sequence of values to be summed. The values should be the result of a lexicon lookup.
$zero (optional): The value to return as zero if the input sequence is the empty sequence.

Usage Notes:

The cts:frequency of the result is the sum of the frequencies of the sequence.

This function is designed to take a sequence of values returned by a lexicon function (for example, cts:element-values); if you input non-lexicon values, the result will always be 0.


Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);

cts:sum(cts:element-values(xs:QName("int"), (),
           ("type=int", "item-frequency"))),
cts:sum(cts:element-values(xs:QName("int"), (),
           ("type=int", "fragment-frequency")))
=>
385
55



cts:sum-aggregate(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:anyAtomicType?
Summary:

Returns the summary given a value lexicon. This function works like cts:sum except it performs the calculation in parallel in all data nodes then aggregates the values. It generally performs better than cts:sum, especially on large clusters.

Parameters:
$range-index : Reference to a range index.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

  (:
     This query assumes an element range index is configured
     in the database for the 'Amount' element.
  :)

cts:sum-aggregate(cts:element-reference(xs:QName("Amount")))

=> 733.7600000000000128

cts:variance(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns a frequency-weighted sample variance given a value lexicon. This function works like math:variance except each item in the lexicon is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values. The function returns the empty sequence if the lexicon contains no value.

Parameters:
$range-index : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);


cts:variance(
  cts:element-reference(xs:QName("int"),"type=int"),
       ("item-frequency","concurrent")),
cts:variance(
  cts:element-reference(xs:QName("int"),"type=int"),
       ("fragment-frequency","concurrent"))
=>
6.11111111111111
9.16666666666667


cts:variance-p(
$range-index as cts:reference,
[$options as xs:string*],
[$query as cts:query?],
[$forest-ids as xs:unsignedLong*]
)  as   xs:double?
Summary:

Returns a frequency-weighted variance of the population given a value lexicon. This function works like math:variance-p except each item in the lexicon is counted cts:frequency times. This function performs the calculation in parallel in all data nodes then aggregates the values. The function returns the empty sequence if the lexicon contains no value.

Parameters:
$range-index : Reference to a range index. The type of the range index must be numeric.
$options (optional): Same as the "options" parameter in cts:aggregate.
$query (optional): Same as the "query" parameter in cts:aggregate.
$forest-ids (optional): Same as the "forest-ids" parameter in cts:aggregate.

Example:
xquery version "1.0-ml";

(:
   This query assumes an int range index
   is configured in the database. It
   generates some sample data and then
   performs the aggregation in a separate
   transaction.
:)
for $x in 1 to 10
return
xdmp:document-insert(fn:concat($x, ".xml"),
<my-element>{
  for $y in 1 to $x
  return <int>{$x}</int>
}</my-element>);


cts:variance-p(
  cts:element-reference(xs:QName("int"),"type=int"),
    ("item-frequency","concurrent")),
cts:variance-p(
  cts:element-reference(xs:QName("int"),"type=int"),
    ("fragment-frequency","concurrent"))
=>
6
8.25