From 5b564916f0ef94fc0fcccd36871c0251d38aba77 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Tue, 26 Jul 2022 03:06:45 +0900 Subject: [PATCH 1/8] Add support for keyed parameter in range and histgram aggregations --- examples/aggregation.rs | 1 + src/aggregation/agg_req.rs | 10 +++++++ src/aggregation/agg_req_with_accessor.rs | 3 +-- src/aggregation/agg_result.rs | 15 +++++++++-- src/aggregation/bucket/histogram/histogram.rs | 3 +++ src/aggregation/bucket/range.rs | 9 ++++--- src/aggregation/intermediate_agg_result.rs | 26 ++++++++++++++++++- src/aggregation/metric/stats.rs | 1 + src/aggregation/mod.rs | 9 +++++++ 9 files changed, 69 insertions(+), 8 deletions(-) diff --git a/examples/aggregation.rs b/examples/aggregation.rs index ae11dc5a5a..fb0d131c17 100644 --- a/examples/aggregation.rs +++ b/examples/aggregation.rs @@ -110,6 +110,7 @@ fn main() -> tantivy::Result<()> { (9f64..14f64).into(), (14f64..20f64).into(), ], + ..Default::default() }), sub_aggregation: sub_agg_req_1.clone(), }), diff --git a/src/aggregation/agg_req.rs b/src/aggregation/agg_req.rs index 898c816ada..97118b7cf1 100644 --- a/src/aggregation/agg_req.rs +++ b/src/aggregation/agg_req.rs @@ -20,6 +20,7 @@ //! bucket_agg: BucketAggregationType::Range(RangeAggregation{ //! field: "score".to_string(), //! ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], +//! keyed: None, //! }), //! sub_aggregation: Default::default(), //! }), @@ -100,6 +101,12 @@ pub(crate) struct BucketAggregationInternal { } impl BucketAggregationInternal { + pub(crate) fn as_range(&self) -> Option<&RangeAggregation> { + match &self.bucket_agg { + BucketAggregationType::Range(range) => Some(range), + _ => None, + } + } pub(crate) fn as_histogram(&self) -> Option<&HistogramAggregation> { match &self.bucket_agg { BucketAggregationType::Histogram(histogram) => Some(histogram), @@ -264,6 +271,7 @@ mod tests { (7f64..20f64).into(), (20f64..f64::MAX).into(), ], + ..Default::default() }), sub_aggregation: Default::default(), }), @@ -312,6 +320,7 @@ mod tests { (7f64..20f64).into(), (20f64..f64::MAX).into(), ], + ..Default::default() }), sub_aggregation: Default::default(), }), @@ -337,6 +346,7 @@ mod tests { (7f64..20f64).into(), (20f64..f64::MAX).into(), ], + ..Default::default() }), sub_aggregation: agg_req2, }), diff --git a/src/aggregation/agg_req_with_accessor.rs b/src/aggregation/agg_req_with_accessor.rs index 491faf2137..0c84f99e3b 100644 --- a/src/aggregation/agg_req_with_accessor.rs +++ b/src/aggregation/agg_req_with_accessor.rs @@ -77,8 +77,7 @@ impl BucketAggregationWithAccessor { let mut inverted_index = None; let (accessor, field_type) = match &bucket { BucketAggregationType::Range(RangeAggregation { - field: field_name, - ranges: _, + field: field_name, .. }) => get_ff_reader_and_validate(reader, field_name, Cardinality::SingleValue)?, BucketAggregationType::Histogram(HistogramAggregation { field: field_name, .. diff --git a/src/aggregation/agg_result.rs b/src/aggregation/agg_result.rs index fc614990b4..f004b7a512 100644 --- a/src/aggregation/agg_result.rs +++ b/src/aggregation/agg_result.rs @@ -104,7 +104,7 @@ pub enum BucketResult { /// sub_aggregations. Range { /// The range buckets sorted by range. - buckets: Vec, + buckets: BucketEntries, }, /// This is the histogram entry for a bucket, which contains a key, count, and optionally /// sub_aggregations. @@ -114,7 +114,7 @@ pub enum BucketResult { /// If there are holes depends on the request, if min_doc_count is 0, then there are no /// holes between the first and last bucket. /// See [HistogramAggregation](super::bucket::HistogramAggregation) - buckets: Vec, + buckets: BucketEntries, }, /// This is the term result Terms { @@ -137,6 +137,17 @@ impl BucketResult { } } +/// This is the wrapper of buckets entries, which can be vector or hashmap +/// depending on if it's keyed or not. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum BucketEntries { + /// Vector format bucket entries + Vec(Vec), + /// HashMap format bucket entries + HashMap(HashMap), +} + /// This is the default entry for a bucket, which contains a key, count, and optionally /// sub_aggregations. /// diff --git a/src/aggregation/bucket/histogram/histogram.rs b/src/aggregation/bucket/histogram/histogram.rs index 70acf0f117..c26417513e 100644 --- a/src/aggregation/bucket/histogram/histogram.rs +++ b/src/aggregation/bucket/histogram/histogram.rs @@ -117,6 +117,9 @@ pub struct HistogramAggregation { /// Cannot be set in conjunction with min_doc_count > 0, since the empty buckets from extended /// bounds would not be returned. pub extended_bounds: Option, + /// Whether to return the buckets as a hash map + #[serde(skip_serializing_if = "Option::is_none")] + pub keyed: Option, } impl HistogramAggregation { diff --git a/src/aggregation/bucket/range.rs b/src/aggregation/bucket/range.rs index 7faa500e7c..0aa48bc8f9 100644 --- a/src/aggregation/bucket/range.rs +++ b/src/aggregation/bucket/range.rs @@ -35,8 +35,6 @@ use crate::{DocId, TantivyError}; /// # Limitations/Compatibility /// Overlapping ranges are not yet supported. /// -/// The keyed parameter (elasticsearch) is not yet supported. -/// /// # Request JSON Format /// ```json /// { @@ -51,13 +49,16 @@ use crate::{DocId, TantivyError}; /// } /// } /// ``` -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct RangeAggregation { /// The field to aggregate on. pub field: String, /// Note that this aggregation includes the from value and excludes the to value for each /// range. Extra buckets will be created until the first to, and last from, if necessary. pub ranges: Vec, + /// Whether to return the buckets as a hash map + #[serde(skip_serializing_if = "Option::is_none")] + pub keyed: Option, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -406,6 +407,7 @@ mod tests { let req = RangeAggregation { field: "dummy".to_string(), ranges, + ..Default::default() }; SegmentRangeCollector::from_req_and_validate( @@ -427,6 +429,7 @@ mod tests { bucket_agg: BucketAggregationType::Range(RangeAggregation { field: "fraction_f64".to_string(), ranges: vec![(0f64..0.1f64).into(), (0.1f64..0.2f64).into()], + ..Default::default() }), sub_aggregation: Default::default(), }), diff --git a/src/aggregation/intermediate_agg_result.rs b/src/aggregation/intermediate_agg_result.rs index b6b38bfde0..222225a886 100644 --- a/src/aggregation/intermediate_agg_result.rs +++ b/src/aggregation/intermediate_agg_result.rs @@ -21,7 +21,7 @@ use super::bucket::{ use super::metric::{IntermediateAverage, IntermediateStats}; use super::segment_agg_result::SegmentMetricResultCollector; use super::{Key, SerializedKey, VecWithNames}; -use crate::aggregation::agg_result::{AggregationResults, BucketEntry}; +use crate::aggregation::agg_result::{AggregationResults, BucketEntries, BucketEntry}; use crate::aggregation::bucket::TermsAggregationInternal; /// Contains the intermediate aggregation result, which is optimized to be merged with other @@ -281,6 +281,21 @@ impl IntermediateBucketResult { .unwrap_or(f64::MIN) .total_cmp(&right.from.unwrap_or(f64::MIN)) }); + + let is_keyed = req + .as_range() + .expect("unexpected aggregation, expected range aggregation") + .keyed + .is_some(); + let buckets = if is_keyed { + let mut bucket_map = HashMap::new(); + for bucket in buckets { + bucket_map.insert(bucket.key.to_string(), bucket); + } + BucketEntries::HashMap(bucket_map) + } else { + BucketEntries::Vec(buckets) + }; Ok(BucketResult::Range { buckets }) } IntermediateBucketResult::Histogram { buckets } => { @@ -291,6 +306,15 @@ impl IntermediateBucketResult { &req.sub_aggregation, )?; + let buckets = if req.as_histogram().unwrap().keyed.is_some() { + let mut bucket_map = HashMap::new(); + for bucket in buckets { + bucket_map.insert(bucket.key.to_string(), bucket); + } + BucketEntries::HashMap(bucket_map) + } else { + BucketEntries::Vec(buckets) + }; Ok(BucketResult::Histogram { buckets }) } IntermediateBucketResult::Terms(terms) => terms.into_final_result( diff --git a/src/aggregation/metric/stats.rs b/src/aggregation/metric/stats.rs index 2f704b17d0..edfec47887 100644 --- a/src/aggregation/metric/stats.rs +++ b/src/aggregation/metric/stats.rs @@ -285,6 +285,7 @@ mod tests { (7f64..19f64).into(), (19f64..20f64).into(), ], + ..Default::default() }), sub_aggregation: iter::once(( "stats".to_string(), diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index 69ae782db7..658c9c41e1 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -132,6 +132,7 @@ //! bucket_agg: BucketAggregationType::Range(RangeAggregation{ //! field: "score".to_string(), //! ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], +//! keyed: None, //! }), //! sub_aggregation: sub_agg_req_1.clone(), //! }), @@ -765,6 +766,7 @@ mod tests { bucket_agg: BucketAggregationType::Range(RangeAggregation { field: "score".to_string(), ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], + ..Default::default() }), sub_aggregation: Default::default(), }), @@ -775,6 +777,7 @@ mod tests { bucket_agg: BucketAggregationType::Range(RangeAggregation { field: "score_f64".to_string(), ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], + ..Default::default() }), sub_aggregation: Default::default(), }), @@ -785,6 +788,7 @@ mod tests { bucket_agg: BucketAggregationType::Range(RangeAggregation { field: "score_i64".to_string(), ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], + ..Default::default() }), sub_aggregation: Default::default(), }), @@ -941,6 +945,7 @@ mod tests { (7f64..19f64).into(), (19f64..20f64).into(), ], + ..Default::default() }), sub_aggregation: sub_agg_req.clone(), }), @@ -955,6 +960,7 @@ mod tests { (7f64..19f64).into(), (19f64..20f64).into(), ], + ..Default::default() }), sub_aggregation: sub_agg_req.clone(), }), @@ -969,6 +975,7 @@ mod tests { (7f64..19f64).into(), (19f64..20f64).into(), ], + ..Default::default() }), sub_aggregation: sub_agg_req, }), @@ -1416,6 +1423,7 @@ mod tests { (40000f64..50000f64).into(), (50000f64..60000f64).into(), ], + ..Default::default() }), sub_aggregation: Default::default(), }), @@ -1575,6 +1583,7 @@ mod tests { (7000f64..20000f64).into(), (20000f64..60000f64).into(), ], + ..Default::default() }), sub_aggregation: sub_agg_req_1.clone(), }), From d122f2c74e6f99ce611b8b0af151f39cb153cc79 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Tue, 26 Jul 2022 04:13:09 +0900 Subject: [PATCH 2/8] Add tests for keyed buckets --- src/aggregation/agg_req.rs | 5 ++- src/aggregation/bucket/histogram/histogram.rs | 42 ++++++++++++++++++ src/aggregation/bucket/range.rs | 43 +++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/src/aggregation/agg_req.rs b/src/aggregation/agg_req.rs index 97118b7cf1..090ad985d6 100644 --- a/src/aggregation/agg_req.rs +++ b/src/aggregation/agg_req.rs @@ -271,7 +271,7 @@ mod tests { (7f64..20f64).into(), (20f64..f64::MAX).into(), ], - ..Default::default() + keyed: Some(true), }), sub_aggregation: Default::default(), }), @@ -298,7 +298,8 @@ mod tests { { "from": 20.0 } - ] + ], + "keyed": true } } }"#; diff --git a/src/aggregation/bucket/histogram/histogram.rs b/src/aggregation/bucket/histogram/histogram.rs index c26417513e..aab68739ff 100644 --- a/src/aggregation/bucket/histogram/histogram.rs +++ b/src/aggregation/bucket/histogram/histogram.rs @@ -1398,4 +1398,46 @@ mod tests { Ok(()) } + + #[test] + fn histogram_keyed_buckets_test() -> crate::Result<()> { + let index = get_test_index_with_num_docs(false, 100)?; + + let agg_req: Aggregations = vec![( + "histogram".to_string(), + Aggregation::Bucket(BucketAggregation { + bucket_agg: BucketAggregationType::Histogram(HistogramAggregation { + field: "score_f64".to_string(), + interval: 50.0, + keyed: Some(true), + ..Default::default() + }), + sub_aggregation: Default::default(), + }), + )] + .into_iter() + .collect(); + + let res = exec_request(agg_req, &index)?; + + assert_eq!( + res, + json!({ + "histogram": { + "buckets": { + "0": { + "key": 0.0, + "doc_count": 50 + }, + "50": { + "key": 50.0, + "doc_count": 50 + } + } + } + }) + ); + + Ok(()) + } } diff --git a/src/aggregation/bucket/range.rs b/src/aggregation/bucket/range.rs index 0aa48bc8f9..c92f8c9a0f 100644 --- a/src/aggregation/bucket/range.rs +++ b/src/aggregation/bucket/range.rs @@ -457,6 +457,49 @@ mod tests { Ok(()) } + #[test] + fn range_keyed_buckets_test() -> crate::Result<()> { + let index = get_test_index_with_num_docs(false, 100)?; + + let agg_req: Aggregations = vec![( + "range".to_string(), + Aggregation::Bucket(BucketAggregation { + bucket_agg: BucketAggregationType::Range(RangeAggregation { + field: "fraction_f64".to_string(), + ranges: vec![(0f64..0.1f64).into(), (0.1f64..0.2f64).into()], + keyed: Some(true), + }), + sub_aggregation: Default::default(), + }), + )] + .into_iter() + .collect(); + + let collector = AggregationCollector::from_aggs(agg_req, None); + + let reader = index.reader()?; + let searcher = reader.searcher(); + let agg_res = searcher.search(&AllQuery, &collector).unwrap(); + + let res: Value = serde_json::from_str(&serde_json::to_string(&agg_res)?)?; + + assert_eq!( + res, + json!({ + "range": { + "buckets": { + "*-0": { "key": "*-0", "doc_count": 0, "to": 0.0}, + "0-0.1": {"key": "0-0.1", "doc_count": 10, "from": 0.0, "to": 0.1}, + "0.1-0.2": {"key": "0.1-0.2", "doc_count": 10, "from": 0.1, "to": 0.2}, + "0.2-*": {"key": "0.2-*", "doc_count": 80, "from": 0.2}, + } + } + }) + ); + + Ok(()) + } + #[test] fn bucket_test_extend_range_hole() { let buckets = vec![(10f64..20f64).into(), (30f64..40f64).into()]; From 5ab5f070edb5ae7e2f00daeb626e517e8290d83a Mon Sep 17 00:00:00 2001 From: k-yomo Date: Tue, 26 Jul 2022 18:18:38 +0900 Subject: [PATCH 3/8] Fix to use bool directory for the keyed parameter --- src/aggregation/agg_req.rs | 2 +- src/aggregation/bucket/histogram/histogram.rs | 7 ++---- src/aggregation/bucket/range.rs | 5 ++--- src/aggregation/intermediate_agg_result.rs | 5 ++--- src/aggregation/mod.rs | 22 +++++++++++++------ 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/aggregation/agg_req.rs b/src/aggregation/agg_req.rs index 090ad985d6..5c5a960fe0 100644 --- a/src/aggregation/agg_req.rs +++ b/src/aggregation/agg_req.rs @@ -271,7 +271,7 @@ mod tests { (7f64..20f64).into(), (20f64..f64::MAX).into(), ], - keyed: Some(true), + keyed: true, }), sub_aggregation: Default::default(), }), diff --git a/src/aggregation/bucket/histogram/histogram.rs b/src/aggregation/bucket/histogram/histogram.rs index aab68739ff..198a123e5e 100644 --- a/src/aggregation/bucket/histogram/histogram.rs +++ b/src/aggregation/bucket/histogram/histogram.rs @@ -48,8 +48,6 @@ use crate::{DocId, TantivyError}; /// /// # Limitations/Compatibility /// -/// The keyed parameter (elasticsearch) is not yet supported. -/// /// # JSON Format /// ```json /// { @@ -118,8 +116,7 @@ pub struct HistogramAggregation { /// bounds would not be returned. pub extended_bounds: Option, /// Whether to return the buckets as a hash map - #[serde(skip_serializing_if = "Option::is_none")] - pub keyed: Option, + pub keyed: bool, } impl HistogramAggregation { @@ -1409,7 +1406,7 @@ mod tests { bucket_agg: BucketAggregationType::Histogram(HistogramAggregation { field: "score_f64".to_string(), interval: 50.0, - keyed: Some(true), + keyed: true, ..Default::default() }), sub_aggregation: Default::default(), diff --git a/src/aggregation/bucket/range.rs b/src/aggregation/bucket/range.rs index c92f8c9a0f..46df8a0648 100644 --- a/src/aggregation/bucket/range.rs +++ b/src/aggregation/bucket/range.rs @@ -57,8 +57,7 @@ pub struct RangeAggregation { /// range. Extra buckets will be created until the first to, and last from, if necessary. pub ranges: Vec, /// Whether to return the buckets as a hash map - #[serde(skip_serializing_if = "Option::is_none")] - pub keyed: Option, + pub keyed: bool, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -467,7 +466,7 @@ mod tests { bucket_agg: BucketAggregationType::Range(RangeAggregation { field: "fraction_f64".to_string(), ranges: vec![(0f64..0.1f64).into(), (0.1f64..0.2f64).into()], - keyed: Some(true), + keyed: true, }), sub_aggregation: Default::default(), }), diff --git a/src/aggregation/intermediate_agg_result.rs b/src/aggregation/intermediate_agg_result.rs index 222225a886..f404f44d40 100644 --- a/src/aggregation/intermediate_agg_result.rs +++ b/src/aggregation/intermediate_agg_result.rs @@ -285,8 +285,7 @@ impl IntermediateBucketResult { let is_keyed = req .as_range() .expect("unexpected aggregation, expected range aggregation") - .keyed - .is_some(); + .keyed; let buckets = if is_keyed { let mut bucket_map = HashMap::new(); for bucket in buckets { @@ -306,7 +305,7 @@ impl IntermediateBucketResult { &req.sub_aggregation, )?; - let buckets = if req.as_histogram().unwrap().keyed.is_some() { + let buckets = if req.as_histogram().unwrap().keyed { let mut bucket_map = HashMap::new(); for bucket in buckets { bucket_map.insert(bucket.key.to_string(), bucket); diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index 658c9c41e1..cca2318028 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -503,13 +503,15 @@ mod tests { "bucketsL1": { "range": { "field": "score", - "ranges": [ { "to": 3.0f64 }, { "from": 3.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ] + "ranges": [ { "to": 3.0f64 }, { "from": 3.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ], + "keyed": false }, "aggs": { "bucketsL2": { "range": { "field": "score", - "ranges": [ { "to": 30.0f64 }, { "from": 30.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ] + "ranges": [ { "to": 30.0f64 }, { "from": 30.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ], + "keyed": false } } } @@ -519,12 +521,14 @@ mod tests { "field": "score", "interval": 70.0, "offset": 3.0, + "keyed": false }, "aggs": { "bucketsL2": { "histogram": { "field": "score", - "interval": 70.0 + "interval": 70.0, + "keyed": false } } } @@ -537,7 +541,8 @@ mod tests { "bucketsL2": { "histogram": { "field": "score", - "interval": 70.0 + "interval": 70.0, + "keyed": false } } } @@ -885,7 +890,8 @@ mod tests { { "from": 7.0, "to": 19.0 }, { "from": 19.0, "to": 20.0 }, { "from": 20.0 } - ] + ], + "keyed": false }, "aggs": { "average_in_range": { "avg": { "field": "score" } }, @@ -901,7 +907,8 @@ mod tests { { "from": 7.0, "to": 19.0 }, { "from": 19.0, "to": 20.0 }, { "from": 20.0 } - ] + ], + "keyed": false }, "aggs": { "average_in_range": { "avg": { "field": "score" } }, @@ -920,7 +927,8 @@ mod tests { { "from": 7.0, "to": 19.0 }, { "from": 19.0, "to": 20.0 }, { "from": 20.0 } - ] + ], + "keyed": false }, "aggs": { "average_in_range": { "avg": { "field": "score" } }, From 80a1418284b140a833fe35aba688d0b14197a9b2 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Tue, 26 Jul 2022 18:24:54 +0900 Subject: [PATCH 4/8] Use FnvHashMap for keyed bucket entries --- src/aggregation/agg_result.rs | 3 ++- src/aggregation/intermediate_agg_result.rs | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/aggregation/agg_result.rs b/src/aggregation/agg_result.rs index f004b7a512..eace07a9f7 100644 --- a/src/aggregation/agg_result.rs +++ b/src/aggregation/agg_result.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; +use fnv::FnvHashMap; use serde::{Deserialize, Serialize}; use super::agg_req::BucketAggregationInternal; @@ -145,7 +146,7 @@ pub enum BucketEntries { /// Vector format bucket entries Vec(Vec), /// HashMap format bucket entries - HashMap(HashMap), + HashMap(FnvHashMap), } /// This is the default entry for a bucket, which contains a key, count, and optionally diff --git a/src/aggregation/intermediate_agg_result.rs b/src/aggregation/intermediate_agg_result.rs index f404f44d40..63a241ffaa 100644 --- a/src/aggregation/intermediate_agg_result.rs +++ b/src/aggregation/intermediate_agg_result.rs @@ -287,7 +287,8 @@ impl IntermediateBucketResult { .expect("unexpected aggregation, expected range aggregation") .keyed; let buckets = if is_keyed { - let mut bucket_map = HashMap::new(); + let mut bucket_map = + FnvHashMap::with_capacity_and_hasher(buckets.len(), Default::default()); for bucket in buckets { bucket_map.insert(bucket.key.to_string(), bucket); } @@ -306,7 +307,8 @@ impl IntermediateBucketResult { )?; let buckets = if req.as_histogram().unwrap().keyed { - let mut bucket_map = HashMap::new(); + let mut bucket_map = + FnvHashMap::with_capacity_and_hasher(buckets.len(), Default::default()); for bucket in buckets { bucket_map.insert(bucket.key.to_string(), bucket); } From 2b333ca635b38377591436bf795960cb37bb135f Mon Sep 17 00:00:00 2001 From: k-yomo Date: Tue, 26 Jul 2022 18:35:01 +0900 Subject: [PATCH 5/8] Fix keyed param type in the comment --- src/aggregation/agg_req.rs | 2 +- src/aggregation/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aggregation/agg_req.rs b/src/aggregation/agg_req.rs index 5c5a960fe0..fd9155e51c 100644 --- a/src/aggregation/agg_req.rs +++ b/src/aggregation/agg_req.rs @@ -20,7 +20,7 @@ //! bucket_agg: BucketAggregationType::Range(RangeAggregation{ //! field: "score".to_string(), //! ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], -//! keyed: None, +//! keyed: false, //! }), //! sub_aggregation: Default::default(), //! }), diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index cca2318028..92d8ef19f3 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -132,7 +132,7 @@ //! bucket_agg: BucketAggregationType::Range(RangeAggregation{ //! field: "score".to_string(), //! ranges: vec![(3f64..7f64).into(), (7f64..20f64).into()], -//! keyed: None, +//! keyed: false, //! }), //! sub_aggregation: sub_agg_req_1.clone(), //! }), From a9b0d1a0ab86d3f00b8f5de56f4aa2f22550d1b9 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Tue, 26 Jul 2022 18:54:27 +0900 Subject: [PATCH 6/8] Fix aggreagtion examples --- src/aggregation/agg_req.rs | 3 ++- src/aggregation/mod.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/aggregation/agg_req.rs b/src/aggregation/agg_req.rs index fd9155e51c..de16c1def1 100644 --- a/src/aggregation/agg_req.rs +++ b/src/aggregation/agg_req.rs @@ -37,7 +37,8 @@ //! "ranges": [ //! { "from": 3.0, "to": 7.0 }, //! { "from": 7.0, "to": 20.0 } -//! ] +//! ], +//! "keyed": false //! } //! } //! }"#; diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index 92d8ef19f3..22a485bb17 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -93,7 +93,8 @@ //! { "from": 3.0, "to": 7.0 }, //! { "from": 7.0, "to": 20.0 }, //! { "from": 20.0 } -//! ] +//! ], +//! "keyed": false //! }, //! "aggs": { //! "average_in_range": { "avg": { "field": "score" } } From 6444516a8252f7dba9602d276f9c67b955e1200a Mon Sep 17 00:00:00 2001 From: k-yomo Date: Wed, 27 Jul 2022 01:12:56 +0900 Subject: [PATCH 7/8] User serde default for the keyed params --- src/aggregation/agg_req.rs | 3 +-- src/aggregation/bucket/histogram/histogram.rs | 1 + src/aggregation/bucket/range.rs | 1 + src/aggregation/mod.rs | 12 ++++-------- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/aggregation/agg_req.rs b/src/aggregation/agg_req.rs index de16c1def1..fd9155e51c 100644 --- a/src/aggregation/agg_req.rs +++ b/src/aggregation/agg_req.rs @@ -37,8 +37,7 @@ //! "ranges": [ //! { "from": 3.0, "to": 7.0 }, //! { "from": 7.0, "to": 20.0 } -//! ], -//! "keyed": false +//! ] //! } //! } //! }"#; diff --git a/src/aggregation/bucket/histogram/histogram.rs b/src/aggregation/bucket/histogram/histogram.rs index 198a123e5e..1d3fd82861 100644 --- a/src/aggregation/bucket/histogram/histogram.rs +++ b/src/aggregation/bucket/histogram/histogram.rs @@ -116,6 +116,7 @@ pub struct HistogramAggregation { /// bounds would not be returned. pub extended_bounds: Option, /// Whether to return the buckets as a hash map + #[serde(default)] pub keyed: bool, } diff --git a/src/aggregation/bucket/range.rs b/src/aggregation/bucket/range.rs index 46df8a0648..fe936004dc 100644 --- a/src/aggregation/bucket/range.rs +++ b/src/aggregation/bucket/range.rs @@ -57,6 +57,7 @@ pub struct RangeAggregation { /// range. Extra buckets will be created until the first to, and last from, if necessary. pub ranges: Vec, /// Whether to return the buckets as a hash map + #[serde(default)] pub keyed: bool, } diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index 22a485bb17..2bc4f5365b 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -93,8 +93,7 @@ //! { "from": 3.0, "to": 7.0 }, //! { "from": 7.0, "to": 20.0 }, //! { "from": 20.0 } -//! ], -//! "keyed": false +//! ] //! }, //! "aggs": { //! "average_in_range": { "avg": { "field": "score" } } @@ -891,8 +890,7 @@ mod tests { { "from": 7.0, "to": 19.0 }, { "from": 19.0, "to": 20.0 }, { "from": 20.0 } - ], - "keyed": false + ] }, "aggs": { "average_in_range": { "avg": { "field": "score" } }, @@ -908,8 +906,7 @@ mod tests { { "from": 7.0, "to": 19.0 }, { "from": 19.0, "to": 20.0 }, { "from": 20.0 } - ], - "keyed": false + ] }, "aggs": { "average_in_range": { "avg": { "field": "score" } }, @@ -928,8 +925,7 @@ mod tests { { "from": 7.0, "to": 19.0 }, { "from": 19.0, "to": 20.0 }, { "from": 20.0 } - ], - "keyed": false + ] }, "aggs": { "average_in_range": { "avg": { "field": "score" } }, From 9b6b60cc2bd66c825fc8cbcc01851cba01601027 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Wed, 27 Jul 2022 18:43:52 +0900 Subject: [PATCH 8/8] Remove unnecessary keyed parameter setting --- src/aggregation/mod.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index 2bc4f5365b..9c708b891b 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -503,15 +503,13 @@ mod tests { "bucketsL1": { "range": { "field": "score", - "ranges": [ { "to": 3.0f64 }, { "from": 3.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ], - "keyed": false + "ranges": [ { "to": 3.0f64 }, { "from": 3.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ] }, "aggs": { "bucketsL2": { "range": { "field": "score", - "ranges": [ { "to": 30.0f64 }, { "from": 30.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ], - "keyed": false + "ranges": [ { "to": 30.0f64 }, { "from": 30.0f64, "to": 70.0f64 }, { "from": 70.0f64 } ] } } } @@ -520,15 +518,13 @@ mod tests { "histogram": { "field": "score", "interval": 70.0, - "offset": 3.0, - "keyed": false + "offset": 3.0 }, "aggs": { "bucketsL2": { "histogram": { "field": "score", - "interval": 70.0, - "keyed": false + "interval": 70.0 } } } @@ -541,8 +537,7 @@ mod tests { "bucketsL2": { "histogram": { "field": "score", - "interval": 70.0, - "keyed": false + "interval": 70.0 } } }