return Option from as_ methods

This commit is contained in:
Pascal Seitz
2022-04-14 10:48:36 +08:00
parent 5ca04beb94
commit 4b6047f7d7
3 changed files with 16 additions and 10 deletions

View File

@@ -100,16 +100,16 @@ pub(crate) struct BucketAggregationInternal {
}
impl BucketAggregationInternal {
pub(crate) fn as_histogram(&self) -> &HistogramAggregation {
pub(crate) fn as_histogram(&self) -> Option<&HistogramAggregation> {
match &self.bucket_agg {
BucketAggregationType::Histogram(histogram) => histogram,
_ => panic!("unexpected aggregation"),
BucketAggregationType::Histogram(histogram) => Some(histogram),
_ => None,
}
}
pub(crate) fn as_term(&self) -> &TermsAggregation {
pub(crate) fn as_term(&self) -> Option<&TermsAggregation> {
match &self.bucket_agg {
BucketAggregationType::Terms(terms) => terms,
_ => panic!("unexpected aggregation"),
BucketAggregationType::Terms(terms) => Some(terms),
_ => None,
}
}
}

View File

@@ -179,15 +179,16 @@ impl BucketResult {
IntermediateBucketResult::Histogram { buckets } => {
let buckets = intermediate_buckets_to_final_buckets(
buckets,
req.as_histogram(),
req.as_histogram().expect("unexpected aggregation"),
&req.sub_aggregation,
);
BucketResult::Histogram { buckets }
}
IntermediateBucketResult::Terms(terms) => {
terms.into_final_result(req.as_term(), &req.sub_aggregation)
}
IntermediateBucketResult::Terms(terms) => terms.into_final_result(
req.as_term().expect("unexpected aggregation"),
&req.sub_aggregation,
),
}
}
}

View File

@@ -51,6 +51,9 @@ use crate::DocId;
///
/// # Limitations/Compatibility
///
/// Each segment returns up to [segment_size](TermsAggregation::segment_size) results. This
/// differences to elasticsearch behaviour.
///
/// # Request JSON Format
/// ```json
/// {
@@ -87,6 +90,8 @@ pub struct TermsAggregation {
/// include doc_count_error_upper_bound, which is an upper bound to the error on the
/// doc_count returned by each shard. Its the sum of the size of the largest bucket on
/// each segment that didnt fit into `shard_size`.
///
/// Defaults to true.
#[serde(default = "default_show_term_doc_count_error")]
pub show_term_doc_count_error: bool,