git.iko.soy/typeable/bloodhound
on master help sign in
Move disparate termKey types out of TermsResult and into own type
cba1a57MHova
src/Database/Bloodhound/Types.hs+18−28
@@ -1746,20 +1746,13 @@ class BucketAggregation a where
1746
1746
1747data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1747data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1748
1748
1749−data TermsResult
1749+data BucketValue = TextValue Text
1750− = TextTermsResult
1750+ | ScientificValue Scientific
1751− { textTermKey :: Text
1751+ | BoolValue Bool deriving (Show)
1752− , termsDocCount :: Int
1752+
1753− , termsAggs :: Maybe AggregationResults }
1753+data TermsResult = TermsResult { termKey :: BucketValue
1754− | ScientificTermsResult
1755− { scientificTermKey :: Scientific
1756− , termsDocCount :: Int
1757− , termsAggs :: Maybe AggregationResults }
1758− | BoolTermsResult
1759− { boolTermKey :: Bool
1760− , termsDocCount :: Int
1754+ , termsDocCount :: Int
1761− , termsAggs :: Maybe AggregationResults }
1755+ , termsAggs :: Maybe AggregationResults } deriving (Show)
1762− deriving (Show)
1763
1756
1764data DateHistogramResult = DateHistogramResult { dateKey :: Int
1757data DateHistogramResult = DateHistogramResult { dateKey :: Int
1765 , dateKeyStr :: Maybe Text
1758 , dateKeyStr :: Maybe Text
@@ -1783,9 +1776,9 @@ toDateHistogram t a = M.lookup t a >>= deserialize
1783 where deserialize = parseMaybe parseJSON
1776 where deserialize = parseMaybe parseJSON
1784
1777
1785instance BucketAggregation TermsResult where
1778instance BucketAggregation TermsResult where
1786− key TextTermsResult{textTermKey} = textTermKey
1779+ key (TermsResult (TextValue t) _ _) = t
1787− key ScientificTermsResult{scientificTermKey} = T.pack $ show scientificTermKey
1780+ key (TermsResult (ScientificValue s) _ _) = T.pack $ show s
1788− key BoolTermsResult{boolTermKey} = T.pack $ show boolTermKey
1781+ key (TermsResult (BoolValue b) _ _) = T.pack $ show b
1789 docCount = termsDocCount
1782 docCount = termsDocCount
1790 aggs = termsAggs
1783 aggs = termsAggs
1791
1784
@@ -1804,21 +1797,18 @@ instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
1804 v .: "buckets"
1797 v .: "buckets"
1805 parseJSON _ = mempty
1798 parseJSON _ = mempty
1806
1799
1800+instance FromJSON BucketValue where
1801+ parseJSON (String t) = return $ TextValue t
1802+ parseJSON (Number s) = return $ ScientificValue s
1803+ parseJSON (Bool b) = return $ BoolValue b
1804+ parseJSON _ = mempty
1805+
1807instance FromJSON TermsResult where
1806instance FromJSON TermsResult where
1808 parseJSON = withObject "TermsResult" $ \o -> do
1807 parseJSON = withObject "TermsResult" $ \o -> do
1808+ termKey <- o .: "key"
1809 termsDocCount <- o .: "doc_count"
1809 termsDocCount <- o .: "doc_count"
1810 termsAggs <- o .:? "aggregations"
1810 termsAggs <- o .:? "aggregations"
1811− case "key" `HM.lookup` o of
1811+ return TermsResult{..}
1812− Just (String _) -> do
1813− textTermKey <- o .: "key"
1814− return TextTermsResult{..}
1815− Just (Number _) -> do
1816− scientificTermKey <- o .: "key"
1817− return ScientificTermsResult{..}
1818− Just (Bool _) -> do
1819− boolTermKey <- o .: "key"
1820− return BoolTermsResult{..}
1821− _ -> mempty
1822
1812
1823instance FromJSON DateHistogramResult where
1813instance FromJSON DateHistogramResult where
1824 parseJSON (Object v) = DateHistogramResult <$>
1814 parseJSON (Object v) = DateHistogramResult <$>