Merge pull request #61 from Soostone/filter-aggregation

Add filter aggregation support
This commit is contained in:
Chris Allen 2015-07-29 10:52:04 -05:00
commit e88613fe93
2 changed files with 27 additions and 4 deletions

View File

@ -203,6 +203,7 @@ module Database.Bloodhound.Types
, BucketAggregation(..)
, TermsAggregation(..)
, ValueCountAggregation(..)
, FilterAggregation(..)
, DateHistogramAggregation(..)
, Highlights(..)
@ -1319,7 +1320,8 @@ data Interval = Year
data Aggregation = TermsAgg TermsAggregation
| DateHistogramAgg DateHistogramAggregation
| ValueCountAgg ValueCountAggregation deriving (Eq, Show)
| ValueCountAgg ValueCountAggregation
| FilterAgg FilterAggregation deriving (Eq, Show)
data TermsAggregation = TermsAggregation { term :: Either Text Text
@ -1345,9 +1347,14 @@ data DateHistogramAggregation = DateHistogramAggregation { dateField :: Fie
, dateAggs :: Maybe Aggregations
} deriving (Eq, Show)
-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
data ValueCountAggregation = FieldValueCount FieldName
| ScriptValueCount Script deriving (Eq, Show)
-- | 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.
data FilterAggregation = FilterAggregation { faFilter :: Filter
, faAggs :: Maybe Aggregations} deriving (Eq, Show)
mkTermsAggregation :: Text -> TermsAggregation
mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
@ -1424,6 +1431,9 @@ instance ToJSON Aggregation where
where v = case a of
(FieldValueCount (FieldName n)) -> object ["field" .= n]
(ScriptValueCount (Script s)) -> object ["script" .= s]
toJSON (FilterAgg (FilterAggregation filt ags)) =
omitNulls [ "filter" .= filt
, "aggs" .= ags]
type AggregationResults = M.Map Text Value

View File

@ -635,11 +635,24 @@ main = hspec $ do
let ags = mkAggregations "user_count" (ValueCountAgg (FieldValueCount (FieldName "user"))) <>
mkAggregations "bogus_count" (ValueCountAgg (FieldValueCount (FieldName "bogus")))
let search = mkAggregateSearch Nothing ags
let countPair k n = (k, object ["value" .= Number n])
let docCountPair k n = (k, object ["value" .= Number n])
res <- searchTweets search
liftIO $
fmap aggregations res `shouldBe` Right (Just (M.fromList [ countPair "user_count" 2
, countPair "bogus_count" 0
fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "user_count" 2
, docCountPair "bogus_count" 0
]))
it "can execute filter aggregations" $ withTestEnv $ do
_ <- insertData
_ <- insertOther
let ags = mkAggregations "bitemyapps" (FilterAgg (FilterAggregation (TermFilter (Term "user" "bitemyapp") defaultCache) Nothing)) <>
mkAggregations "notmyapps" (FilterAgg (FilterAggregation (TermFilter (Term "user" "notmyapp") defaultCache) Nothing))
let search = mkAggregateSearch Nothing ags
let docCountPair k n = (k, object ["doc_count" .= Number n])
res <- searchTweets search
liftIO $
fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "bitemyapps" 1
, docCountPair "notmyapps" 1
]))
-- Interaction of date serialization and date histogram aggregation is broken.
-- it "returns date histogram aggregation results" $ withTestEnv $ do