git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #61 from Soostone/filter-aggregation

Add filter aggregation support

commite88613fe93e06cd9522198ff746bb36cd9304228 authorChris Allen <cma@bitemyapp.com> dateWed 29 Jul 2015, 10:52 (11 years ago) parent45a0834b23bd7a
2 files changed+27−4
src/Database/Bloodhound/Types.hs+11−1 tests/tests.hs+16−3
src/Database/Bloodhound/Types.hs+11−1
@@ -203,6 +203,7 @@ module Database.Bloodhound.Types
203203 , BucketAggregation(..)
204204 , TermsAggregation(..)
205205 , ValueCountAggregation(..)
206+ , FilterAggregation(..)
206207 , DateHistogramAggregation(..)
207208
208209 , Highlights(..)
@@ -1319,7 +1320,8 @@ data Interval = Year
13191320
13201321data Aggregation = TermsAgg TermsAggregation
13211322 | DateHistogramAgg DateHistogramAggregation
1322− | ValueCountAgg ValueCountAggregation deriving (Eq, Show)
1323+ | ValueCountAgg ValueCountAggregation
1324+ | FilterAgg FilterAggregation deriving (Eq, Show)
13231325
13241326
13251327data TermsAggregation = TermsAggregation { term :: Either Text Text
@@ -1345,9 +1347,14 @@ data DateHistogramAggregation = DateHistogramAggregation { dateField :: Fie
13451347 , dateAggs :: Maybe Aggregations
13461348 } deriving (Eq, Show)
13471349
1350+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
13481351data ValueCountAggregation = FieldValueCount FieldName
13491352 | ScriptValueCount Script deriving (Eq, Show)
13501353
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+
13511358mkTermsAggregation :: Text -> TermsAggregation
13521359mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
13531360
@@ -1424,6 +1431,9 @@ instance ToJSON Aggregation where
14241431 where v = case a of
14251432 (FieldValueCount (FieldName n)) -> object ["field" .= n]
14261433 (ScriptValueCount (Script s)) -> object ["script" .= s]
1434+ toJSON (FilterAgg (FilterAggregation filt ags)) =
1435+ omitNulls [ "filter" .= filt
1436+ , "aggs" .= ags]
14271437
14281438type AggregationResults = M.Map Text Value
14291439
tests/tests.hs+16−3
@@ -635,11 +635,24 @@ main = hspec $ do
635635 let ags = mkAggregations "user_count" (ValueCountAgg (FieldValueCount (FieldName "user"))) <>
636636 mkAggregations "bogus_count" (ValueCountAgg (FieldValueCount (FieldName "bogus")))
637637 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])
639639 res <- searchTweets search
640640 liftIO $
641− fmap aggregations res `shouldBe` Right (Just (M.fromList [ countPair "user_count" 2
642− , countPair "bogus_count" 0
641+ fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "user_count" 2
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
643656 ]))
644657 -- Interaction of date serialization and date histogram aggregation is broken.
645658 -- it "returns date histogram aggregation results" $ withTestEnv $ do