git.iko.soy/typeable/bloodhound
on master help sign in
Merge branch 'master' into node-stats-api
commitdf27a379bb25a1cf5a7af9827989532e727405ec authorMichael Xavier <michael@michaelxavier.net> dateWed 14 Sep 2016, 18:18 (10 years ago) parent8672b4f5589154
6 files changed+97−14
Makefile+15−1 README.md+1 src/Database/Bloodhound/Types.hs+19−6 stack-7.10.yaml+1−1 stack-8.0.yaml+16 tests/tests.hs+45−6
Makefile+15−1
@@ -1,10 +1,24 @@
11build:
22 stack build
33
4−test:
4+echo-warn:
55 echo "Make certain you have an elasticsearch instance on localhost:9200 !"
6+
7+test: echo-warn
68 stack test
79
10+7.8-build:
11+ STACK_YAML="stack-7.8.yaml" stack build
12+
13+7.8-test: echo-warn
14+ STACK_YAML="stack-7.8.yaml" stack test
15+
16+8.0-build:
17+ STACK_YAML="stack-8.0.yaml" stack build
18+
19+8.0-test: echo-warn
20+ STACK_YAML="stack-8.0.yaml" stack test
21+
822ghci:
923 stack ghci
1024
README.md+1
@@ -70,6 +70,7 @@ Index Operations
7070-- the multi-line examples into your ghci REPL.
7171
7272:set -XDeriveGeneric
73+:set -XOverloadedStrings
7374:{
7475import Control.Applicative
7576import Database.Bloodhound
src/Database/Bloodhound/Types.hs+19−6
@@ -45,6 +45,7 @@ module Database.Bloodhound.Types
4545 , mkTermsAggregation
4646 , mkTermsScriptAggregation
4747 , mkDateHistogram
48+ , mkCardinalityAggregation
4849 , mkDocVersion
4950 , docVersionNumber
5051 , toMissing
@@ -330,6 +331,7 @@ module Database.Bloodhound.Types
330331 , MissingAggregation(..)
331332 , ValueCountAggregation(..)
332333 , FilterAggregation(..)
334+ , CardinalityAggregation(..)
333335 , DateHistogramAggregation(..)
334336 , DateRangeAggregation(..)
335337 , DateRangeAggRange(..)
@@ -1714,6 +1716,7 @@ data Interval = Year
17141716 | FractionalInterval Float TimeInterval deriving (Eq, Read, Show, Generic, Typeable)
17151717
17161718data Aggregation = TermsAgg TermsAggregation
1719+ | CardinalityAgg CardinalityAggregation
17171720 | DateHistogramAgg DateHistogramAggregation
17181721 | ValueCountAgg ValueCountAggregation
17191722 | FilterAgg FilterAggregation
@@ -1736,6 +1739,10 @@ data TermsAggregation = TermsAggregation { term :: Either Text Text
17361739 , termAggs :: Maybe Aggregations
17371740 } deriving (Eq, Read, Show, Generic, Typeable)
17381741
1742+data CardinalityAggregation = CardinalityAggregation { cardinalityField :: FieldName,
1743+ precisionThreshold :: Maybe Int
1744+ } deriving (Eq, Read, Show, Generic, Typeable)
1745+
17391746data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
17401747 , dateInterval :: Interval
17411748 , dateFormat :: Maybe Text
@@ -1794,6 +1801,9 @@ mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing
17941801mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
17951802mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
17961803
1804+mkCardinalityAggregation :: FieldName -> CardinalityAggregation
1805+mkCardinalityAggregation t = CardinalityAggregation t Nothing
1806+
17971807instance ToJSON Version where
17981808 toJSON Version {..} = object ["number" .= number
17991809 ,"build_hash" .= build_hash
@@ -1884,6 +1894,12 @@ instance ToJSON Aggregation where
18841894 where
18851895 toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
18861896
1897+ toJSON (CardinalityAgg (CardinalityAggregation field precisionThreshold)) =
1898+ object ["cardinality" .= omitNulls [ "field" .= field,
1899+ "precisionThreshold" .= precisionThreshold
1900+ ]
1901+ ]
1902+
18871903 toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
18881904 omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
18891905 "interval" .= interval,
@@ -1921,10 +1937,7 @@ instance ToJSON DateRangeAggRange where
19211937instance ToJSON DateMathExpr where
19221938 toJSON (DateMathExpr a mods) = String (fmtA a <> mconcat (fmtMod <$> mods))
19231939 where fmtA DMNow = "now"
1924− fmtA (DMDate date) = case toGregorian date of
1925− (y,m,d) -> showText y <> "-" <>
1926− showText m <> "-" <>
1927− showText d <> "||"
1940+ fmtA (DMDate date) = (T.pack $ showGregorian date) <> "||"
19281941 fmtMod (AddTime n u) = "+" <> showText n <> fmtU u
19291942 fmtMod (SubtractTime n u) = "-" <> showText n <> fmtU u
19301943 fmtMod (RoundDownTo u) = "/" <> fmtU u
@@ -3010,7 +3023,7 @@ instance FromJSON MatchQueryType where
30103023instance FromJSON Status where
30113024 parseJSON (Object v) = Status <$>
30123025 v .:? "ok" <*>
3013− v .: "status" <*>
3026+ (v .:? "status" .!= 200) <*>
30143027 v .: "name" <*>
30153028 v .: "version" <*>
30163029 v .: "tagline"
@@ -3281,7 +3294,7 @@ instance (FromJSON a) => FromJSON (EsResultFound a) where
32813294instance FromJSON EsError where
32823295 parseJSON (Object v) = EsError <$>
32833296 v .: "status" <*>
3284− v .: "error"
3297+ (v .: "error" <|> (v .: "error" >>= (.: "reason")))
32853298 parseJSON _ = empty
32863299
32873300instance FromJSON IndexAliasesSummary where
stack-7.10.yaml+1−1
@@ -13,4 +13,4 @@ extra-deps:
1313- semigroups-0.18.0.1
1414- uri-bytestring-0.1.9
1515- temporary-resourcet-0.1.0.0
16−resolver: lts-5.1
16+resolver: lts-6.12
stack-8.0.yamlnew file+16
@@ -0,0 +1,16 @@
1+flags: {}
2+packages:
3+- '.'
4+extra-deps:
5+- doctest-prop-0.2.0.1
6+- quickcheck-properties-0.1
7+
8+# - http-client-0.5.0
9+# - fail-4.9.0.0
10+# - http-types-0.9
11+# - attoparsec-0.13.0.1
12+# - doctest-0.10.1
13+# - semigroups-0.18.0.1
14+# - uri-bytestring-0.1.9
15+# - temporary-resourcet-0.1.0.0
16+resolver: nightly-2016-08-20
tests/tests.hs+45−6
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE DefaultSignatures #-}
23{-# LANGUAGE DeriveGeneric #-}
34{-# LANGUAGE FlexibleContexts #-}
@@ -95,6 +96,9 @@ es15 = Vers.Version [1, 5, 0] []
9596es16 :: Vers.Version
9697es16 = Vers.Version [1, 6, 0] []
9798
99+es20 :: Vers.Version
100+es20 = Vers.Version [2, 0, 0] []
101+
98102getServerVersion :: IO (Maybe Vers.Version)
99103getServerVersion = fmap extractVersion <$> withTestEnv getStatus
100104 where
@@ -173,15 +177,32 @@ data ParentMapping = ParentMapping deriving (Eq, Show)
173177
174178instance ToJSON ParentMapping where
175179 toJSON ParentMapping =
176− object ["parent" .= Null ]
180+ object ["parent" .=
181+ object ["properties" .=
182+ object [ "user" .= object ["type" .= ("string" :: Text)]
183+ -- Serializing the date as a date is breaking other tests, mysteriously.
184+ -- , "postDate" .= object [ "type" .= ("date" :: Text)
185+ -- , "format" .= ("YYYY-MM-dd`T`HH:mm:ss.SSSZZ" :: Text)]
186+ , "message" .= object ["type" .= ("string" :: Text)]
187+ , "age" .= object ["type" .= ("integer" :: Text)]
188+ , "location" .= object ["type" .= ("geo_point" :: Text)]
189+ ]]]
177190
178191data ChildMapping = ChildMapping deriving (Eq, Show)
179192
180193instance ToJSON ChildMapping where
181194 toJSON ChildMapping =
182195 object ["child" .=
183− object ["_parent" .= object ["type" .= ("parent" :: Text)]]
184− ]
196+ object ["_parent" .= object ["type" .= ("parent" :: Text)]
197+ , "properties" .=
198+ object [ "user" .= object ["type" .= ("string" :: Text)]
199+ -- Serializing the date as a date is breaking other tests, mysteriously.
200+ -- , "postDate" .= object [ "type" .= ("date" :: Text)
201+ -- , "format" .= ("YYYY-MM-dd`T`HH:mm:ss.SSSZZ" :: Text)]
202+ , "message" .= object ["type" .= ("string" :: Text)]
203+ , "age" .= object ["type" .= ("integer" :: Text)]
204+ , "location" .= object ["type" .= ("geo_point" :: Text)]
205+ ]]]
185206
186207data TweetMapping = TweetMapping deriving (Eq, Show)
187208
@@ -585,8 +606,10 @@ noDuplicates xs = nub xs == xs
585606instance Arbitrary NominalDiffTime where
586607 arbitrary = fromInteger <$> arbitrary
587608
609+#if !MIN_VERSION_QuickCheck(2,8,0)
588610instance (Arbitrary k, Ord k, Arbitrary v) => Arbitrary (M.Map k v) where
589611 arbitrary = M.fromList <$> arbitrary
612+#endif
590613
591614instance Arbitrary Text where
592615 arbitrary = T.pack <$> arbitrary
@@ -882,12 +905,17 @@ main = hspec $ do
882905 validateStatus resp 200
883906 validateStatus deleteResp 200
884907
885− describe "error parsing" $ do
886− it "can parse EsErrors" $ withTestEnv $ do
908+ describe "error parsing" $ do
909+ it "can parse EsErrors for < 2.0" $ when' (atmost es16) $ withTestEnv $ do
887910 res <- getDocument (IndexName "bogus") (MappingName "also_bogus") (DocId "bogus_as_well")
888911 let errorResp = eitherDecode (responseBody res)
889912 liftIO (errorResp `shouldBe` Right (EsError 404 "IndexMissingException[[bogus] missing]"))
890913
914+ it "can parse EsErrors for >= 2.0" $ when' (atleast es20) $ withTestEnv $ do
915+ res <- getDocument (IndexName "bogus") (MappingName "also_bogus") (DocId "bogus_as_well")
916+ let errorResp = eitherDecode (responseBody res)
917+ liftIO (errorResp `shouldBe` Right (EsError 404 "no such index"))
918+
891919 describe "document API" $ do
892920 it "indexes, updates, gets, and then deletes the generated document" $ withTestEnv $ do
893921 _ <- insertData
@@ -921,8 +949,8 @@ main = hspec $ do
921949
922950 it "indexes two documents in a parent/child relationship and checks that the child exists" $ withTestEnv $ do
923951 resetIndex
924− _ <- putMapping testIndex (MappingName "parent") ParentMapping
925952 _ <- putMapping testIndex (MappingName "child") ChildMapping
953+ _ <- putMapping testIndex (MappingName "parent") ParentMapping
926954 _ <- indexDocument testIndex (MappingName "parent") defaultIndexDocumentSettings exampleTweet (DocId "1")
927955 let parent = (Just . DocumentParent . DocId) "1"
928956 ids = IndexDocumentSettings NoVersionControl parent
@@ -1241,6 +1269,17 @@ main = hspec $ do
12411269 searchExpectAggs search
12421270 searchValidBucketAgg search "users" toTerms
12431271
1272+ it "returns cardinality aggregation results" $ withTestEnv $ do
1273+ _ <- insertData
1274+ let cardinality = CardinalityAgg $ mkCardinalityAggregation $ FieldName "user"
1275+ let search = mkAggregateSearch Nothing $ mkAggregations "users" cardinality
1276+ let search' = search { Database.Bloodhound.from = From 0, size = Size 0 }
1277+ searchExpectAggs search'
1278+ let docCountPair k n = (k, object ["value" .= Number n])
1279+ res <- searchTweets search'
1280+ liftIO $
1281+ fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
1282+
12441283 it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
12451284 _ <- insertData
12461285 let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }