git.iko.soy/typeable/bloodhound
on master help sign in
add stats aggregation
commitb547b18f339bee33872d36fd390cd11bd855912b authorDavid Smith <shmish111@gmail.com> dateWed 22 Mar 2017, 17:10 (10 years ago) parentb3e5c10
2 files changed+35
src/Database/V5/Bloodhound/Types.hs+24 tests/V5/tests.hs+11
src/Database/V5/Bloodhound/Types.hs+24
@@ -47,6 +47,8 @@ module Database.V5.Bloodhound.Types
4747 , mkDateHistogram
4848 , mkCardinalityAggregation
4949 , mkDocVersion
50+ , mkStatsAggregation
51+ , mkExtendedStatsAggregation
5052 , docVersionNumber
5153 , toMissing
5254 , toTerms
@@ -341,6 +343,7 @@ module Database.V5.Bloodhound.Types
341343 , DateMathModifier(..)
342344 , DateMathUnit(..)
343345 , TopHitsAggregation(..)
346+ , StatisticsAggregation(..)
344347
345348 , Highlights(..)
346349 , FieldHighlight(..)
@@ -1715,6 +1718,7 @@ data Aggregation = TermsAgg TermsAggregation
17151718 | DateRangeAgg DateRangeAggregation
17161719 | MissingAgg MissingAggregation
17171720 | TopHitsAgg TopHitsAggregation
1721+ | StatsAgg StatisticsAggregation
17181722 deriving (Eq, Read, Show, Generic, Typeable)
17191723
17201724data TopHitsAggregation = TopHitsAggregation
@@ -1792,6 +1796,14 @@ data ValueCountAggregation = FieldValueCount FieldName
17921796data FilterAggregation = FilterAggregation { faFilter :: Filter
17931797 , faAggs :: Maybe Aggregations} deriving (Eq, Read, Show, Generic, Typeable)
17941798
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+
17951807mkTermsAggregation :: Text -> TermsAggregation
17961808mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
17971809
@@ -1804,6 +1816,12 @@ mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothi
18041816mkCardinalityAggregation :: FieldName -> CardinalityAggregation
18051817mkCardinalityAggregation t = CardinalityAggregation t Nothing
18061818
1819+mkStatsAggregation :: FieldName -> StatisticsAggregation
1820+mkStatsAggregation = StatisticsAggregation Basic
1821+
1822+mkExtendedStatsAggregation :: FieldName -> StatisticsAggregation
1823+mkExtendedStatsAggregation = StatisticsAggregation Extended
1824+
18071825instance ToJSON Version where
18081826 toJSON Version {..} = object ["number" .= number
18091827 ,"build_hash" .= build_hash
@@ -1928,6 +1946,12 @@ instance ToJSON Aggregation where
19281946 ]
19291947 ]
19301948
1949+ toJSON (StatsAgg (StatisticsAggregation typ field)) =
1950+ object [stType .= omitNulls [ "field" .= field ]]
1951+ where
1952+ stType | typ == Basic = "stats"
1953+ | otherwise = "extended_stats"
1954+
19311955instance ToJSON DateRangeAggregation where
19321956 toJSON DateRangeAggregation {..} =
19331957 omitNulls [ "field" .= draField
tests/V5/tests.hs+11
@@ -1144,6 +1144,17 @@ main = hspec $ do
11441144 liftIO $
11451145 fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
11461146
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+
11471158 it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
11481159 _ <- insertData
11491160 let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }