Expand description
§Aggregations
An aggregation summarizes your data as statistics on buckets or metrics.
Aggregations can provide answer to questions like:
- What is the average price of all sold articles?
- How many errors with status code 500 do we have per day?
- What is the average listing price of cars grouped by color?
There are two categories: Metrics and Buckets.
§Prerequisite
Currently aggregations work only on fast fields. Fast fields
of type u64, f64, i64, date and fast fields on text fields.
§Usage
To use aggregations, build an aggregation request by constructing
Aggregations.
Create an AggregationCollector from this request. AggregationCollector implements the
Collector trait and can be passed as collector into
Searcher::search().
§JSON Format
Aggregations request and result structures de/serialize into elasticsearch compatible JSON.
Notice: Intermediate aggregation results should not be de/serialized via JSON format. Postcard is a good choice.
let agg_req: Aggregations = serde_json::from_str(json_request_string).unwrap();
let collector = AggregationCollector::from_aggs(agg_req, None);
let searcher = reader.searcher();
let agg_res = searcher.search(&term_query, &collector).unwrap_err();
let json_response_string: String = &serde_json::to_string(&agg_res)?;§Supported Aggregations
§Example
Compute the average metric, by building agg_req::Aggregations, which is built from an
(String, agg_req::Aggregation) iterator.
Requests are compatible with the elasticsearch JSON request format.
use tantivy::aggregation::agg_req::Aggregations;
let elasticsearch_compatible_json_req = r#"
{
"average": {
"avg": { "field": "score" }
},
"range": {
"range": {
"field": "score",
"ranges": [
{ "to": 3.0 },
{ "from": 3.0, "to": 7.0 },
{ "from": 7.0, "to": 20.0 },
{ "from": 20.0 }
]
},
"aggs": {
"average_in_range": { "avg": { "field": "score" } }
}
}
}
"#;
let agg_req: Aggregations =
serde_json::from_str(elasticsearch_compatible_json_req).unwrap();§Code Organization
Check the README on github to see how the code is organized.
§Nested Aggregation
Buckets can contain sub-aggregations. In this example we create buckets with the range aggregation and then calculate the average on each bucket.
use tantivy::aggregation::agg_req::*;
use serde_json::json;
let agg_req_1: Aggregations = serde_json::from_value(json!({
"rangef64": {
"range": {
"field": "score",
"ranges": [
{ "from": 3, "to": 7000 },
{ "from": 7000, "to": 20000 },
{ "from": 50000, "to": 60000 }
]
},
"aggs": {
"average_in_range": { "avg": { "field": "score" } }
}
},
}))
.unwrap();§Distributed Aggregation
When the data is distributed on different Index instances, the
DistributedAggregationCollector provides functionality to merge data between independent
search calls by returning
IntermediateAggregationResults.
IntermediateAggregationResults provides the
merge_fruits method
to merge multiple results. The merged result can then be converted into
AggregationResults via the
into_final_result method.
Modules§
- agg_req
- Contains the aggregation request tree. Used to build an
AggregationCollector. - agg_
result - Contains the final aggregation tree.
- bucket
- Module for all bucket aggregations.
- intermediate_
agg_ result - Contains the intermediate aggregation tree, that can be merged. Intermediate aggregation results can be used to merge results between segments or between indices.
- metric
- Module for all metric aggregations.
Structs§
- AggContext
Params - Context parameters for aggregation execution
- Aggregation
Collector - Collector for aggregations.
- Aggregation
Limits Guard - Aggregation memory limit after which the request fails. Defaults to DEFAULT_MEMORY_LIMIT (500MB). The limit is shared by all SegmentCollectors
- Aggregation
Segment Collector AggregationSegmentCollectordoes the aggregation collection on a segment.- Distributed
Aggregation Collector - Collector for distributed aggregations.
- Value
Source Registry - Named computed sources available to an aggregation request.
Enums§
- Aggregation
Error - Error that may occur when opening a directory
- Key
- The key to identify a bucket.
Constants§
- DEFAULT_
BUCKET_ LIMIT - The default max bucket count, before the aggregation fails.
Traits§
- Value
Source - A source of values for a block of documents.
- Value
Source Dictionary - Maps term ords of a
StrValueSourceback into terms. - Value
Source Provider - Creates a value source for each segment.
Type Aliases§
- Bucket
Id - A bucket id is a dense identifier for a bucket within an aggregation. It is used to index into a Vec that hold per-bucket data.
- Serialized
Key - The serialized key is used in a
HashMap.