git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #100 from MHova/99-missing-aggregation

Add Missing Aggregation support

commitd82fabeea19f24753fec3c3773098297aff1584c authorChris Allen <cma@bitemyapp.com> dateTue 2 Feb 2016, 23:22 (11 years ago) parent2096c3b9d07301
2 files changed+42−5
src/Database/Bloodhound/Types.hs+8−1 tests/tests.hs+34−4
src/Database/Bloodhound/Types.hs+8−1
@@ -237,6 +237,7 @@ module Database.Bloodhound.Types
237237 , Bucket(..)
238238 , BucketAggregation(..)
239239 , TermsAggregation(..)
240+ , MissingAggregation(..)
240241 , ValueCountAggregation(..)
241242 , FilterAggregation(..)
242243 , DateHistogramAggregation(..)
@@ -1544,8 +1545,12 @@ data Aggregation = TermsAgg TermsAggregation
15441545 | DateHistogramAgg DateHistogramAggregation
15451546 | ValueCountAgg ValueCountAggregation
15461547 | FilterAgg FilterAggregation
1547− | DateRangeAgg DateRangeAggregation deriving (Eq, Show, Generic, Typeable)
1548+ | DateRangeAgg DateRangeAggregation
1549+ | MissingAgg MissingAggregation deriving (Eq, Show, Generic, Typeable)
15481550
1551+data MissingAggregation = MissingAggregation
1552+ { maField :: Text
1553+ } deriving (Eq, Show, Generic, Typeable)
15491554
15501555data TermsAggregation = TermsAggregation { term :: Either Text Text
15511556 , termInclude :: Maybe TermInclusion
@@ -1705,6 +1710,8 @@ instance ToJSON Aggregation where
17051710 , "aggs" .= ags]
17061711 toJSON (DateRangeAgg a) = object [ "date_range" .= a
17071712 ]
1713+ toJSON (MissingAgg (MissingAggregation{..})) =
1714+ object ["missing" .= object ["field" .= maField]]
17081715
17091716instance ToJSON DateRangeAggregation where
17101717 toJSON DateRangeAggregation {..} =
tests/tests.hs+34−4
@@ -130,7 +130,8 @@ data Tweet = Tweet { user :: Text
130130 , postDate :: UTCTime
131131 , message :: Text
132132 , age :: Int
133− , location :: Location }
133+ , location :: Location
134+ , extra :: Maybe Text }
134135 deriving (Eq, Generic, Show)
135136
136137instance ToJSON Tweet where
@@ -178,7 +179,18 @@ exampleTweet = Tweet { user = "bitemyapp"
178179 (secondsToDiffTime 10)
179180 , message = "Use haskell!"
180181 , age = 10000
181− , location = Location 40.12 (-71.34) }
182+ , location = Location 40.12 (-71.34)
183+ , extra = Nothing }
184+
185+tweetWithExtra :: Tweet
186+tweetWithExtra = Tweet { user = "bitemyapp"
187+ , postDate = UTCTime
188+ (ModifiedJulianDay 55000)
189+ (secondsToDiffTime 10)
190+ , message = "Use haskell!"
191+ , age = 10000
192+ , location = Location 40.12 (-71.34)
193+ , extra = Just "blah blah" }
182194
183195newAge :: Int
184196newAge = 31337
@@ -202,7 +214,8 @@ otherTweet = Tweet { user = "notmyapp"
202214 (secondsToDiffTime 11)
203215 , message = "Use haskell!"
204216 , age = 1000
205− , location = Location 40.12 (-71.34) }
217+ , location = Location 40.12 (-71.34)
218+ , extra = Nothing }
206219
207220resetIndex :: BH IO ()
208221resetIndex = do
@@ -234,6 +247,12 @@ insertOther = do
234247 _ <- refreshIndex testIndex
235248 return ()
236249
250+insertExtra :: BH IO ()
251+insertExtra = do
252+ _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings tweetWithExtra (DocId "4")
253+ _ <- refreshIndex testIndex
254+ return ()
255+
237256insertWithSpaceInId :: BH IO ()
238257insertWithSpaceInId = do
239258 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings exampleTweet (DocId "Hello World")
@@ -1236,6 +1255,16 @@ main = hspec $ do
12361255 forM_ intervals expect
12371256 forM_ intervals valid
12381257
1258+ it "can execute missing aggregations" $ withTestEnv $ do
1259+ _ <- insertData
1260+ _ <- insertExtra
1261+ let ags = mkAggregations "missing_agg" (MissingAgg (MissingAggregation "extra"))
1262+ let search = mkAggregateSearch Nothing ags
1263+ let docCountPair k n = (k, object ["doc_count" .= Number n])
1264+ res <- searchTweets search
1265+ liftIO $
1266+ fmap aggregations res `shouldBe` Right (Just (M.fromList [docCountPair "missing_agg" 1]))
1267+
12391268 describe "Highlights API" $ do
12401269
12411270 it "returns highlight from query when there should be one" $ withTestEnv $ do
@@ -1284,7 +1313,8 @@ main = hspec $ do
12841313
12851314 it "excludes source patterns" $ withTestEnv $ do
12861315 searchExpectSource
1287− (SourceIncludeExclude (Include []) (Exclude [Pattern "l*", Pattern "*ge", Pattern "postDate"]))
1316+ (SourceIncludeExclude (Include [])
1317+ (Exclude [Pattern "l*", Pattern "*ge", Pattern "postDate", Pattern "extra"]))
12881318 (Right (Object (HM.fromList [("user",String "bitemyapp")])))
12891319
12901320 describe "ToJSON RegexpFlags" $ do