Repository: elastic-rs/elastic (language: rust)

Issue:
Provide access to raw aggregations
#257 addresses the ability to work with additional aggregation types (I think).

I have a case where I am using cardinality aggs to calculate a count in ES, but this kind of aggregation can't be handled at all in the current system.

Even a simple `fn raw_aggs(&self) -> Option<&::serde_json::Value>` would be acceptable for the time being.

Also, somewhat off-topic, but what is the rationale for flattening typical "bucket" aggs as is done currently? It seems to break with ES conventions without offering any real benefits (in my opinion/as far as I can tell).


Discussion / hints:
Yes, I've been meaning to generalise our support for aggregations. The original use-case was fairly specific for collecting metrics.

I think an `aggs_raw` method on `SearchResponse` that returns a `Value` is something we'll want to have, even in the future API where we'll keep aggs as `Value`s internally.

Fix patch:
diff --git a/src/responses/src/search.rs b/src/responses/src/search.rs
index c082437ac5..af22c7be97 100644
--- a/src/responses/src/search.rs
+++ b/src/responses/src/search.rs
@@ -133,6 +133,13 @@ impl<T> SearchResponse<T> {
     pub fn aggs(&self) -> Aggs {
         Aggs::new(self.aggregations.as_ref())
     }
+
+    /**
+    Get a reference to the raw aggregation value.
+    */
+    pub fn aggs_raw(&self) -> Option<&Value> {
+        self.aggregations.as_ref().map(|wrapper| &wrapper.0)
+    }
 }
 
 impl<T: DeserializeOwned> IsOk for SearchResponse<T> {


Test patch:
diff --git a/src/responses/tests/search/mod.rs b/src/responses/tests/search/mod.rs
index b8670325b7..f76418439f 100644
--- a/src/responses/tests/search/mod.rs
+++ b/src/responses/tests/search/mod.rs
@@ -107,6 +107,12 @@ fn success_parse_simple_nested_aggs() {
         .unwrap();
 
     assert_eq!(deserialized.aggs().count(), 124);
+
+    let doc_count = deserialized
+        .aggs_raw()
+        .and_then(|aggs| aggs["timechart"]["buckets"][0]["doc_count"].as_u64());
+
+    assert_eq!(Some(101), doc_count);
 }
 
 #[test]