git.iko.soy/typeable/bloodhound
on master help sign in
remote years,quarters,months; created a couple new functions to make creating aggregations easier. new date histogram test.
2af0980Christopher Guiney
Database/Bloodhound/Client.hs+4
@@ -15,6 +15,7 @@ module Database.Bloodhound.Client
15 , searchByType
15 , searchByType
16 , refreshIndex
16 , refreshIndex
17 , mkSearch
17 , mkSearch
18+ , mkAggregateSearch
18 , bulk
19 , bulk
19 , pageSearch
20 , pageSearch
20 , mkShardCount
21 , mkShardCount
@@ -246,5 +247,8 @@ searchByType (Server server) (IndexName indexName)
246mkSearch :: Maybe Query -> Maybe Filter -> Search
247mkSearch :: Maybe Query -> Maybe Filter -> Search
247mkSearch query filter = Search query filter Nothing Nothing False 0 10
248mkSearch query filter = Search query filter Nothing Nothing False 0 10
248
249
250+mkAggregateSearch :: Maybe Query -> Aggregations -> Search
251+mkAggregateSearch query aggregations = Search query Nothing Nothing (Just aggregations) False 0 0
252+
249pageSearch :: Int -> Int -> Search -> Search
253pageSearch :: Int -> Int -> Search -> Search
250pageSearch pageFrom pageSize search = search { from = pageFrom, size = pageSize }
254pageSearch pageFrom pageSize search = search { from = pageFrom, size = pageSize }
Database/Bloodhound/Types.hs+8−7
@@ -22,6 +22,7 @@ module Database.Bloodhound.Types
22 , mkBoolQuery
22 , mkBoolQuery
23 , mkRangeQuery
23 , mkRangeQuery
24 , mkQueryStringQuery
24 , mkQueryStringQuery
25+ , mkAggregations
25 , mkTermsAggregation
26 , mkTermsAggregation
26 , mkDateHistogram
27 , mkDateHistogram
27 , toTerms
28 , toTerms
@@ -962,6 +963,12 @@ showText = T.pack . show
962
963
963type Aggregations = M.Map Text Aggregation
964type Aggregations = M.Map Text Aggregation
964
965
966+emptyAggregations :: Aggregations
967+emptyAggregations = M.empty
968+
969+mkAggregations :: Text -> Aggregation -> Aggregations
970+mkAggregations name aggregation = M.insert name aggregation emptyAggregations
971+
965data TermOrder = TermOrder{ termSortField :: Text
972data TermOrder = TermOrder{ termSortField :: Text
966 , termSortOrder :: SortOrder } deriving (Eq, Show)
973 , termSortOrder :: SortOrder } deriving (Eq, Show)
967
974
@@ -976,10 +983,7 @@ data ExecutionHint = GlobalOrdinals
976 | GlobalOrdinalsLowCardinality
983 | GlobalOrdinalsLowCardinality
977 | Map deriving(Eq, Show)
984 | Map deriving(Eq, Show)
978
985
979−data TimeInterval = Years
986+data TimeInterval = Weeks
980− | Quarters
981− | Months
982− | Weeks
983 | Days
987 | Days
984 | Hours
988 | Hours
985 | Minutes
989 | Minutes
@@ -1056,9 +1060,6 @@ instance ToJSON Interval where
1056 toJSON (FractionalInterval fraction interval) = toJSON $ (show fraction) ++ (show interval)
1060 toJSON (FractionalInterval fraction interval) = toJSON $ (show fraction) ++ (show interval)
1057
1061
1058instance Show TimeInterval where
1062instance Show TimeInterval where
1059− show Years = "y"
1060− show Quarters = "q"
1061− show Months = "M"
1062 show Weeks = "w"
1063 show Weeks = "w"
1063 show Days = "d"
1064 show Days = "d"
1064 show Hours = "h"
1065 show Hours = "h"
tests/tests.hs+15−4
@@ -4,6 +4,7 @@
4module Main where
4module Main where
5
5
6import Control.Applicative
6import Control.Applicative
7+import Control.Monad
7import Database.Bloodhound
8import Database.Bloodhound
8import Data.Aeson
9import Data.Aeson
9import Data.Aeson.Types (parseMaybe)
10import Data.Aeson.Types (parseMaybe)
@@ -22,6 +23,8 @@ import Test.Hspec.QuickCheck (prop)
22import Test.QuickCheck
23import Test.QuickCheck
23import qualified Data.Map.Strict as M
24import qualified Data.Map.Strict as M
24
25
26+import System.IO
27+
25testServer :: Server
28testServer :: Server
26testServer = Server "http://localhost:9200"
29testServer = Server "http://localhost:9200"
27testIndex :: IndexName
30testIndex :: IndexName
@@ -382,18 +385,26 @@ main = hspec $ do
382 describe "Aggregation API" $ do
385 describe "Aggregation API" $ do
383 it "returns term aggregation results" $ do
386 it "returns term aggregation results" $ do
384 _ <- insertData
387 _ <- insertData
385− let aggs = M.insert "users" (mkTermsAggregation "user") M.empty
388+ let search = mkAggregateSearch Nothing $ mkAggregations "users" $ mkTermsAggregation "user"
386− let search = (mkSearch Nothing Nothing) {aggBody = Just(aggs)}
387 searchExpectAggs search
389 searchExpectAggs search
388 searchValidBucketAgg search "users" toTerms
390 searchValidBucketAgg search "users" toTerms
389
391
390 it "returns date histogram aggregation results" $ do
392 it "returns date histogram aggregation results" $ do
391 _ <- insertData
393 _ <- insertData
392− let aggs = M.insert "byDate" (mkDateHistogram (FieldName "postDate") Minute) M.empty
394+ let histogram = mkDateHistogram (FieldName "postDate") Minute
393− let search = (mkSearch Nothing Nothing) {aggBody = Just(aggs)}
395+ let search = mkAggregateSearch Nothing (mkAggregations "byDate" histogram)
394 searchExpectAggs search
396 searchExpectAggs search
395 searchValidBucketAgg search "byDate" toDateHistogram
397 searchValidBucketAgg search "byDate" toDateHistogram
396
398
399+ it "returns date histogram using fractional date" $ do
400+ _ <- insertData
401+ let intervals = [Weeks, Days, Hours, Minutes, Seconds]
402+ let histogram interval = mkDateHistogram (FieldName "postDate") (FractionalInterval 1.5 interval)
403+ let search interval = mkAggregateSearch Nothing $ mkAggregations "byDate" $ histogram interval
404+ let expect interval = searchExpectAggs (search interval)
405+ let valid interval = searchValidBucketAgg (search interval) "byDate" toDateHistogram
406+ forM_ intervals expect
407+ forM_ intervals valid
397
408
398 describe "ToJSON RegexpFlags" $ do
409 describe "ToJSON RegexpFlags" $ do
399 it "generates the correct JSON for AllRegexpFlags" $
410 it "generates the correct JSON for AllRegexpFlags" $