git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #100 from MHova/99-missing-aggregation
d82fabeChris Allen
src/Database/Bloodhound/Types.hs+8−1
@@ -237,6 +237,7 @@ module Database.Bloodhound.Types
237 , Bucket(..)
237 , Bucket(..)
238 , BucketAggregation(..)
238 , BucketAggregation(..)
239 , TermsAggregation(..)
239 , TermsAggregation(..)
240+ , MissingAggregation(..)
240 , ValueCountAggregation(..)
241 , ValueCountAggregation(..)
241 , FilterAggregation(..)
242 , FilterAggregation(..)
242 , DateHistogramAggregation(..)
243 , DateHistogramAggregation(..)
@@ -1544,8 +1545,12 @@ data Aggregation = TermsAgg TermsAggregation
1544 | DateHistogramAgg DateHistogramAggregation
1545 | DateHistogramAgg DateHistogramAggregation
1545 | ValueCountAgg ValueCountAggregation
1546 | ValueCountAgg ValueCountAggregation
1546 | FilterAgg FilterAggregation
1547 | FilterAgg FilterAggregation
1547− | DateRangeAgg DateRangeAggregation deriving (Eq, Show, Generic, Typeable)
1548+ | DateRangeAgg DateRangeAggregation
1549+ | MissingAgg MissingAggregation deriving (Eq, Show, Generic, Typeable)
1548
1550
1551+data MissingAggregation = MissingAggregation
1552+ { maField :: Text
1553+ } deriving (Eq, Show, Generic, Typeable)
1549
1554
1550data TermsAggregation = TermsAggregation { term :: Either Text Text
1555data TermsAggregation = TermsAggregation { term :: Either Text Text
1551 , termInclude :: Maybe TermInclusion
1556 , termInclude :: Maybe TermInclusion
@@ -1705,6 +1710,8 @@ instance ToJSON Aggregation where
1705 , "aggs" .= ags]
1710 , "aggs" .= ags]
1706 toJSON (DateRangeAgg a) = object [ "date_range" .= a
1711 toJSON (DateRangeAgg a) = object [ "date_range" .= a
1707 ]
1712 ]
1713+ toJSON (MissingAgg (MissingAggregation{..})) =
1714+ object ["missing" .= object ["field" .= maField]]
1708
1715
1709instance ToJSON DateRangeAggregation where
1716instance ToJSON DateRangeAggregation where
1710 toJSON DateRangeAggregation {..} =
1717 toJSON DateRangeAggregation {..} =
tests/tests.hs+34−4
@@ -130,7 +130,8 @@ data Tweet = Tweet { user :: Text
130 , postDate :: UTCTime
130 , postDate :: UTCTime
131 , message :: Text
131 , message :: Text
132 , age :: Int
132 , age :: Int
133− , location :: Location }
133+ , location :: Location
134+ , extra :: Maybe Text }
134 deriving (Eq, Generic, Show)
135 deriving (Eq, Generic, Show)
135
136
136instance ToJSON Tweet where
137instance ToJSON Tweet where
@@ -178,7 +179,18 @@ exampleTweet = Tweet { user = "bitemyapp"
178 (secondsToDiffTime 10)
179 (secondsToDiffTime 10)
179 , message = "Use haskell!"
180 , message = "Use haskell!"
180 , age = 10000
181 , 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" }
182
194
183newAge :: Int
195newAge :: Int
184newAge = 31337
196newAge = 31337
@@ -202,7 +214,8 @@ otherTweet = Tweet { user = "notmyapp"
202 (secondsToDiffTime 11)
214 (secondsToDiffTime 11)
203 , message = "Use haskell!"
215 , message = "Use haskell!"
204 , age = 1000
216 , age = 1000
205− , location = Location 40.12 (-71.34) }
217+ , location = Location 40.12 (-71.34)
218+ , extra = Nothing }
206
219
207resetIndex :: BH IO ()
220resetIndex :: BH IO ()
208resetIndex = do
221resetIndex = do
@@ -234,6 +247,12 @@ insertOther = do
234 _ <- refreshIndex testIndex
247 _ <- refreshIndex testIndex
235 return ()
248 return ()
236
249
250+insertExtra :: BH IO ()
251+insertExtra = do
252+ _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings tweetWithExtra (DocId "4")
253+ _ <- refreshIndex testIndex
254+ return ()
255+
237insertWithSpaceInId :: BH IO ()
256insertWithSpaceInId :: BH IO ()
238insertWithSpaceInId = do
257insertWithSpaceInId = do
239 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings exampleTweet (DocId "Hello World")
258 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings exampleTweet (DocId "Hello World")
@@ -1236,6 +1255,16 @@ main = hspec $ do
1236 forM_ intervals expect
1255 forM_ intervals expect
1237 forM_ intervals valid
1256 forM_ intervals valid
1238
1257
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+
1239 describe "Highlights API" $ do
1268 describe "Highlights API" $ do
1240
1269
1241 it "returns highlight from query when there should be one" $ withTestEnv $ do
1270 it "returns highlight from query when there should be one" $ withTestEnv $ do
@@ -1284,7 +1313,8 @@ main = hspec $ do
1284
1313
1285 it "excludes source patterns" $ withTestEnv $ do
1314 it "excludes source patterns" $ withTestEnv $ do
1286 searchExpectSource
1315 searchExpectSource
1316+ (SourceIncludeExclude (Include [])
1287− (SourceIncludeExclude (Include []) (Exclude [Pattern "l*", Pattern "*ge", Pattern "postDate"]))
1317+ (Exclude [Pattern "l*", Pattern "*ge", Pattern "postDate", Pattern "extra"]))
1288 (Right (Object (HM.fromList [("user",String "bitemyapp")])))
1318 (Right (Object (HM.fromList [("user",String "bitemyapp")])))
1289
1319
1290 describe "ToJSON RegexpFlags" $ do
1320 describe "ToJSON RegexpFlags" $ do