git.iko.soy/typeable/bloodhound
on master help sign in
Support Bool and Numeric keys in TermsResults
commitcbb9531377e59d1312d586ff35ab66c1788130cb authorMHova <mho@sumall.com> dateWed 20 Jan 2016, 16:44 (11 years ago) parentc3df597
2 files changed+34−9
bloodhound.cabal+1 src/Database/Bloodhound/Types.hs+33−9
bloodhound.cabal+1
@@ -42,6 +42,7 @@ library
4242 transformers >= 0.2 && <0.5,
4343 http-types >= 0.8 && <0.10,
4444 vector >= 0.10.9 && <0.12,
45+ scientific >= 0.3.0.0 && <0.4.0.0,
4546 exceptions,
4647 data-default-class,
4748 blaze-builder,
src/Database/Bloodhound/Types.hs+33−9
@@ -8,6 +8,7 @@
88{-# LANGUAGE OverloadedStrings #-}
99{-# LANGUAGE RecordWildCards #-}
1010{-# LANGUAGE UndecidableInstances #-}
11+{-# LANGUAGE NamedFieldPuns #-}
1112
1213-------------------------------------------------------------------------------
1314-- |
@@ -284,6 +285,7 @@ import Data.List (foldl', nub)
284285import Data.List.NonEmpty (NonEmpty (..), toList)
285286import qualified Data.Map.Strict as M
286287import Data.Maybe
288+import Data.Scientific (Scientific)
287289import Data.Text (Text)
288290import qualified Data.Text as T
289291import Data.Time.Calendar
@@ -1744,9 +1746,20 @@ class BucketAggregation a where
17441746
17451747data Bucket a = Bucket { buckets :: [a]} deriving (Show)
17461748
1747−data TermsResult = TermsResult { termKey :: Text
1748− , termsDocCount :: Int
1749− , termsAggs :: Maybe AggregationResults } deriving (Show)
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)
17501763
17511764data DateHistogramResult = DateHistogramResult { dateKey :: Int
17521765 , dateKeyStr :: Maybe Text
@@ -1770,7 +1783,9 @@ toDateHistogram t a = M.lookup t a >>= deserialize
17701783 where deserialize = parseMaybe parseJSON
17711784
17721785instance 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
17741789 docCount = termsDocCount
17751790 aggs = termsAggs
17761791
@@ -1790,11 +1805,20 @@ instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
17901805 parseJSON _ = mempty
17911806
17921807instance FromJSON TermsResult where
1793− parseJSON (Object v) = TermsResult <$>
1794− v .: "key" <*>
1795− v .: "doc_count" <*>
1796− v .:? "aggregations"
1797− parseJSON _ = mempty
1808+ parseJSON = withObject "TermsResult" $ \o -> do
1809+ termsDocCount <- o .: "doc_count"
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{..}
1821+ _ -> mempty
17981822
17991823instance FromJSON DateHistogramResult where
18001824 parseJSON (Object v) = DateHistogramResult <$>