git.iko.soy/typeable/bloodhound
on master help sign in
add stats aggregation
b547b18David Smith
src/Database/V5/Bloodhound/Types.hs+24
@@ -47,6 +47,8 @@ module Database.V5.Bloodhound.Types
47 , mkDateHistogram
47 , mkDateHistogram
48 , mkCardinalityAggregation
48 , mkCardinalityAggregation
49 , mkDocVersion
49 , mkDocVersion
50+ , mkStatsAggregation
51+ , mkExtendedStatsAggregation
50 , docVersionNumber
52 , docVersionNumber
51 , toMissing
53 , toMissing
52 , toTerms
54 , toTerms
@@ -341,6 +343,7 @@ module Database.V5.Bloodhound.Types
341 , DateMathModifier(..)
343 , DateMathModifier(..)
342 , DateMathUnit(..)
344 , DateMathUnit(..)
343 , TopHitsAggregation(..)
345 , TopHitsAggregation(..)
346+ , StatisticsAggregation(..)
344
347
345 , Highlights(..)
348 , Highlights(..)
346 , FieldHighlight(..)
349 , FieldHighlight(..)
@@ -1715,6 +1718,7 @@ data Aggregation = TermsAgg TermsAggregation
1715 | DateRangeAgg DateRangeAggregation
1718 | DateRangeAgg DateRangeAggregation
1716 | MissingAgg MissingAggregation
1719 | MissingAgg MissingAggregation
1717 | TopHitsAgg TopHitsAggregation
1720 | TopHitsAgg TopHitsAggregation
1721+ | StatsAgg StatisticsAggregation
1718 deriving (Eq, Read, Show, Generic, Typeable)
1722 deriving (Eq, Read, Show, Generic, Typeable)
1719
1723
1720data TopHitsAggregation = TopHitsAggregation
1724data TopHitsAggregation = TopHitsAggregation
@@ -1792,6 +1796,14 @@ data ValueCountAggregation = FieldValueCount FieldName
1792data FilterAggregation = FilterAggregation { faFilter :: Filter
1796data FilterAggregation = FilterAggregation { faFilter :: Filter
1793 , faAggs :: Maybe Aggregations} deriving (Eq, Read, Show, Generic, Typeable)
1797 , faAggs :: Maybe Aggregations} deriving (Eq, Read, Show, Generic, Typeable)
1794
1798
1799+data StatisticsAggregation = StatisticsAggregation { statsType :: StatsType
1800+ , statsField :: FieldName } deriving (Eq, Read, Show, Generic, Typeable)
1801+
1802+data StatsType
1803+ = Basic
1804+ | Extended
1805+ deriving (Eq, Read, Show, Generic, Typeable)
1806+
1795mkTermsAggregation :: Text -> TermsAggregation
1807mkTermsAggregation :: Text -> TermsAggregation
1796mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1808mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1797
1809
@@ -1804,6 +1816,12 @@ mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothi
1804mkCardinalityAggregation :: FieldName -> CardinalityAggregation
1816mkCardinalityAggregation :: FieldName -> CardinalityAggregation
1805mkCardinalityAggregation t = CardinalityAggregation t Nothing
1817mkCardinalityAggregation t = CardinalityAggregation t Nothing
1806
1818
1819+mkStatsAggregation :: FieldName -> StatisticsAggregation
1820+mkStatsAggregation = StatisticsAggregation Basic
1821+
1822+mkExtendedStatsAggregation :: FieldName -> StatisticsAggregation
1823+mkExtendedStatsAggregation = StatisticsAggregation Extended
1824+
1807instance ToJSON Version where
1825instance ToJSON Version where
1808 toJSON Version {..} = object ["number" .= number
1826 toJSON Version {..} = object ["number" .= number
1809 ,"build_hash" .= build_hash
1827 ,"build_hash" .= build_hash
@@ -1928,6 +1946,12 @@ instance ToJSON Aggregation where
1928 ]
1946 ]
1929 ]
1947 ]
1930
1948
1949+ toJSON (StatsAgg (StatisticsAggregation typ field)) =
1950+ object [stType .= omitNulls [ "field" .= field ]]
1951+ where
1952+ stType | typ == Basic = "stats"
1953+ | otherwise = "extended_stats"
1954+
1931instance ToJSON DateRangeAggregation where
1955instance ToJSON DateRangeAggregation where
1932 toJSON DateRangeAggregation {..} =
1956 toJSON DateRangeAggregation {..} =
1933 omitNulls [ "field" .= draField
1957 omitNulls [ "field" .= draField
tests/V5/tests.hs+11
@@ -1144,6 +1144,17 @@ main = hspec $ do
1144 liftIO $
1144 liftIO $
1145 fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
1145 fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
1146
1146
1147+ it "returns stats aggregation results" $ withTestEnv $ do
1148+ _ <- insertData
1149+ let stats = StatsAgg $ mkStatsAggregation $ FieldName "user"
1150+ let search = mkAggregateSearch Nothing $ mkAggregations "users" stats
1151+ let search' = search { Database.V5.Bloodhound.from = From 0, size = Size 0 }
1152+ searchExpectAggs search'
1153+ let docCountPair k n = (k, object ["value" .= Number n])
1154+ res <- searchTweets search'
1155+ liftIO $
1156+ fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
1157+
1147 it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
1158 it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
1148 _ <- insertData
1159 _ <- insertData
1149 let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }
1160 let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }