git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #61 from Soostone/filter-aggregation
e88613fChris Allen
src/Database/Bloodhound/Types.hs+11−1
@@ -203,6 +203,7 @@ module Database.Bloodhound.Types
203 , BucketAggregation(..)
203 , BucketAggregation(..)
204 , TermsAggregation(..)
204 , TermsAggregation(..)
205 , ValueCountAggregation(..)
205 , ValueCountAggregation(..)
206+ , FilterAggregation(..)
206 , DateHistogramAggregation(..)
207 , DateHistogramAggregation(..)
207
208
208 , Highlights(..)
209 , Highlights(..)
@@ -1319,7 +1320,8 @@ data Interval = Year
1319
1320
1320data Aggregation = TermsAgg TermsAggregation
1321data Aggregation = TermsAgg TermsAggregation
1321 | DateHistogramAgg DateHistogramAggregation
1322 | DateHistogramAgg DateHistogramAggregation
1322− | ValueCountAgg ValueCountAggregation deriving (Eq, Show)
1323+ | ValueCountAgg ValueCountAggregation
1324+ | FilterAgg FilterAggregation deriving (Eq, Show)
1323
1325
1324
1326
1325data TermsAggregation = TermsAggregation { term :: Either Text Text
1327data TermsAggregation = TermsAggregation { term :: Either Text Text
@@ -1345,9 +1347,14 @@ data DateHistogramAggregation = DateHistogramAggregation { dateField :: Fie
1345 , dateAggs :: Maybe Aggregations
1347 , dateAggs :: Maybe Aggregations
1346 } deriving (Eq, Show)
1348 } deriving (Eq, Show)
1347
1349
1350+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
1348data ValueCountAggregation = FieldValueCount FieldName
1351data ValueCountAggregation = FieldValueCount FieldName
1349 | ScriptValueCount Script deriving (Eq, Show)
1352 | ScriptValueCount Script deriving (Eq, Show)
1350
1353
1354+-- | Single-bucket filter aggregations. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html#search-aggregations-bucket-filter-aggregation> for more information.
1355+data FilterAggregation = FilterAggregation { faFilter :: Filter
1356+ , faAggs :: Maybe Aggregations} deriving (Eq, Show)
1357+
1351mkTermsAggregation :: Text -> TermsAggregation
1358mkTermsAggregation :: Text -> TermsAggregation
1352mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1359mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1353
1360
@@ -1424,6 +1431,9 @@ instance ToJSON Aggregation where
1424 where v = case a of
1431 where v = case a of
1425 (FieldValueCount (FieldName n)) -> object ["field" .= n]
1432 (FieldValueCount (FieldName n)) -> object ["field" .= n]
1426 (ScriptValueCount (Script s)) -> object ["script" .= s]
1433 (ScriptValueCount (Script s)) -> object ["script" .= s]
1434+ toJSON (FilterAgg (FilterAggregation filt ags)) =
1435+ omitNulls [ "filter" .= filt
1436+ , "aggs" .= ags]
1427
1437
1428type AggregationResults = M.Map Text Value
1438type AggregationResults = M.Map Text Value
1429
1439
tests/tests.hs+16−3
@@ -635,11 +635,24 @@ main = hspec $ do
635 let ags = mkAggregations "user_count" (ValueCountAgg (FieldValueCount (FieldName "user"))) <>
635 let ags = mkAggregations "user_count" (ValueCountAgg (FieldValueCount (FieldName "user"))) <>
636 mkAggregations "bogus_count" (ValueCountAgg (FieldValueCount (FieldName "bogus")))
636 mkAggregations "bogus_count" (ValueCountAgg (FieldValueCount (FieldName "bogus")))
637 let search = mkAggregateSearch Nothing ags
637 let search = mkAggregateSearch Nothing ags
638− let countPair k n = (k, object ["value" .= Number n])
638+ let docCountPair k n = (k, object ["value" .= Number n])
639 res <- searchTweets search
639 res <- searchTweets search
640 liftIO $
640 liftIO $
641− fmap aggregations res `shouldBe` Right (Just (M.fromList [ countPair "user_count" 2
641+ fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "user_count" 2
642− , countPair "bogus_count" 0
642+ , docCountPair "bogus_count" 0
643+ ]))
644+
645+ it "can execute filter aggregations" $ withTestEnv $ do
646+ _ <- insertData
647+ _ <- insertOther
648+ let ags = mkAggregations "bitemyapps" (FilterAgg (FilterAggregation (TermFilter (Term "user" "bitemyapp") defaultCache) Nothing)) <>
649+ mkAggregations "notmyapps" (FilterAgg (FilterAggregation (TermFilter (Term "user" "notmyapp") defaultCache) Nothing))
650+ let search = mkAggregateSearch Nothing ags
651+ let docCountPair k n = (k, object ["doc_count" .= Number n])
652+ res <- searchTweets search
653+ liftIO $
654+ fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "bitemyapps" 1
655+ , docCountPair "notmyapps" 1
643 ]))
656 ]))
644 -- Interaction of date serialization and date histogram aggregation is broken.
657 -- Interaction of date serialization and date histogram aggregation is broken.
645 -- it "returns date histogram aggregation results" $ withTestEnv $ do
658 -- it "returns date histogram aggregation results" $ withTestEnv $ do