@@ -8,6 +8,7 @@
8{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE OverloadedStrings #-}
9{-# LANGUAGE RecordWildCards #-}
9{-# LANGUAGE RecordWildCards #-}
10{-# LANGUAGE UndecidableInstances #-}
10{-# LANGUAGE UndecidableInstances #-}
11+{-# LANGUAGE NamedFieldPuns #-}
12-------------------------------------------------------------------------------
13-------------------------------------------------------------------------------
@@ -285,6 +286,7 @@ import Data.List (foldl', nub)
285import Data.List.NonEmpty (NonEmpty (..), toList)
286import Data.List.NonEmpty (NonEmpty (..), toList)
286import qualified Data.Map.Strict as M
287import qualified Data.Map.Strict as M
287import Data.Maybe
288import Data.Maybe
289+import Data.Scientific (Scientific)
288import Data.Text (Text)
290import Data.Text (Text)
289import qualified Data.Text as T
291import qualified Data.Text as T
290import Data.Time.Calendar
292import Data.Time.Calendar
@@ -1747,14 +1749,18 @@ instance ToJSON DateMathExpr where
1747type AggregationResults = M.Map Text Value
1749type AggregationResults = M.Map Text Value
1749class BucketAggregation a where
1751class BucketAggregation a where
1750− key :: a -> Text
1752+ key :: a -> BucketValue
1751 docCount :: a -> Int
1753 docCount :: a -> Int
1752 aggs :: a -> Maybe AggregationResults
1754 aggs :: a -> Maybe AggregationResults
1755data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1757data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1759+data BucketValue = TextValue Text
1760+ | ScientificValue Scientific
1761+ | BoolValue Bool deriving (Show)
1757−data TermsResult = TermsResult { termKey :: Text
1763+data TermsResult = TermsResult { termKey :: BucketValue
1758 , termsDocCount :: Int
1764 , termsDocCount :: Int
1759 , termsAggs :: Maybe AggregationResults } deriving (Show)
1765 , termsAggs :: Maybe AggregationResults } deriving (Show)
@@ -1785,12 +1791,12 @@ instance BucketAggregation TermsResult where
1785 aggs = termsAggs
1791 aggs = termsAggs
1787instance BucketAggregation DateHistogramResult where
1793instance BucketAggregation DateHistogramResult where
1788− key = showText . dateKey
1794+ key = TextValue . showText . dateKey
1789 docCount = dateDocCount
1795 docCount = dateDocCount
1790 aggs = dateHistogramAggs
1796 aggs = dateHistogramAggs
1792instance BucketAggregation DateRangeResult where
1798instance BucketAggregation DateRangeResult where
1793− key = dateRangeKey
1799+ key = TextValue . dateRangeKey
1794 docCount = dateRangeDocCount
1800 docCount = dateRangeDocCount
1795 aggs = dateRangeAggs
1801 aggs = dateRangeAggs
@@ -1799,6 +1805,12 @@ instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
1799 v .: "buckets"
1805 v .: "buckets"
1800 parseJSON _ = mempty
1806 parseJSON _ = mempty
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
1802instance FromJSON TermsResult where
1814instance FromJSON TermsResult where
1803 parseJSON (Object v) = TermsResult <$>
1815 parseJSON (Object v) = TermsResult <$>
1804 v .: "key" <*>
1816 v .: "key" <*>