Merge pull request #177 from shmish111/master

add stats aggregation
This commit is contained in:
Chris Allen 2017-03-23 16:04:10 -05:00 committed by GitHub
commit bab35650c3
2 changed files with 39 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,21 @@ 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 "age"
let search = mkAggregateSearch Nothing $ mkAggregations "users" stats
let search' = search { Database.V5.Bloodhound.from = From 0, size = Size 0 }
searchExpectAggs search'
let statsAggRes k n = (k, object [ "max" .= Number n
, "avg" .= Number n
, "count" .= Number 1
, "min" .= Number n
, "sum" .= Number n])
res <- searchTweets search'
liftIO $
fmap aggregations res `shouldBe` Right (Just (M.fromList [ statsAggRes "users" 10000]))
it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
_ <- insertData
let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }