git.iko.soy/typeable/bloodhound
on master help sign in
Move disparate termKey types out of TermsResult and into own type
commitcba1a574c3693fa7845212393025ca48fcd4890f authorMHova <mho@sumall.com> dateThu 21 Jan 2016, 09:44 (11 years ago) parentcbb9531
1 file changed+18−28
src/Database/Bloodhound/Types.hs+18−28
src/Database/Bloodhound/Types.hs+18−28
@@ -1746,20 +1746,13 @@ class BucketAggregation a where
17461746
17471747data Bucket a = Bucket { buckets :: [a]} deriving (Show)
17481748
1749−data TermsResult
1750− = TextTermsResult
1751− { textTermKey :: Text
1752− , termsDocCount :: Int
1753− , termsAggs :: Maybe AggregationResults }
1754− | ScientificTermsResult
1755− { scientificTermKey :: Scientific
1756− , termsDocCount :: Int
1757− , termsAggs :: Maybe AggregationResults }
1758− | BoolTermsResult
1759− { boolTermKey :: Bool
1760− , termsDocCount :: Int
1761− , termsAggs :: Maybe AggregationResults }
1762− deriving (Show)
1749+data BucketValue = TextValue Text
1750+ | ScientificValue Scientific
1751+ | BoolValue Bool deriving (Show)
1752+
1753+data TermsResult = TermsResult { termKey :: BucketValue
1754+ , termsDocCount :: Int
1755+ , termsAggs :: Maybe AggregationResults } deriving (Show)
17631756
17641757data DateHistogramResult = DateHistogramResult { dateKey :: Int
17651758 , dateKeyStr :: Maybe Text
@@ -1783,9 +1776,9 @@ toDateHistogram t a = M.lookup t a >>= deserialize
17831776 where deserialize = parseMaybe parseJSON
17841777
17851778instance BucketAggregation TermsResult where
1786− key TextTermsResult{textTermKey} = textTermKey
1787− key ScientificTermsResult{scientificTermKey} = T.pack $ show scientificTermKey
1788− key BoolTermsResult{boolTermKey} = T.pack $ show boolTermKey
1779+ key (TermsResult (TextValue t) _ _) = t
1780+ key (TermsResult (ScientificValue s) _ _) = T.pack $ show s
1781+ key (TermsResult (BoolValue b) _ _) = T.pack $ show b
17891782 docCount = termsDocCount
17901783 aggs = termsAggs
17911784
@@ -1804,21 +1797,18 @@ instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
18041797 v .: "buckets"
18051798 parseJSON _ = mempty
18061799
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+
18071806instance FromJSON TermsResult where
18081807 parseJSON = withObject "TermsResult" $ \o -> do
1808+ termKey <- o .: "key"
18091809 termsDocCount <- o .: "doc_count"
18101810 termsAggs <- o .:? "aggregations"
1811− case "key" `HM.lookup` o of
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
1811+ return TermsResult{..}
18221812
18231813instance FromJSON DateHistogramResult where
18241814 parseJSON (Object v) = DateHistogramResult <$>