git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #89 from MHova/issue-88
afc5928Chris Allen
bloodhound.cabal+1
@@ -42,6 +42,7 @@ library
42 transformers >= 0.2 && <0.5,
42 transformers >= 0.2 && <0.5,
43 http-types >= 0.8 && <0.10,
43 http-types >= 0.8 && <0.10,
44 vector >= 0.10.9 && <0.12,
44 vector >= 0.10.9 && <0.12,
45+ scientific >= 0.3.0.0 && <0.4.0.0,
45 exceptions,
46 exceptions,
46 data-default-class,
47 data-default-class,
47 blaze-builder,
48 blaze-builder,
src/Database/Bloodhound/Types.hs+16−4
@@ -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 #-}
11
12
12-------------------------------------------------------------------------------
13-------------------------------------------------------------------------------
13-- |
14-- |
@@ -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
1748
1750
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
1753
1755
1754
1756
1755data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1757data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1756
1758
1759+data BucketValue = TextValue Text
1760+ | ScientificValue Scientific
1761+ | BoolValue Bool deriving (Show)
1762+
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)
1760
1766
@@ -1785,12 +1791,12 @@ instance BucketAggregation TermsResult where
1785 aggs = termsAggs
1791 aggs = termsAggs
1786
1792
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
1791
1797
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
1796
1802
@@ -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
1801
1807
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+
1802instance FromJSON TermsResult where
1814instance FromJSON TermsResult where
1803 parseJSON (Object v) = TermsResult <$>
1815 parseJSON (Object v) = TermsResult <$>
1804 v .: "key" <*>
1816 v .: "key" <*>