Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] Added docs for aggregate_metric_double field #66306

Merged
merged 18 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
de6fc92
Added docs for aggregate_metric_double field
csoulios Nov 6, 2020
525a683
Fixes
csoulios Dec 14, 2020
8bda450
More fixes
csoulios Dec 15, 2020
02a7c3c
More fixes
csoulios Dec 15, 2020
b5aa79f
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
e5dc168
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
6821379
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
131d032
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
4b46291
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
38ced72
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
52a5213
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
c782266
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
e617471
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
866fde2
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
814cb9b
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
03384ee
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
813b5b1
Update docs/reference/mapping/types/aggregate-metric-double.asciidoc
csoulios Dec 15, 2020
828d9c6
Addressing reviewer comments
csoulios Dec 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/reference/mapping/types.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ values.
[[aggregated-data-types]]
==== Aggregate data types

<<histogram,`histogram`>>:: Pre-aggregated numerical values.
<<aggregate-metric-double,`aggregate_metric_double`>>:: Pre-aggregated metric values.
<<histogram,`histogram`>>:: Pre-aggregated numerical values in the form of a histogram.


[discrete]
Expand Down Expand Up @@ -126,6 +127,8 @@ the <<analysis-standard-analyzer,`standard` analyzer>>, the
This is the purpose of _multi-fields_. Most field types support multi-fields
via the <<multi-fields>> parameter.

include::types/aggregate-metric-double.asciidoc[]

include::types/alias.asciidoc[]

include::types/array.asciidoc[]
Expand Down
196 changes: 196 additions & 0 deletions docs/reference/mapping/types/aggregate-metric-double.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
[role="xpack"]
[testenv="basic"]
[[aggregate-metric-double]]
=== Aggregate metric field type
++++
<titleabbrev>Aggregate metric</titleabbrev>
++++

The `aggregate_metric_double` field stores pre-aggregated numeric values (`min`, `max`, `sum` and `value_count`) that can be directly
accessed by metric aggregations. An `aggregate_metric_double` field is defined as an object with one ore more of the following sub-fields:
`min`, `max`, `sum` and `value_count`.
csoulios marked this conversation as resolved.
Show resolved Hide resolved

The power of the `aggregate_metric_double` field comes from the fact that when an `aggregate_metric_double` field
participates in a metric aggregation, it returns the value of the associated sub-field for that aggregation.
For example, an `aggregate_metric_double` field that stores `min` and `max` metrics will return the `min` metric when
collected by a <<search-aggregations-metrics-min-aggregation,min>> aggregation and the `max` metric
when participating in a <<search-aggregations-metrics-max-aggregation-histogram-fields,max>> aggregation.
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[IMPORTANT]
========
* An `aggregate_metric_double` field can only store a single value for each `metric` sub-field per document.
Nested arrays are not supported.
* The values of the `min`, `max`, `sum` can be any `double` numbers. The values of `value_count` can only be
a positive long number.
========
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[[aggregate-metric-double-uses]]
==== Uses

All metric sub-fields of the `aggregate_metric_double` fields are stored as numeric <<doc-values,doc values>>
and are primarily intended for use with the following aggregations:
csoulios marked this conversation as resolved.
Show resolved Hide resolved

* <<search-aggregations-metrics-min-aggregation,min>> aggregation returns the minimum value of all `min` sub-fields.
* <<search-aggregations-metrics-max-aggregation,max>> aggregation returns the maximum value of all `max` sub-fields.
* <<search-aggregations-metrics-sum-aggregation,sum>> aggregation returns the sum of the values of all `sum` sub-fields.
* <<search-aggregations-metrics-valuecount-aggregation,value_count>> aggregation returns the sum of the values of all `value_count`
sub-fields.
* <<search-aggregations-metrics-avg-aggregation,avg>> aggregation. The result of the `avg` aggregation is computed using
the `sum` and `value_count` metrics. Therefore, there is no `avg` metric. To support `avg` aggregations, both `sum` and
`value_count` metrics must be present.
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[[aggregate-metric-double-default]]
==== Default metric

When an `aggregate_metric_double` field does not participate in any of the above aggregations,
it behaves as a `double` numeric field. To achieve this, its mapping must contain a `default_metric` which can
be one of its supported metrics. When an `aggregate_metric_double` field is accessed by a script or any
of the following queries, it will delegate the operation to its `default_metric` sub-field:

* <<query-dsl-exists-query,exists>> query.
* <<query-dsl-range-query,range>> query.
* <<query-dsl-term-query,term>> query.
* <<query-dsl-terms-query,terms>> query.
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[[aggregate-metric-double-example]]
==== Examples

The following <<indices-create-index, create index>> API request creates a new index with an `aggregate_metric_double` field used
to store aggregate metric data and sets the `max`:
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[source,console]
--------------------------------------------------
PUT stats-index
{
"mappings": {
"properties": {
"agg_metric": {
"type": "aggregate_metric_double",
"metrics": [ "min", "max", "sum", "value_count"], <1>
"default_metric": "max" <2>
}
}
}
}
--------------------------------------------------
<1> The metrics supported by this index are `min`, `max`, `sum`, `value_count`.
<2> The `default_metric` is the `max` sub-field.
csoulios marked this conversation as resolved.
Show resolved Hide resolved

The following <<docs-index_,index>> API requests store pre-aggregated data:
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[source,console]
--------------------------------------------------
PUT stats-index/_doc/1
{
"agg_metric": {
"min": -302.50,
"max": 702.30,
"sum": 200.0,
"value_count": 25
}
}

PUT stats-index/_doc/2
{
"agg_metric": {
"min": -93.00,
"max": 1702.30,
"sum": 300.00,
"value_count": 25
}
}
--------------------------------------------------

The `agg_metric` field allows us to run (`min`, `max`, `sum`, `value_count`, `avg`) aggregations
as in the following example:
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[source,console]
--------------------------------------------------
POST stats-index/_search?size=0
{
"aggs" : {
"metric_min" : { "min" : { "field" : "agg_metric" } },
"metric_max" : { "max" : { "field" : "agg_metric" } },
"metric_value_count" : { "value_count" : { "field" : "agg_metric" } },
"metric_sum" : { "sum" : { "field" : "agg_metric" } },
"metric_avg" : { "avg" : { "field" : "agg_metric" } }
}
}
--------------------------------------------------

The above aggregations will compute results based on their associated metrics
and will return the following output:
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[source,console-result]
--------------------------------------------------
{
...
"aggregations" : {
"metric_min" : {
"value" : -302.5
},
"metric_max" : {
"value" : 1702.3
},
"metric_value_count" : {
"value" : 50
},
"metric_sum" : {
"value" : 500.0
},
"metric_avg" : {
"value" : 10.0
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]


To give an example of how the `default_metric` is used in queries, let's see
a `term` query on the `agg_metric` field:
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[source,console]
--------------------------------------------------
GET stats-index/_search
{
"query": {
"term": {
"agg_metric": {
"value": 702.30,
"boost": 1.0
csoulios marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
--------------------------------------------------

Running a `term` query for a specific value, will return all documents that have this value
stored as the `default_metric` sub-field. From the result, we can see that the `aggregate_metric_double`
field delegates the `term` query to its `max` sub-field.
csoulios marked this conversation as resolved.
Show resolved Hide resolved

[source,console-result]
--------------------------------------------------
{
...
"hits" : [
{
"_index" : "stats-index",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"agg_metric" : {
"min" : -302.5,
"max" : 702.3,
"sum" : 200.0,
"value_count" : 25
}
}
}
]
}
}


--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]