@@ -8,6 +8,7 @@
88{-# LANGUAGE OverloadedStrings #-}
99{-# LANGUAGE RecordWildCards #-}
1010{-# LANGUAGE UndecidableInstances #-}
11+{-# LANGUAGE NamedFieldPuns #-}
1112
1213-------------------------------------------------------------------------------
1314-- |
@@ -285,6 +286,7 @@ import Data.List (foldl', nub)
285286import Data.List.NonEmpty (NonEmpty (..), toList)
286287import qualified Data.Map.Strict as M
287288import Data.Maybe
289+import Data.Scientific (Scientific)
288290import Data.Text (Text)
289291import qualified Data.Text as T
290292import Data.Time.Calendar
@@ -1747,14 +1749,18 @@ instance ToJSON DateMathExpr where
17471749type AggregationResults = M.Map Text Value
17481750
17491751class BucketAggregation a where
1750− key :: a -> Text
1752+ key :: a -> BucketValue
17511753 docCount :: a -> Int
17521754 aggs :: a -> Maybe AggregationResults
17531755
17541756
17551757data Bucket a = Bucket { buckets :: [a]} deriving (Show)
17561758
1757−data TermsResult = TermsResult { termKey :: Text
1759+data BucketValue = TextValue Text
1760+ | ScientificValue Scientific
1761+ | BoolValue Bool deriving (Show)
1762+
1763+data TermsResult = TermsResult { termKey :: BucketValue
17581764 , termsDocCount :: Int
17591765 , termsAggs :: Maybe AggregationResults } deriving (Show)
17601766
@@ -1785,12 +1791,12 @@ instance BucketAggregation TermsResult where
17851791 aggs = termsAggs
17861792
17871793instance BucketAggregation DateHistogramResult where
1788− key = showText . dateKey
1794+ key = TextValue . showText . dateKey
17891795 docCount = dateDocCount
17901796 aggs = dateHistogramAggs
17911797
17921798instance BucketAggregation DateRangeResult where
1793− key = dateRangeKey
1799+ key = TextValue . dateRangeKey
17941800 docCount = dateRangeDocCount
17951801 aggs = dateRangeAggs
17961802
@@ -1799,6 +1805,12 @@ instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
17991805 v .: "buckets"
18001806 parseJSON _ = mempty
18011807
1808+instance FromJSON BucketValue where
1809+ parseJSON (String t) = return $ TextValue t
1810+ parseJSON (Number s) = return $ ScientificValue s
1811+ parseJSON (Bool b) = return $ BoolValue b
1812+ parseJSON _ = mempty
1813+
18021814instance FromJSON TermsResult where
18031815 parseJSON (Object v) = TermsResult <$>
18041816 v .: "key" <*>