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.
commit2af098072e303da0fd0c9aa2a3a22da3f5b3e4d4 authorChristopher Guiney <chris@guiney.net> dateTue 7 Oct 2014, 06:41 (12 years ago) parenta2066dd
3 files changed+27−11
Database/Bloodhound/Client.hs+4 Database/Bloodhound/Types.hs+8−7 tests/tests.hs+15−4
Database/Bloodhound/Client.hs+4
@@ -15,6 +15,7 @@ module Database.Bloodhound.Client
1515 , searchByType
1616 , refreshIndex
1717 , mkSearch
18+ , mkAggregateSearch
1819 , bulk
1920 , pageSearch
2021 , mkShardCount
@@ -246,5 +247,8 @@ searchByType (Server server) (IndexName indexName)
246247mkSearch :: Maybe Query -> Maybe Filter -> Search
247248mkSearch query filter = Search query filter Nothing Nothing False 0 10
248249
250+mkAggregateSearch :: Maybe Query -> Aggregations -> Search
251+mkAggregateSearch query aggregations = Search query Nothing Nothing (Just aggregations) False 0 0
252+
249253pageSearch :: Int -> Int -> Search -> Search
250254pageSearch pageFrom pageSize search = search { from = pageFrom, size = pageSize }
Database/Bloodhound/Types.hs+8−7
@@ -22,6 +22,7 @@ module Database.Bloodhound.Types
2222 , mkBoolQuery
2323 , mkRangeQuery
2424 , mkQueryStringQuery
25+ , mkAggregations
2526 , mkTermsAggregation
2627 , mkDateHistogram
2728 , toTerms
@@ -962,6 +963,12 @@ showText = T.pack . show
962963
963964type Aggregations = M.Map Text Aggregation
964965
966+emptyAggregations :: Aggregations
967+emptyAggregations = M.empty
968+
969+mkAggregations :: Text -> Aggregation -> Aggregations
970+mkAggregations name aggregation = M.insert name aggregation emptyAggregations
971+
965972data TermOrder = TermOrder{ termSortField :: Text
966973 , termSortOrder :: SortOrder } deriving (Eq, Show)
967974
@@ -976,10 +983,7 @@ data ExecutionHint = GlobalOrdinals
976983 | GlobalOrdinalsLowCardinality
977984 | Map deriving(Eq, Show)
978985
979−data TimeInterval = Years
980− | Quarters
981− | Months
982− | Weeks
986+data TimeInterval = Weeks
983987 | Days
984988 | Hours
985989 | Minutes
@@ -1056,9 +1060,6 @@ instance ToJSON Interval where
10561060 toJSON (FractionalInterval fraction interval) = toJSON $ (show fraction) ++ (show interval)
10571061
10581062instance Show TimeInterval where
1059− show Years = "y"
1060− show Quarters = "q"
1061− show Months = "M"
10621063 show Weeks = "w"
10631064 show Days = "d"
10641065 show Hours = "h"
tests/tests.hs+15−4
@@ -4,6 +4,7 @@
44module Main where
55
66import Control.Applicative
7+import Control.Monad
78import Database.Bloodhound
89import Data.Aeson
910import Data.Aeson.Types (parseMaybe)
@@ -22,6 +23,8 @@ import Test.Hspec.QuickCheck (prop)
2223import Test.QuickCheck
2324import qualified Data.Map.Strict as M
2425
26+import System.IO
27+
2528testServer :: Server
2629testServer = Server "http://localhost:9200"
2730testIndex :: IndexName
@@ -382,18 +385,26 @@ main = hspec $ do
382385 describe "Aggregation API" $ do
383386 it "returns term aggregation results" $ do
384387 _ <- insertData
385− let aggs = M.insert "users" (mkTermsAggregation "user") M.empty
386− let search = (mkSearch Nothing Nothing) {aggBody = Just(aggs)}
388+ let search = mkAggregateSearch Nothing $ mkAggregations "users" $ mkTermsAggregation "user"
387389 searchExpectAggs search
388390 searchValidBucketAgg search "users" toTerms
389391
390392 it "returns date histogram aggregation results" $ do
391393 _ <- insertData
392− let aggs = M.insert "byDate" (mkDateHistogram (FieldName "postDate") Minute) M.empty
393− let search = (mkSearch Nothing Nothing) {aggBody = Just(aggs)}
394+ let histogram = mkDateHistogram (FieldName "postDate") Minute
395+ let search = mkAggregateSearch Nothing (mkAggregations "byDate" histogram)
394396 searchExpectAggs search
395397 searchValidBucketAgg search "byDate" toDateHistogram
396398
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
397408
398409 describe "ToJSON RegexpFlags" $ do
399410 it "generates the correct JSON for AllRegexpFlags" $