git.iko.soy/typeable/bloodhound
on master help sign in
Support Bool and Numeric keys in TermsResults
cbb9531MHova
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+33−9
@@ -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-- |
@@ -284,6 +285,7 @@ import Data.List (foldl', nub)
284import Data.List.NonEmpty (NonEmpty (..), toList)
285import Data.List.NonEmpty (NonEmpty (..), toList)
285import qualified Data.Map.Strict as M
286import qualified Data.Map.Strict as M
286import Data.Maybe
287import Data.Maybe
288+import Data.Scientific (Scientific)
287import Data.Text (Text)
289import Data.Text (Text)
288import qualified Data.Text as T
290import qualified Data.Text as T
289import Data.Time.Calendar
291import Data.Time.Calendar
@@ -1744,9 +1746,20 @@ class BucketAggregation a where
1744
1746
1745data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1747data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1746
1748
1747−data TermsResult = TermsResult { termKey :: Text
1749+data TermsResult
1750+ = TextTermsResult
1751+ { textTermKey :: Text
1752+ , termsDocCount :: Int
1753+ , termsAggs :: Maybe AggregationResults }
1754+ | ScientificTermsResult
1755+ { scientificTermKey :: Scientific
1748− , termsDocCount :: Int
1756+ , termsDocCount :: Int
1757+ , termsAggs :: Maybe AggregationResults }
1758+ | BoolTermsResult
1759+ { boolTermKey :: Bool
1760+ , termsDocCount :: Int
1749− , termsAggs :: Maybe AggregationResults } deriving (Show)
1761+ , termsAggs :: Maybe AggregationResults }
1762+ deriving (Show)
1750
1763
1751data DateHistogramResult = DateHistogramResult { dateKey :: Int
1764data DateHistogramResult = DateHistogramResult { dateKey :: Int
1752 , dateKeyStr :: Maybe Text
1765 , dateKeyStr :: Maybe Text
@@ -1770,7 +1783,9 @@ toDateHistogram t a = M.lookup t a >>= deserialize
1770 where deserialize = parseMaybe parseJSON
1783 where deserialize = parseMaybe parseJSON
1771
1784
1772instance BucketAggregation TermsResult where
1785instance BucketAggregation TermsResult where
1773− key = termKey
1786+ key TextTermsResult{textTermKey} = textTermKey
1787+ key ScientificTermsResult{scientificTermKey} = T.pack $ show scientificTermKey
1788+ key BoolTermsResult{boolTermKey} = T.pack $ show boolTermKey
1774 docCount = termsDocCount
1789 docCount = termsDocCount
1775 aggs = termsAggs
1790 aggs = termsAggs
1776
1791
@@ -1790,11 +1805,20 @@ instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
1790 parseJSON _ = mempty
1805 parseJSON _ = mempty
1791
1806
1792instance FromJSON TermsResult where
1807instance FromJSON TermsResult where
1793− parseJSON (Object v) = TermsResult <$>
1808+ parseJSON = withObject "TermsResult" $ \o -> do
1794− v .: "key" <*>
1795− v .: "doc_count" <*>
1809+ termsDocCount <- o .: "doc_count"
1796− v .:? "aggregations"
1810+ 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{..}
1797− parseJSON _ = mempty
1821+ _ -> mempty
1798
1822
1799instance FromJSON DateHistogramResult where
1823instance FromJSON DateHistogramResult where
1800 parseJSON (Object v) = DateHistogramResult <$>
1824 parseJSON (Object v) = DateHistogramResult <$>