add stats aggregation

This commit is contained in:
David Smith 2017-03-22 17:10:11 +00:00
parent b3e5c10ac0
commit b547b18f33
2 changed files with 35 additions and 0 deletions

View File

@ -47,6 +47,8 @@ module Database.V5.Bloodhound.Types
, mkDateHistogram
, mkCardinalityAggregation
, mkDocVersion
, mkStatsAggregation
, mkExtendedStatsAggregation
, docVersionNumber
, toMissing
, toTerms
@ -341,6 +343,7 @@ module Database.V5.Bloodhound.Types
, DateMathModifier(..)
, DateMathUnit(..)
, TopHitsAggregation(..)
, StatisticsAggregation(..)
, Highlights(..)
, FieldHighlight(..)
@ -1715,6 +1718,7 @@ data Aggregation = TermsAgg TermsAggregation
| DateRangeAgg DateRangeAggregation
| MissingAgg MissingAggregation
| TopHitsAgg TopHitsAggregation
| StatsAgg StatisticsAggregation
deriving (Eq, Read, Show, Generic, Typeable)
data TopHitsAggregation = TopHitsAggregation
@ -1792,6 +1796,14 @@ data ValueCountAggregation = FieldValueCount FieldName
data FilterAggregation = FilterAggregation { faFilter :: Filter
, faAggs :: Maybe Aggregations} deriving (Eq, Read, Show, Generic, Typeable)
data StatisticsAggregation = StatisticsAggregation { statsType :: StatsType
, statsField :: FieldName } deriving (Eq, Read, Show, Generic, Typeable)
data StatsType
= Basic
| Extended
deriving (Eq, Read, Show, Generic, Typeable)
mkTermsAggregation :: Text -> TermsAggregation
mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
@ -1804,6 +1816,12 @@ mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothi
mkCardinalityAggregation :: FieldName -> CardinalityAggregation
mkCardinalityAggregation t = CardinalityAggregation t Nothing
mkStatsAggregation :: FieldName -> StatisticsAggregation
mkStatsAggregation = StatisticsAggregation Basic
mkExtendedStatsAggregation :: FieldName -> StatisticsAggregation
mkExtendedStatsAggregation = StatisticsAggregation Extended
instance ToJSON Version where
toJSON Version {..} = object ["number" .= number
,"build_hash" .= build_hash
@ -1928,6 +1946,12 @@ instance ToJSON Aggregation where
]
]
toJSON (StatsAgg (StatisticsAggregation typ field)) =
object [stType .= omitNulls [ "field" .= field ]]
where
stType | typ == Basic = "stats"
| otherwise = "extended_stats"
instance ToJSON DateRangeAggregation where
toJSON DateRangeAggregation {..} =
omitNulls [ "field" .= draField

View File

@ -1144,6 +1144,17 @@ main = hspec $ do
liftIO $
fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
it "returns stats aggregation results" $ withTestEnv $ do
_ <- insertData
let stats = StatsAgg $ mkStatsAggregation $ FieldName "user"
let search = mkAggregateSearch Nothing $ mkAggregations "users" stats
let search' = search { Database.V5.Bloodhound.from = From 0, size = Size 0 }
searchExpectAggs search'
let docCountPair k n = (k, object ["value" .= Number n])
res <- searchTweets search'
liftIO $
fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
_ <- insertData
let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }