git.iko.soy/typeable/bloodhound
on 1e14794 help sign in
1789 lines 83 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DefaultSignatures #-}
3{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE DeriveGeneric #-}
5{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE RecordWildCards #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE TypeOperators #-}
11{-# OPTIONS_GHC -fno-warn-orphans #-}
12#if __GLASGOW_HASKELL__ < 800
13{-# OPTIONS_GHC -fcontext-stack=100 #-}
14#endif
15#if __GLASGOW_HASKELL__ >= 802
16{-# LANGUAGE MonoLocalBinds #-}
17#endif
18module Main where
20import Control.Applicative
21import Control.Error
22import Control.Exception (evaluate)
23import Control.Monad
24import Control.Monad.Catch
25import Control.Monad.Reader
26import Data.Aeson
27import Data.Aeson.Types (parseEither)
28import qualified Data.ByteString.Lazy.Char8 as BL8
29import qualified Data.HashMap.Strict as HM
30import Data.List (nub)
31import qualified Data.List as L
32import Data.List.NonEmpty (NonEmpty (..))
33import qualified Data.List.NonEmpty as NE
34import qualified Data.Map.Strict as M
35import Data.Monoid
36import Data.Ord (comparing)
37import Data.Proxy
38import Data.Text (Text)
39import qualified Data.Text as T
40import Data.Time.Calendar (Day (..), fromGregorian)
41import Data.Time.Clock (NominalDiffTime, UTCTime (..),
42 secondsToDiffTime)
43import Data.Typeable
44import qualified Data.Vector as V
45import qualified Data.Version as Vers
46import Database.V5.Bloodhound
47import GHC.Generics as G
48import Network.HTTP.Client hiding (Proxy)
49import qualified Network.HTTP.Types.Method as NHTM
50import qualified Network.HTTP.Types.Status as NHTS
51import qualified Network.URI as URI
52import Prelude hiding (filter)
53import System.IO.Temp
54import System.PosixCompat.Files
55import Test.Hspec
56import Test.QuickCheck.Property.Monoid (T (..), eq, prop_Monoid)
58import Test.Hspec.QuickCheck (prop)
59import Test.QuickCheck
61import qualified Generics.SOP as SOP
62import qualified Generics.SOP.GGP as SOP
64testServer :: Server
65testServer = Server "http://localhost:9200"
66testIndex :: IndexName
67testIndex = IndexName "bloodhound-tests-twitter-1"
68testMapping :: MappingName
69testMapping = MappingName "tweet"
71withTestEnv :: BH IO a -> IO a
72withTestEnv = withBH defaultManagerSettings testServer
74validateStatus :: Show body => Response body -> Int -> Expectation
75validateStatus resp expected =
76 if actual == expected
77 then return ()
78 else expectationFailure ("Expected " <> show expected <> " but got " <> show actual <> ": " <> show body)
79 where
80 actual = NHTS.statusCode (responseStatus resp)
81 body = responseBody resp
83createExampleIndex :: (MonadBH m) => m Reply
84createExampleIndex = createIndex (IndexSettings (ShardCount 1) (ReplicaCount 0)) testIndex
85deleteExampleIndex :: (MonadBH m) => m Reply
86deleteExampleIndex = deleteIndex testIndex
88es13 :: Vers.Version
89es13 = Vers.Version [1, 3, 0] []
91es12 :: Vers.Version
92es12 = Vers.Version [1, 2, 0] []
94es11 :: Vers.Version
95es11 = Vers.Version [1, 1, 0] []
97es14 :: Vers.Version
98es14 = Vers.Version [1, 4, 0] []
100es15 :: Vers.Version
101es15 = Vers.Version [1, 5, 0] []
103es16 :: Vers.Version
104es16 = Vers.Version [1, 6, 0] []
106es20 :: Vers.Version
107es20 = Vers.Version [2, 0, 0] []
109es50 :: Vers.Version
110es50 = Vers.Version [5, 0, 0] []
112getServerVersion :: IO (Maybe Vers.Version)
113getServerVersion = fmap extractVersion <$> withTestEnv getStatus
114 where
115 extractVersion = versionNumber . number . version
117-- | Get configured repo paths for snapshotting. Note that by default
118-- this is not enabled and if we are over es 1.5, we won't be able to
119-- test snapshotting. Note that this can and should be part of the
120-- client functionality in a much less ad-hoc incarnation.
121getRepoPaths :: IO [FilePath]
122getRepoPaths = withTestEnv $ do
123 bhe <- getBHEnv
124 let Server s = bhServer bhe
125 let tUrl = s <> "/" <> "_nodes"
126 initReq <- parseRequest (URI.escapeURIString URI.isAllowedInURI (T.unpack tUrl))
127 let req = setRequestIgnoreStatus $ initReq { method = NHTM.methodGet }
128 Right (Object o) <- parseEsResponse =<< liftIO (httpLbs req (bhManager bhe))
129 return $ fromMaybe mempty $ do
130 Object nodes <- HM.lookup "nodes" o
131 Object firstNode <- snd <$> headMay (HM.toList nodes)
132 Object settings <- HM.lookup "settings" firstNode
133 Object path <- HM.lookup "path" settings
134 Array repo <- HM.lookup "repo" path
135 return [ T.unpack t | String t <- V.toList repo]
137-- | 1.5 and earlier don't care about repo paths
138canSnapshot :: IO Bool
139canSnapshot = do
140 caresAboutRepos <- atleast es16
141 repoPaths <- getRepoPaths
142 return (not caresAboutRepos || not (null (repoPaths)))
144atleast :: Vers.Version -> IO Bool
145atleast v = getServerVersion >>= \x -> return $ x >= Just v
147atmost :: Vers.Version -> IO Bool
148atmost v = getServerVersion >>= \x -> return $ x <= Just v
150is :: Vers.Version -> IO Bool
151is v = getServerVersion >>= \x -> return $ x == Just v
153when' :: Monad m => m Bool -> m () -> m ()
154when' b f = b >>= \x -> when x f
156(==~) :: (ApproxEq a) => a -> a -> Property
157a ==~ b = counterexample (showApproxEq a ++ " !=~ " ++ showApproxEq b) (a =~ b)
159propJSON :: forall a. (Arbitrary a, ToJSON a, FromJSON a, Show a, ApproxEq a, Typeable a) => Proxy a -> Spec
160propJSON _ = prop testName $ \(a :: a) ->
161 let jsonStr = "via " <> BL8.unpack (encode a)
162 in counterexample jsonStr (parseEither parseJSON (toJSON a) ==~ Right a)
163 where testName = show ty <> " FromJSON/ToJSON roundtrips"
164 ty = typeOf (undefined :: a)
166data Location = Location { lat :: Double
167 , lon :: Double } deriving (Eq, Generic, Show)
169data Tweet = Tweet { user :: Text
170 , postDate :: UTCTime
171 , message :: Text
172 , age :: Int
173 , location :: Location
174 , extra :: Maybe Text }
175 deriving (Eq, Generic, Show)
177instance ToJSON Tweet where
178 toJSON = genericToJSON defaultOptions
179instance FromJSON Tweet where
180 parseJSON = genericParseJSON defaultOptions
181instance ToJSON Location where
182 toJSON = genericToJSON defaultOptions
183instance FromJSON Location where
184 parseJSON = genericParseJSON defaultOptions
186data ParentMapping = ParentMapping deriving (Eq, Show)
188instance ToJSON ParentMapping where
189 toJSON ParentMapping =
190 object ["properties" .=
191 object [ "user" .= object ["type" .= ("string" :: Text)
192 , "fielddata" .= True
193 ]
194 -- Serializing the date as a date is breaking other tests, mysteriously.
195 -- , "postDate" .= object [ "type" .= ("date" :: Text)
196 -- , "format" .= ("YYYY-MM-dd`T`HH:mm:ss.SSSZZ" :: Text)]
197 , "message" .= object ["type" .= ("string" :: Text)]
198 , "age" .= object ["type" .= ("integer" :: Text)]
199 , "location" .= object ["type" .= ("geo_point" :: Text)]
200 , "extra" .= object ["type" .= ("keyword" :: Text)]
201 ]]
203data ChildMapping = ChildMapping deriving (Eq, Show)
205instance ToJSON ChildMapping where
206 toJSON ChildMapping =
207 object ["_parent" .= object ["type" .= ("parent" :: Text)]
208 , "properties" .=
209 object [ "user" .= object ["type" .= ("string" :: Text)
210 , "fielddata" .= True
211 ]
212 -- Serializing the date as a date is breaking other tests, mysteriously.
213 -- , "postDate" .= object [ "type" .= ("date" :: Text)
214 -- , "format" .= ("YYYY-MM-dd`T`HH:mm:ss.SSSZZ" :: Text)]
215 , "message" .= object ["type" .= ("string" :: Text)]
216 , "age" .= object ["type" .= ("integer" :: Text)]
217 , "location" .= object ["type" .= ("geo_point" :: Text)]
218 , "extra" .= object ["type" .= ("keyword" :: Text)]
219 ]]
221data TweetMapping = TweetMapping deriving (Eq, Show)
223instance ToJSON TweetMapping where
224 toJSON TweetMapping =
225 object ["tweet" .=
226 object ["properties" .=
227 object [ "user" .= object [ "type" .= ("string" :: Text)
228 , "fielddata" .= True
229 ]
230 -- Serializing the date as a date is breaking other tests, mysteriously.
231 -- , "postDate" .= object [ "type" .= ("date" :: Text)
232 -- , "format" .= ("YYYY-MM-dd`T`HH:mm:ss.SSSZZ" :: Text)]
233 , "message" .= object ["type" .= ("string" :: Text)]
234 , "age" .= object ["type" .= ("integer" :: Text)]
235 , "location" .= object ["type" .= ("geo_point" :: Text)]
236 , "extra" .= object ["type" .= ("keyword" :: Text)]
237 ]]]
239exampleTweet :: Tweet
240exampleTweet = Tweet { user = "bitemyapp"
241 , postDate = UTCTime
242 (ModifiedJulianDay 55000)
243 (secondsToDiffTime 10)
244 , message = "Use haskell!"
245 , age = 10000
246 , location = Location 40.12 (-71.34)
247 , extra = Nothing }
249tweetWithExtra :: Tweet
250tweetWithExtra = Tweet { user = "bitemyapp"
251 , postDate = UTCTime
252 (ModifiedJulianDay 55000)
253 (secondsToDiffTime 10)
254 , message = "Use haskell!"
255 , age = 10000
256 , location = Location 40.12 (-71.34)
257 , extra = Just "blah blah" }
259newAge :: Int
260newAge = 31337
262newUser :: Text
263newUser = "someotherapp"
265tweetPatch :: Value
266tweetPatch =
267 object [ "age" .= newAge
268 , "user" .= newUser
269 ]
271patchedTweet :: Tweet
272patchedTweet = exampleTweet{age = newAge, user = newUser}
274otherTweet :: Tweet
275otherTweet = Tweet { user = "notmyapp"
276 , postDate = UTCTime
277 (ModifiedJulianDay 55000)
278 (secondsToDiffTime 11)
279 , message = "Use haskell!"
280 , age = 1000
281 , location = Location 40.12 (-71.34)
282 , extra = Nothing }
284resetIndex :: BH IO ()
285resetIndex = do
286 _ <- deleteExampleIndex
287 _ <- createExampleIndex
288 _ <- putMapping testIndex testMapping TweetMapping
289 return ()
291insertData :: BH IO Reply
292insertData = do
293 resetIndex
294 insertData' defaultIndexDocumentSettings
296insertData' :: IndexDocumentSettings -> BH IO Reply
297insertData' ids = do
298 r <- indexDocument testIndex testMapping ids exampleTweet (DocId "1")
299 _ <- refreshIndex testIndex
300 return r
302updateData :: BH IO Reply
303updateData = do
304 r <- updateDocument testIndex testMapping defaultIndexDocumentSettings tweetPatch (DocId "1")
305 _ <- refreshIndex testIndex
306 return r
308insertOther :: BH IO ()
309insertOther = do
310 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings otherTweet (DocId "2")
311 _ <- refreshIndex testIndex
312 return ()
314insertExtra :: BH IO ()
315insertExtra = do
316 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings tweetWithExtra (DocId "4")
317 _ <- refreshIndex testIndex
318 return ()
320insertWithSpaceInId :: BH IO ()
321insertWithSpaceInId = do
322 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings exampleTweet (DocId "Hello World")
323 _ <- refreshIndex testIndex
324 return ()
326searchTweet :: Search -> BH IO (Either EsError Tweet)
327searchTweet search = do
328 result <- searchTweets search
329 let myTweet :: Either EsError Tweet
330 myTweet = grabFirst result
331 return myTweet
333searchTweets :: Search -> BH IO (Either EsError (SearchResult Tweet))
334searchTweets search = parseEsResponse =<< searchByIndex testIndex search
336searchExpectNoResults :: Search -> BH IO ()
337searchExpectNoResults search = do
338 result <- searchTweets search
339 let emptyHits = fmap (hits . searchHits) result
340 liftIO $
341 emptyHits `shouldBe` Right []
343searchExpectAggs :: Search -> BH IO ()
344searchExpectAggs search = do
345 reply <- searchByIndex testIndex search
346 let isEmpty x = return (M.null x)
347 let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
348 liftIO $
349 (result >>= aggregations >>= isEmpty) `shouldBe` Just False
351searchValidBucketAgg :: (BucketAggregation a, FromJSON a, Show a) =>
352 Search -> Text -> (Text -> AggregationResults -> Maybe (Bucket a)) -> BH IO ()
353searchValidBucketAgg search aggKey extractor = do
354 reply <- searchByIndex testIndex search
355 let bucketDocs = docCount . head . buckets
356 let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
357 let count = result >>= aggregations >>= extractor aggKey >>= \x -> return (bucketDocs x)
358 liftIO $
359 count `shouldBe` Just 1
361searchTermsAggHint :: [ExecutionHint] -> BH IO ()
362searchTermsAggHint hints = do
363 let terms hint = TermsAgg $ (mkTermsAggregation "user") { termExecutionHint = Just hint }
364 let search hint = mkAggregateSearch Nothing $ mkAggregations "users" $ terms hint
365 forM_ hints $ searchExpectAggs . search
366 forM_ hints (\x -> searchValidBucketAgg (search x) "users" toTerms)
368searchTweetHighlight :: Search -> BH IO (Either EsError (Maybe HitHighlight))
369searchTweetHighlight search = do
370 result <- searchTweets search
371 let myHighlight = fmap (hitHighlight . head . hits . searchHits) result
372 return myHighlight
374searchExpectSource :: Source -> Either EsError Value -> BH IO ()
375searchExpectSource src expected = do
376 _ <- insertData
377 let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
378 let search = (mkSearch (Just query) Nothing) { source = Just src }
379 reply <- searchByIndex testIndex search
380 result <- parseEsResponse reply
381 let value = grabFirst result
382 liftIO $
383 value `shouldBe` expected
385withSnapshotRepo
386 :: ( MonadMask m
387 , MonadBH m
388 )
389 => SnapshotRepoName
390 -> (GenericSnapshotRepo -> m a)
391 -> m a
392withSnapshotRepo srn@(SnapshotRepoName n) f = do
393 repoPaths <- liftIO getRepoPaths
394 -- we'll use the first repo path if available, otherwise system temp
395 -- dir. Note that this will fail on ES > 1.6, so be sure you use
396 -- @when' canSnapshot@.
397 case repoPaths of
398 (firstRepoPath:_) -> withTempDirectory firstRepoPath (T.unpack n) $ \dir -> bracket (alloc dir) free f
399 [] -> withSystemTempDirectory (T.unpack n) $ \dir -> bracket (alloc dir) free f
400 where
401 alloc dir = do
402 liftIO (setFileMode dir mode)
403 let repo = FsSnapshotRepo srn "bloodhound-tests-backups" True Nothing Nothing Nothing
404 resp <- updateSnapshotRepo defaultSnapshotRepoUpdateSettings repo
405 liftIO (validateStatus resp 200)
406 return (toGSnapshotRepo repo)
407 mode = ownerModes `unionFileModes` groupModes `unionFileModes` otherModes
408 free GenericSnapshotRepo {..} = do
409 resp <- deleteSnapshotRepo gSnapshotRepoName
410 liftIO (validateStatus resp 200)
413withSnapshot
414 :: ( MonadMask m
415 , MonadBH m
416 )
417 => SnapshotRepoName
418 -> SnapshotName
419 -> m a
420 -> m a
421withSnapshot srn sn = bracket_ alloc free
422 where
423 alloc = do
424 resp <- createSnapshot srn sn createSettings
425 liftIO (validateStatus resp 200)
426 -- We'll make this synchronous for testing purposes
427 createSettings = defaultSnapshotCreateSettings { snapWaitForCompletion = True
428 , snapIndices = Just (IndexList (testIndex :| []))
429 -- We don't actually need to back up any data
430 }
431 free = do
432 deleteSnapshot srn sn
436data BulkTest = BulkTest { name :: Text } deriving (Eq, Generic, Show)
437instance FromJSON BulkTest where
438 parseJSON = genericParseJSON defaultOptions
439instance ToJSON BulkTest where
440 toJSON = genericToJSON defaultOptions
442class GApproxEq f where
443 gApproxEq :: f a -> f a -> Bool
445-- | Unit type
446instance GApproxEq U1 where
447 gApproxEq U1 U1 = True
449-- | Sum type, ensure same constructors, recurse
450instance (GApproxEq a, GApproxEq b) => GApproxEq (a :+: b) where
451 gApproxEq (L1 a) (L1 b) = gApproxEq a b
452 gApproxEq (R1 a) (R1 b) = gApproxEq a b
453 gApproxEq _ _ = False
455-- | Product type, ensure each field is approx eq
456instance (GApproxEq a, GApproxEq b) => GApproxEq (a :*: b) where
457 gApproxEq (a1 :*: b1) (a2 :*: b2) = gApproxEq a1 a2 && gApproxEq b1 b2
459-- | Value type, actually check the values for approx equality
460instance (ApproxEq a) => GApproxEq (K1 i a) where
461 gApproxEq (K1 a) (K1 b) = a =~ b
463instance (GApproxEq f) => GApproxEq (M1 i t f) where
464 gApproxEq (M1 a) (M1 b) = gApproxEq a b
466-- | Typeclass for "equal where it matters". Use this to specify
467-- less-strict equivalence for things such as lists that can wind up
468-- in an unpredictable order
469class ApproxEq a where
470 (=~) :: a -> a -> Bool
471 default (=~) :: (Generic a, GApproxEq (Rep a)) => a -> a -> Bool
472 a =~ b = gApproxEq (G.from a) (G.from b)
474 showApproxEq :: a -> String
475 default showApproxEq :: (Show a) => a -> String
476 showApproxEq = show
478instance ApproxEq NominalDiffTime where (=~) = (==)
479instance ApproxEq UTCTime where (=~) = (==)
480instance ApproxEq Text where (=~) = (==)
481instance ApproxEq Bool where (=~) = (==)
482instance ApproxEq Int where (=~) = (==)
483instance ApproxEq Double where (=~) = (==)
484instance (ApproxEq a, Show a) => ApproxEq (NonEmpty a)
485instance (ApproxEq a, Show a) => ApproxEq (Maybe a)
486instance ApproxEq GeoPoint
487instance ApproxEq Regexp
488instance ApproxEq RangeValue
489instance ApproxEq LessThan
490instance ApproxEq LessThanEq
491instance ApproxEq LessThanD
492instance ApproxEq LessThanEqD
493instance ApproxEq GreaterThan
494instance ApproxEq GreaterThanEq
495instance ApproxEq GreaterThanD
496instance ApproxEq GreaterThanEqD
497instance ApproxEq MinimumMatchHighLow
498instance ApproxEq RegexpFlag
499instance ApproxEq RegexpFlags
500instance ApproxEq NullValue
501instance ApproxEq Version
502instance ApproxEq VersionNumber
503instance ApproxEq DistanceRange
504instance ApproxEq IndexName
505instance ApproxEq MappingName
506instance ApproxEq DocId
507instance ApproxEq IndexAliasRouting
508instance ApproxEq RoutingValue
509instance ApproxEq ShardCount
510instance ApproxEq ReplicaCount
511instance ApproxEq TemplateName
512instance ApproxEq TemplatePattern
513instance ApproxEq QueryString
514instance ApproxEq FieldName
515instance ApproxEq CacheName
516instance ApproxEq CacheKey
517instance ApproxEq Existence
518instance ApproxEq CutoffFrequency
519instance ApproxEq Analyzer
520instance ApproxEq Lenient
521instance ApproxEq Tiebreaker
522instance ApproxEq Boost
523instance ApproxEq BoostTerms
524instance ApproxEq MaxExpansions
525instance ApproxEq MinimumMatch
526instance ApproxEq DisableCoord
527instance ApproxEq IgnoreTermFrequency
528instance ApproxEq MinimumTermFrequency
529instance ApproxEq MaxQueryTerms
530instance ApproxEq Fuzziness
531instance ApproxEq PrefixLength
532instance ApproxEq TypeName
533instance ApproxEq PercentMatch
534instance ApproxEq StopWord
535instance ApproxEq QueryPath
536instance ApproxEq AllowLeadingWildcard
537instance ApproxEq LowercaseExpanded
538instance ApproxEq EnablePositionIncrements
539instance ApproxEq AnalyzeWildcard
540instance ApproxEq GeneratePhraseQueries
541instance ApproxEq Locale
542instance ApproxEq MaxWordLength
543instance ApproxEq MinWordLength
544instance ApproxEq PhraseSlop
545instance ApproxEq MinDocFrequency
546instance ApproxEq MaxDocFrequency
547instance ApproxEq Filter
548instance ApproxEq Query
549instance ApproxEq SimpleQueryStringQuery
550instance ApproxEq FieldOrFields
551instance ApproxEq SimpleQueryFlag
552instance ApproxEq RegexpQuery
553instance ApproxEq QueryStringQuery
554instance ApproxEq RangeQuery
555instance ApproxEq PrefixQuery
556instance ApproxEq NestedQuery
557instance ApproxEq MoreLikeThisFieldQuery
558instance ApproxEq MoreLikeThisQuery
559instance ApproxEq IndicesQuery
560instance ApproxEq HasParentQuery
561instance ApproxEq HasChildQuery
562instance ApproxEq FuzzyQuery
563instance ApproxEq FuzzyLikeFieldQuery
564instance ApproxEq FuzzyLikeThisQuery
565instance ApproxEq DisMaxQuery
566instance ApproxEq CommonTermsQuery
567instance ApproxEq CommonMinimumMatch
568instance ApproxEq BoostingQuery
569instance ApproxEq BoolQuery
570instance ApproxEq MatchQuery
571instance ApproxEq MultiMatchQueryType
572instance ApproxEq BooleanOperator
573instance ApproxEq ZeroTermsQuery
574instance ApproxEq MatchQueryType
575instance ApproxEq AliasRouting
576instance ApproxEq IndexAliasCreate
577instance ApproxEq SearchAliasRouting
578instance ApproxEq ScoreType
579instance ApproxEq Distance
580instance ApproxEq DistanceUnit
581instance ApproxEq DistanceType
582instance ApproxEq OptimizeBbox
583instance ApproxEq GeoBoundingBoxConstraint
584instance ApproxEq GeoFilterType
585instance ApproxEq GeoBoundingBox
586instance ApproxEq LatLon
587instance ApproxEq RangeExecution
588instance ApproxEq FSType
589instance ApproxEq CompoundFormat
590instance ApproxEq InitialShardCount
591instance ApproxEq Bytes
592instance ApproxEq ReplicaBounds
593instance ApproxEq Term
594instance ApproxEq BoolMatch
595instance ApproxEq MultiMatchQuery
596instance ApproxEq IndexSettings
597instance ApproxEq AllocationPolicy
598instance ApproxEq Char where
599 (=~) = (==)
600instance ApproxEq Vers.Version where
601 (=~) = (==)
602instance (ApproxEq a, Show a) => ApproxEq [a] where
603 as =~ bs = and (zipWith (=~) as bs)
604instance (ApproxEq l, Show l, ApproxEq r, Show r) => ApproxEq (Either l r) where
605 Left a =~ Left b = a =~ b
606 Right a =~ Right b = a =~ b
607 _ =~ _ = False
608 showApproxEq (Left x) = "Left " <> showApproxEq x
609 showApproxEq (Right x) = "Right " <> showApproxEq x
610instance ApproxEq NodeAttrFilter
611instance ApproxEq NodeAttrName
612instance ApproxEq BuildHash
613instance ApproxEq TemplateQueryKeyValuePairs where
614 (=~) = (==)
615instance ApproxEq TemplateQueryInline
616instance ApproxEq Size
617instance ApproxEq PhraseSuggesterHighlighter
618instance ApproxEq PhraseSuggesterCollate
619instance ApproxEq PhraseSuggester
620instance ApproxEq SuggestType
621instance ApproxEq Suggest
623-- | Due to the way nodeattrfilters get serialized here, they may come
624-- out in a different order, but they are morally equivalent
625instance ApproxEq UpdatableIndexSetting where
626 RoutingAllocationInclude a =~ RoutingAllocationInclude b =
627 NE.sort a =~ NE.sort b
628 RoutingAllocationExclude a =~ RoutingAllocationExclude b =
629 NE.sort a =~ NE.sort b
630 RoutingAllocationRequire a =~ RoutingAllocationRequire b =
631 NE.sort a =~ NE.sort b
632 a =~ b = a == b
633 showApproxEq (RoutingAllocationInclude xs) = show (RoutingAllocationInclude (NE.sort xs))
634 showApproxEq (RoutingAllocationExclude xs) = show (RoutingAllocationExclude (NE.sort xs))
635 showApproxEq (RoutingAllocationRequire xs) = show (RoutingAllocationRequire (NE.sort xs))
636 showApproxEq x = show x
639noDuplicates :: Eq a => [a] -> Bool
640noDuplicates xs = nub xs == xs
642instance Arbitrary NominalDiffTime where
643 arbitrary = fromInteger <$> arbitrary
645#if !MIN_VERSION_QuickCheck(2,8,0)
646instance (Arbitrary k, Ord k, Arbitrary v) => Arbitrary (M.Map k v) where
647 arbitrary = M.fromList <$> arbitrary
648#endif
650instance Arbitrary Text where
651 arbitrary = T.pack <$> arbitrary
653instance Arbitrary UTCTime where
654 arbitrary = UTCTime
655 <$> arbitrary
656 <*> (fromRational . toRational <$> choose (0::Double, 86400))
658instance Arbitrary Day where
659 arbitrary = ModifiedJulianDay <$> (2000 +) <$> arbitrary
660 shrink = (ModifiedJulianDay <$>) . shrink . toModifiedJulianDay
662#if !MIN_VERSION_QuickCheck(2,9,0)
663instance Arbitrary a => Arbitrary (NonEmpty a) where
664 arbitrary = liftA2 (:|) arbitrary arbitrary
665#endif
667arbitraryScore :: Gen Score
668arbitraryScore = fmap getPositive <$> arbitrary
670instance (Arbitrary a, Typeable a) => Arbitrary (Hit a) where
671 arbitrary = Hit <$> arbitrary
672 <*> arbitrary
673 <*> arbitrary
674 <*> arbitraryScore
675 <*> arbitrary
676 <*> arbitrary
677 shrink = genericShrink
680instance (Arbitrary a, Typeable a) => Arbitrary (SearchHits a) where
681 arbitrary = reduceSize $ do
682 tot <- getPositive <$> arbitrary
683 score <- arbitraryScore
684 hs <- arbitrary
685 return $ SearchHits tot score hs
686 shrink = genericShrink
688reduceSize :: Gen a -> Gen a
689reduceSize f = sized $ \n -> resize (n `div` 2) f
691getSource :: EsResult a -> Maybe a
692getSource = fmap _source . foundResult
694grabFirst :: Either EsError (SearchResult a) -> Either EsError a
695grabFirst r =
696 case fmap (hitSource . head . hits . searchHits) r of
697 (Left e) -> Left e
698 (Right Nothing) -> Left (EsError 500 "Source was missing")
699 (Right (Just x)) -> Right x
701-------------------------------------------------------------------------------
702arbitraryAlphaNum :: Gen Char
703arbitraryAlphaNum = oneof [choose ('a', 'z')
704 ,choose ('A','Z')
705 , choose ('0', '9')]
707instance Arbitrary RoutingValue where
708 arbitrary = RoutingValue . T.pack <$> listOf1 arbitraryAlphaNum
710instance Arbitrary AliasRouting where
711 arbitrary = oneof [allAlias
712 ,one
713 ,theOther
714 ,both]
715 where one = GranularAliasRouting
716 <$> (Just <$> arbitrary)
717 <*> pure Nothing
718 theOther = GranularAliasRouting Nothing
719 <$> (Just <$> arbitrary)
720 both = GranularAliasRouting
721 <$> (Just <$> arbitrary)
722 <*> (Just <$> arbitrary)
723 allAlias = AllAliasRouting <$> arbitrary
724 shrink = genericShrink
727instance Arbitrary FieldName where
728 arbitrary = FieldName . T.pack <$> listOf1 arbitraryAlphaNum
729 shrink = genericShrink
732instance Arbitrary RegexpFlags where
733 arbitrary = oneof [ pure AllRegexpFlags
734 , pure NoRegexpFlags
735 , SomeRegexpFlags <$> genUniqueFlags
736 ]
737 where genUniqueFlags = NE.fromList . nub <$> listOf1 arbitrary
738 shrink = genericShrink
741instance Arbitrary IndexAliasCreate where
742 arbitrary = IndexAliasCreate <$> arbitrary <*> reduceSize arbitrary
743 shrink = genericShrink
745instance Arbitrary Query where
746 arbitrary = reduceSize $ oneof [ TermQuery <$> arbitrary <*> arbitrary
747 , TermsQuery <$> arbitrary <*> arbitrary
748 , QueryMatchQuery <$> arbitrary
749 , QueryMultiMatchQuery <$> arbitrary
750 , QueryBoolQuery <$> arbitrary
751 , QueryBoostingQuery <$> arbitrary
752 , QueryCommonTermsQuery <$> arbitrary
753 , ConstantScoreQuery <$> arbitrary <*> arbitrary
754 , QueryDisMaxQuery <$> arbitrary
755 , QueryFuzzyLikeThisQuery <$> arbitrary
756 , QueryFuzzyLikeFieldQuery <$> arbitrary
757 , QueryFuzzyQuery <$> arbitrary
758 , QueryHasChildQuery <$> arbitrary
759 , QueryHasParentQuery <$> arbitrary
760 , IdsQuery <$> arbitrary <*> arbitrary
761 , QueryIndicesQuery <$> arbitrary
762 , MatchAllQuery <$> arbitrary
763 , QueryMoreLikeThisQuery <$> arbitrary
764 , QueryMoreLikeThisFieldQuery <$> arbitrary
765 , QueryNestedQuery <$> arbitrary
766 , QueryPrefixQuery <$> arbitrary
767 , QueryQueryStringQuery <$> arbitrary
768 , QuerySimpleQueryStringQuery <$> arbitrary
769 , QueryRangeQuery <$> arbitrary
770 , QueryRegexpQuery <$> arbitrary
771 , QueryTemplateQueryInline <$> arbitrary
772 ]
773 shrink = genericShrink
775instance Arbitrary Filter where
776 arbitrary = Filter <$> arbitrary
777 shrink = genericShrink
779instance Arbitrary ReplicaBounds where
780 arbitrary = oneof [ replicasBounded
781 , replicasLowerBounded
782 , pure ReplicasUnbounded
783 ]
784 where replicasBounded = do Positive a <- arbitrary
785 Positive b <- arbitrary
786 return (ReplicasBounded a b)
787 replicasLowerBounded = do Positive a <- arbitrary
788 return (ReplicasLowerBounded a)
790instance Arbitrary NodeAttrName where
791 arbitrary = NodeAttrName . T.pack <$> listOf1 arbitraryAlphaNum
794instance Arbitrary NodeAttrFilter where
795 arbitrary = do
796 n <- arbitrary
797 s:ss <- listOf1 (listOf1 arbitraryAlphaNum)
798 let ts = T.pack <$> s :| ss
799 return (NodeAttrFilter n ts)
800 shrink = genericShrink
802instance Arbitrary VersionNumber where
803 arbitrary = mk . fmap getPositive . getNonEmpty <$> arbitrary
804 where
805 mk versions = VersionNumber (Vers.Version versions [])
807instance Arbitrary TemplateQueryKeyValuePairs where
808 arbitrary = TemplateQueryKeyValuePairs . HM.fromList <$> arbitrary
809 shrink (TemplateQueryKeyValuePairs x) = map (TemplateQueryKeyValuePairs . HM.fromList) . shrink $ HM.toList x
811instance Arbitrary IndexName where arbitrary = sopArbitrary; shrink = genericShrink
812instance Arbitrary MappingName where arbitrary = sopArbitrary; shrink = genericShrink
813instance Arbitrary DocId where arbitrary = sopArbitrary; shrink = genericShrink
814instance Arbitrary Version where arbitrary = sopArbitrary; shrink = genericShrink
815instance Arbitrary BuildHash where arbitrary = sopArbitrary; shrink = genericShrink
816instance Arbitrary IndexAliasRouting where arbitrary = sopArbitrary; shrink = genericShrink
817instance Arbitrary ShardCount where arbitrary = sopArbitrary; shrink = genericShrink
818instance Arbitrary ReplicaCount where arbitrary = sopArbitrary; shrink = genericShrink
819instance Arbitrary TemplateName where arbitrary = sopArbitrary; shrink = genericShrink
820instance Arbitrary TemplatePattern where arbitrary = sopArbitrary; shrink = genericShrink
821instance Arbitrary QueryString where arbitrary = sopArbitrary; shrink = genericShrink
822instance Arbitrary CacheName where arbitrary = sopArbitrary; shrink = genericShrink
823instance Arbitrary CacheKey where arbitrary = sopArbitrary; shrink = genericShrink
824instance Arbitrary Existence where arbitrary = sopArbitrary; shrink = genericShrink
825instance Arbitrary CutoffFrequency where arbitrary = sopArbitrary; shrink = genericShrink
826instance Arbitrary Analyzer where arbitrary = sopArbitrary; shrink = genericShrink
827instance Arbitrary MaxExpansions where arbitrary = sopArbitrary; shrink = genericShrink
828instance Arbitrary Lenient where arbitrary = sopArbitrary; shrink = genericShrink
829instance Arbitrary Tiebreaker where arbitrary = sopArbitrary; shrink = genericShrink
830instance Arbitrary Boost where arbitrary = sopArbitrary; shrink = genericShrink
831instance Arbitrary BoostTerms where arbitrary = sopArbitrary; shrink = genericShrink
832instance Arbitrary MinimumMatch where arbitrary = sopArbitrary; shrink = genericShrink
833instance Arbitrary DisableCoord where arbitrary = sopArbitrary; shrink = genericShrink
834instance Arbitrary IgnoreTermFrequency where arbitrary = sopArbitrary; shrink = genericShrink
835instance Arbitrary MinimumTermFrequency where arbitrary = sopArbitrary; shrink = genericShrink
836instance Arbitrary MaxQueryTerms where arbitrary = sopArbitrary; shrink = genericShrink
837instance Arbitrary Fuzziness where arbitrary = sopArbitrary; shrink = genericShrink
838instance Arbitrary PrefixLength where arbitrary = sopArbitrary; shrink = genericShrink
839instance Arbitrary TypeName where arbitrary = sopArbitrary; shrink = genericShrink
840instance Arbitrary PercentMatch where arbitrary = sopArbitrary; shrink = genericShrink
841instance Arbitrary StopWord where arbitrary = sopArbitrary; shrink = genericShrink
842instance Arbitrary QueryPath where arbitrary = sopArbitrary; shrink = genericShrink
843instance Arbitrary AllowLeadingWildcard where arbitrary = sopArbitrary; shrink = genericShrink
844instance Arbitrary LowercaseExpanded where arbitrary = sopArbitrary; shrink = genericShrink
845instance Arbitrary EnablePositionIncrements where arbitrary = sopArbitrary; shrink = genericShrink
846instance Arbitrary AnalyzeWildcard where arbitrary = sopArbitrary; shrink = genericShrink
847instance Arbitrary GeneratePhraseQueries where arbitrary = sopArbitrary; shrink = genericShrink
848instance Arbitrary Locale where arbitrary = sopArbitrary; shrink = genericShrink
849instance Arbitrary MaxWordLength where arbitrary = sopArbitrary; shrink = genericShrink
850instance Arbitrary MinWordLength where arbitrary = sopArbitrary; shrink = genericShrink
851instance Arbitrary PhraseSlop where arbitrary = sopArbitrary; shrink = genericShrink
852instance Arbitrary MinDocFrequency where arbitrary = sopArbitrary; shrink = genericShrink
853instance Arbitrary MaxDocFrequency where arbitrary = sopArbitrary; shrink = genericShrink
854instance Arbitrary Regexp where arbitrary = sopArbitrary; shrink = genericShrink
855instance Arbitrary SimpleQueryStringQuery where arbitrary = sopArbitrary; shrink = genericShrink
856instance Arbitrary FieldOrFields where arbitrary = sopArbitrary; shrink = genericShrink
857instance Arbitrary SimpleQueryFlag where arbitrary = sopArbitrary; shrink = genericShrink
858instance Arbitrary RegexpQuery where arbitrary = sopArbitrary; shrink = genericShrink
859instance Arbitrary QueryStringQuery where arbitrary = sopArbitrary; shrink = genericShrink
860instance Arbitrary RangeQuery where arbitrary = sopArbitrary; shrink = genericShrink
861instance Arbitrary RangeValue where arbitrary = sopArbitrary; shrink = genericShrink
862instance Arbitrary PrefixQuery where arbitrary = sopArbitrary; shrink = genericShrink
863instance Arbitrary NestedQuery where arbitrary = sopArbitrary; shrink = genericShrink
864instance Arbitrary MoreLikeThisFieldQuery where arbitrary = sopArbitrary; shrink = genericShrink
865instance Arbitrary MoreLikeThisQuery where arbitrary = sopArbitrary; shrink = genericShrink
866instance Arbitrary IndicesQuery where arbitrary = sopArbitrary; shrink = genericShrink
867instance Arbitrary HasParentQuery where arbitrary = sopArbitrary; shrink = genericShrink
868instance Arbitrary HasChildQuery where arbitrary = sopArbitrary; shrink = genericShrink
869instance Arbitrary FuzzyQuery where arbitrary = sopArbitrary; shrink = genericShrink
870instance Arbitrary FuzzyLikeFieldQuery where arbitrary = sopArbitrary; shrink = genericShrink
871instance Arbitrary FuzzyLikeThisQuery where arbitrary = sopArbitrary; shrink = genericShrink
872instance Arbitrary DisMaxQuery where arbitrary = sopArbitrary; shrink = genericShrink
873instance Arbitrary CommonTermsQuery where arbitrary = sopArbitrary; shrink = genericShrink
874instance Arbitrary DistanceRange where arbitrary = sopArbitrary; shrink = genericShrink
875instance Arbitrary MultiMatchQuery where arbitrary = sopArbitrary; shrink = genericShrink
876instance Arbitrary LessThanD where arbitrary = sopArbitrary; shrink = genericShrink
877instance Arbitrary LessThanEqD where arbitrary = sopArbitrary; shrink = genericShrink
878instance Arbitrary GreaterThanD where arbitrary = sopArbitrary; shrink = genericShrink
879instance Arbitrary GreaterThanEqD where arbitrary = sopArbitrary; shrink = genericShrink
880instance Arbitrary LessThan where arbitrary = sopArbitrary; shrink = genericShrink
881instance Arbitrary LessThanEq where arbitrary = sopArbitrary; shrink = genericShrink
882instance Arbitrary GreaterThan where arbitrary = sopArbitrary; shrink = genericShrink
883instance Arbitrary GreaterThanEq where arbitrary = sopArbitrary; shrink = genericShrink
884instance Arbitrary GeoPoint where arbitrary = sopArbitrary; shrink = genericShrink
885instance Arbitrary NullValue where arbitrary = sopArbitrary; shrink = genericShrink
886instance Arbitrary MinimumMatchHighLow where arbitrary = sopArbitrary; shrink = genericShrink
887instance Arbitrary CommonMinimumMatch where arbitrary = sopArbitrary; shrink = genericShrink
888instance Arbitrary BoostingQuery where arbitrary = sopArbitrary; shrink = genericShrink
889instance Arbitrary BoolQuery where arbitrary = sopArbitrary; shrink = genericShrink
890instance Arbitrary MatchQuery where arbitrary = sopArbitrary; shrink = genericShrink
891instance Arbitrary MultiMatchQueryType where arbitrary = sopArbitrary; shrink = genericShrink
892instance Arbitrary BooleanOperator where arbitrary = sopArbitrary; shrink = genericShrink
893instance Arbitrary ZeroTermsQuery where arbitrary = sopArbitrary; shrink = genericShrink
894instance Arbitrary MatchQueryType where arbitrary = sopArbitrary; shrink = genericShrink
895instance Arbitrary SearchAliasRouting where arbitrary = sopArbitrary; shrink = genericShrink
896instance Arbitrary ScoreType where arbitrary = sopArbitrary; shrink = genericShrink
897instance Arbitrary Distance where arbitrary = sopArbitrary; shrink = genericShrink
898instance Arbitrary DistanceUnit where arbitrary = sopArbitrary; shrink = genericShrink
899instance Arbitrary DistanceType where arbitrary = sopArbitrary; shrink = genericShrink
900instance Arbitrary OptimizeBbox where arbitrary = sopArbitrary; shrink = genericShrink
901instance Arbitrary GeoBoundingBoxConstraint where arbitrary = sopArbitrary; shrink = genericShrink
902instance Arbitrary GeoFilterType where arbitrary = sopArbitrary; shrink = genericShrink
903instance Arbitrary GeoBoundingBox where arbitrary = sopArbitrary; shrink = genericShrink
904instance Arbitrary LatLon where arbitrary = sopArbitrary; shrink = genericShrink
905instance Arbitrary RangeExecution where arbitrary = sopArbitrary; shrink = genericShrink
906instance Arbitrary RegexpFlag where arbitrary = sopArbitrary; shrink = genericShrink
907instance Arbitrary BoolMatch where arbitrary = sopArbitrary; shrink = genericShrink
908instance Arbitrary Term where arbitrary = sopArbitrary; shrink = genericShrink
909instance Arbitrary IndexSettings where arbitrary = sopArbitrary; shrink = genericShrink
910instance Arbitrary TokenChar where arbitrary = sopArbitrary; shrink = genericShrink
911instance Arbitrary Ngram where arbitrary = sopArbitrary; shrink = genericShrink
912instance Arbitrary TokenizerDefinition where arbitrary = sopArbitrary; shrink = genericShrink
913instance Arbitrary AnalyzerDefinition where arbitrary = sopArbitrary; shrink = genericShrink
914instance Arbitrary Analysis where arbitrary = sopArbitrary; shrink = genericShrink
915instance Arbitrary Tokenizer where arbitrary = sopArbitrary; shrink = genericShrink
916instance Arbitrary UpdatableIndexSetting where arbitrary = sopArbitrary; shrink = genericShrink
917instance Arbitrary Bytes where arbitrary = sopArbitrary; shrink = genericShrink
918instance Arbitrary AllocationPolicy where arbitrary = sopArbitrary; shrink = genericShrink
919instance Arbitrary InitialShardCount where arbitrary = sopArbitrary; shrink = genericShrink
920instance Arbitrary FSType where arbitrary = sopArbitrary; shrink = genericShrink
921instance Arbitrary CompoundFormat where arbitrary = sopArbitrary; shrink = genericShrink
922instance Arbitrary FsSnapshotRepo where arbitrary = sopArbitrary; shrink = genericShrink
923instance Arbitrary SnapshotRepoName where arbitrary = sopArbitrary; shrink = genericShrink
924instance Arbitrary TemplateQueryInline where arbitrary = sopArbitrary; shrink = genericShrink
925instance Arbitrary PhraseSuggesterCollate where arbitrary = sopArbitrary; shrink = genericShrink
926instance Arbitrary PhraseSuggesterHighlighter where arbitrary = sopArbitrary; shrink = genericShrink
927instance Arbitrary Size where arbitrary = sopArbitrary; shrink = genericShrink
928instance Arbitrary PhraseSuggester where arbitrary = sopArbitrary; shrink = genericShrink
929instance Arbitrary SuggestType where arbitrary = sopArbitrary; shrink = genericShrink
930instance Arbitrary Suggest where arbitrary = sopArbitrary; shrink = genericShrink
932newtype UpdatableIndexSetting' = UpdatableIndexSetting' UpdatableIndexSetting
933 deriving (Show, Eq, ToJSON, FromJSON, ApproxEq, Typeable)
935instance Arbitrary UpdatableIndexSetting' where
936 arbitrary = do
937 settings <- arbitrary
938 return $ UpdatableIndexSetting' $ case settings of
939 RoutingAllocationInclude xs -> RoutingAllocationInclude (dropDuplicateAttrNames xs)
940 RoutingAllocationExclude xs -> RoutingAllocationExclude (dropDuplicateAttrNames xs)
941 RoutingAllocationRequire xs -> RoutingAllocationRequire (dropDuplicateAttrNames xs)
942 x -> x
943 where
944 dropDuplicateAttrNames = NE.fromList . L.nubBy sameAttrName . NE.toList
945 sameAttrName a b = nodeAttrFilterName a == nodeAttrFilterName b
947main :: IO ()
948main = hspec $ do
950 describe "index create/delete API" $ do
951 it "creates and then deletes the requested index" $ withTestEnv $ do
952 -- priming state.
953 _ <- deleteExampleIndex
954 resp <- createExampleIndex
955 deleteResp <- deleteExampleIndex
956 liftIO $ do
957 validateStatus resp 200
958 validateStatus deleteResp 200
960 describe "error parsing" $ do
961 it "can parse EsErrors for < 2.0" $ when' (atmost es16) $ withTestEnv $ do
962 res <- getDocument (IndexName "bogus") (MappingName "also_bogus") (DocId "bogus_as_well")
963 let errorResp = eitherDecode (responseBody res)
964 liftIO (errorResp `shouldBe` Right (EsError 404 "IndexMissingException[[bogus] missing]"))
966 it "can parse EsErrors for >= 2.0" $ when' (atleast es20) $ withTestEnv $ do
967 res <- getDocument (IndexName "bogus") (MappingName "also_bogus") (DocId "bogus_as_well")
968 let errorResp = eitherDecode (responseBody res)
969 liftIO (errorResp `shouldBe` Right (EsError 404 "no such index"))
971 describe "document API" $ do
972 it "indexes, updates, gets, and then deletes the generated document" $ withTestEnv $ do
973 _ <- insertData
974 _ <- updateData
975 docInserted <- getDocument testIndex testMapping (DocId "1")
976 let newTweet = eitherDecode
977 (responseBody docInserted) :: Either String (EsResult Tweet)
978 liftIO $ (fmap getSource newTweet `shouldBe` Right (Just patchedTweet))
980 it "indexes, gets, and then deletes the generated document with a DocId containing a space" $ withTestEnv $ do
981 _ <- insertWithSpaceInId
982 docInserted <- getDocument testIndex testMapping (DocId "Hello World")
983 let newTweet = eitherDecode
984 (responseBody docInserted) :: Either String (EsResult Tweet)
985 liftIO $ (fmap getSource newTweet `shouldBe` Right (Just exampleTweet))
987 it "produces a parseable result when looking up a bogus document" $ withTestEnv $ do
988 doc <- getDocument testIndex testMapping (DocId "bogus")
989 let noTweet = eitherDecode
990 (responseBody doc) :: Either String (EsResult Tweet)
991 liftIO $ fmap foundResult noTweet `shouldBe` Right Nothing
993 it "can use optimistic concurrency control" $ withTestEnv $ do
994 let ev = ExternalDocVersion minBound
995 let cfg = defaultIndexDocumentSettings { idsVersionControl = ExternalGT ev }
996 resetIndex
997 res <- insertData' cfg
998 liftIO $ isCreated res `shouldBe` True
999 res' <- insertData' cfg
1000 liftIO $ isVersionConflict res' `shouldBe` True
1002 it "indexes two documents in a parent/child relationship and checks that the child exists" $ withTestEnv $ do
1003 resetIndex
1004 _ <- putMapping testIndex (MappingName "child") ChildMapping
1005 _ <- putMapping testIndex (MappingName "parent") ParentMapping
1006 _ <- indexDocument testIndex (MappingName "parent") defaultIndexDocumentSettings exampleTweet (DocId "1")
1007 let parent = (Just . DocumentParent . DocId) "1"
1008 ids = IndexDocumentSettings NoVersionControl parent
1009 _ <- indexDocument testIndex (MappingName "child") ids otherTweet (DocId "2")
1010 _ <- refreshIndex testIndex
1011 exists <- documentExists testIndex (MappingName "child") parent (DocId "2")
1012 liftIO $ exists `shouldBe` True
1014 describe "template API" $ do
1015 it "can create a template" $ withTestEnv $ do
1016 let idxTpl = IndexTemplate (TemplatePattern "tweet-*") (Just (IndexSettings (ShardCount 1) (ReplicaCount 1))) [toJSON TweetMapping]
1017 resp <- putTemplate idxTpl (TemplateName "tweet-tpl")
1018 liftIO $ validateStatus resp 200
1020 it "can detect if a template exists" $ withTestEnv $ do
1021 exists <- templateExists (TemplateName "tweet-tpl")
1022 liftIO $ exists `shouldBe` True
1024 it "can delete a template" $ withTestEnv $ do
1025 resp <- deleteTemplate (TemplateName "tweet-tpl")
1026 liftIO $ validateStatus resp 200
1028 it "can detect if a template doesn't exist" $ withTestEnv $ do
1029 exists <- templateExists (TemplateName "tweet-tpl")
1030 liftIO $ exists `shouldBe` False
1032 describe "bulk API" $ do
1033 it "inserts all documents we request" $ withTestEnv $ do
1034 _ <- insertData
1035 let firstTest = BulkTest "blah"
1036 let secondTest = BulkTest "bloo"
1037 let thirdTest = BulkTest "graffle"
1038 let firstDoc = BulkIndex testIndex
1039 testMapping (DocId "2") (toJSON firstTest)
1040 let secondDoc = BulkCreate testIndex
1041 testMapping (DocId "3") (toJSON secondTest)
1042 let thirdDoc = BulkCreateEncoding testIndex
1043 testMapping (DocId "4") (toEncoding thirdTest)
1044 let stream = V.fromList [firstDoc, secondDoc, thirdDoc]
1045 _ <- bulk stream
1046 _ <- refreshIndex testIndex
1047 fDoc <- getDocument testIndex testMapping (DocId "2")
1048 sDoc <- getDocument testIndex testMapping (DocId "3")
1049 tDoc <- getDocument testIndex testMapping (DocId "4")
1050 let maybeFirst = eitherDecode $ responseBody fDoc :: Either String (EsResult BulkTest)
1051 let maybeSecond = eitherDecode $ responseBody sDoc :: Either String (EsResult BulkTest)
1052 let maybeThird = eitherDecode $ responseBody tDoc :: Either String (EsResult BulkTest)
1053 liftIO $ do
1054 fmap getSource maybeFirst `shouldBe` Right (Just firstTest)
1055 fmap getSource maybeSecond `shouldBe` Right (Just secondTest)
1056 fmap getSource maybeThird `shouldBe` Right (Just thirdTest)
1059 describe "query API" $ do
1060 it "returns document for term query and identity filter" $ withTestEnv $ do
1061 _ <- insertData
1062 let query = TermQuery (Term "user" "bitemyapp") Nothing
1063 let filter = Filter $ MatchAllQuery Nothing
1064 let search = mkSearch (Just query) (Just filter)
1065 myTweet <- searchTweet search
1066 liftIO $
1067 myTweet `shouldBe` Right exampleTweet
1069 it "handles constant score queries" $ withTestEnv $ do
1070 _ <- insertData
1071 let query = TermsQuery "user" ("bitemyapp" :| [])
1072 let cfQuery = ConstantScoreQuery query (Boost 1.0)
1073 let filter = Filter $ MatchAllQuery Nothing
1074 let search = mkSearch (Just cfQuery) (Just filter)
1075 myTweet <- searchTweet search
1076 liftIO $
1077 myTweet `shouldBe` Right exampleTweet
1079 it "returns document for terms query and identity filter" $ withTestEnv $ do
1080 _ <- insertData
1081 let query = TermsQuery "user" ("bitemyapp" :| [])
1082 let filter = Filter $ MatchAllQuery Nothing
1083 let search = mkSearch (Just query) (Just filter)
1084 myTweet <- searchTweet search
1085 liftIO $
1086 myTweet `shouldBe` Right exampleTweet
1088 it "returns document for match query" $ withTestEnv $ do
1089 _ <- insertData
1090 let query = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
1091 let search = mkSearch (Just query) Nothing
1092 myTweet <- searchTweet search
1093 liftIO $
1094 myTweet `shouldBe` Right exampleTweet
1096 it "returns document for multi-match query" $ withTestEnv $ do
1097 _ <- insertData
1098 let flds = [FieldName "user", FieldName "message"]
1099 let query = QueryMultiMatchQuery $ mkMultiMatchQuery flds (QueryString "bitemyapp")
1100 let search = mkSearch (Just query) Nothing
1101 myTweet <- searchTweet search
1102 liftIO $
1103 myTweet `shouldBe` Right exampleTweet
1105 it "returns document for multi-match query with a custom tiebreaker" $ withTestEnv $ do
1106 _ <- insertData
1107 let tiebreaker = Just $ Tiebreaker 0.3
1108 flds = [FieldName "user", FieldName "message"]
1109 multiQuery' = mkMultiMatchQuery flds (QueryString "bitemyapp")
1110 query = QueryMultiMatchQuery $ multiQuery' { multiMatchQueryTiebreaker = tiebreaker }
1111 search = mkSearch (Just query) Nothing
1112 myTweet <- searchTweet search
1113 liftIO $
1114 myTweet `shouldBe` Right exampleTweet
1116 it "returns document for bool query" $ withTestEnv $ do
1117 _ <- insertData
1118 let innerQuery = QueryMatchQuery $
1119 mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
1120 let query = QueryBoolQuery $
1121 mkBoolQuery [innerQuery] [] [] []
1122 let search = mkSearch (Just query) Nothing
1123 myTweet <- searchTweet search
1124 liftIO $
1125 myTweet `shouldBe` Right exampleTweet
1127 it "returns document for boosting query" $ withTestEnv $ do
1128 _ <- insertData
1129 let posQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
1130 let negQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "notmyapp")
1131 let query = QueryBoostingQuery $ BoostingQuery posQuery negQuery (Boost 0.2)
1132 let search = mkSearch (Just query) Nothing
1133 myTweet <- searchTweet search
1134 liftIO $
1135 myTweet `shouldBe` Right exampleTweet
1137 it "returns document for common terms query" $ withTestEnv $ do
1138 _ <- insertData
1139 let query = QueryCommonTermsQuery $
1140 CommonTermsQuery (FieldName "user")
1141 (QueryString "bitemyapp")
1142 (CutoffFrequency 0.0001)
1143 Or Or Nothing Nothing Nothing Nothing
1144 let search = mkSearch (Just query) Nothing
1145 myTweet <- searchTweet search
1146 liftIO $
1147 myTweet `shouldBe` Right exampleTweet
1149 it "returns document for for inline template query" $ withTestEnv $ do
1150 _ <- insertData
1151 let innerQuery = QueryMatchQuery $
1152 mkMatchQuery (FieldName "{{userKey}}")
1153 (QueryString "{{bitemyappKey}}")
1154 templateParams = TemplateQueryKeyValuePairs $ HM.fromList
1155 [ ("userKey", "user")
1156 , ("bitemyappKey", "bitemyapp")
1158 templateQuery = QueryTemplateQueryInline $
1159 TemplateQueryInline innerQuery templateParams
1160 search = mkSearch (Just templateQuery) Nothing
1161 myTweet <- searchTweet search
1162 liftIO $ myTweet `shouldBe` Right exampleTweet
1165 describe "sorting" $ do
1166 it "returns documents in the right order" $ withTestEnv $ do
1167 _ <- insertData
1168 _ <- insertOther
1169 let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
1170 let search = Search Nothing
1171 Nothing (Just [sortSpec]) Nothing Nothing
1172 False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
1173 Nothing
1174 result <- searchTweets search
1175 let myTweet = grabFirst result
1176 liftIO $
1177 myTweet `shouldBe` Right otherTweet
1179 describe "Aggregation API" $ do
1180 it "returns term aggregation results" $ withTestEnv $ do
1181 _ <- insertData
1182 let terms = TermsAgg $ mkTermsAggregation "user"
1183 let search = mkAggregateSearch Nothing $ mkAggregations "users" terms
1184 searchExpectAggs search
1185 searchValidBucketAgg search "users" toTerms
1187 it "return sub-aggregation results" $ withTestEnv $ do
1188 _ <- insertData
1189 let subaggs = mkAggregations "age_agg" . TermsAgg $ mkTermsAggregation "age"
1190 agg = TermsAgg $ (mkTermsAggregation "user") { termAggs = Just subaggs}
1191 search = mkAggregateSearch Nothing $ mkAggregations "users" agg
1192 reply <- searchByIndex testIndex search
1193 let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
1194 usersAggResults = result >>= aggregations >>= toTerms "users"
1195 subAggResults = usersAggResults >>= (listToMaybe . buckets) >>= termsAggs >>= toTerms "age_agg"
1196 subAddResultsExists = isJust subAggResults
1197 liftIO $ (subAddResultsExists) `shouldBe` True
1199 it "returns cardinality aggregation results" $ withTestEnv $ do
1200 _ <- insertData
1201 let cardinality = CardinalityAgg $ mkCardinalityAggregation $ FieldName "user"
1202 let search = mkAggregateSearch Nothing $ mkAggregations "users" cardinality
1203 let search' = search { Database.V5.Bloodhound.from = From 0, size = Size 0 }
1204 searchExpectAggs search'
1205 let docCountPair k n = (k, object ["value" .= Number n])
1206 res <- searchTweets search'
1207 liftIO $
1208 fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
1210 it "returns stats aggregation results" $ withTestEnv $ do
1211 _ <- insertData
1212 let stats = StatsAgg $ mkStatsAggregation $ FieldName "age"
1213 let search = mkAggregateSearch Nothing $ mkAggregations "users" stats
1214 let search' = search { Database.V5.Bloodhound.from = From 0, size = Size 0 }
1215 searchExpectAggs search'
1216 let statsAggRes k n = (k, object [ "max" .= Number n
1217 , "avg" .= Number n
1218 , "count" .= Number 1
1219 , "min" .= Number n
1220 , "sum" .= Number n])
1221 res <- searchTweets search'
1222 liftIO $
1223 fmap aggregations res `shouldBe` Right (Just (M.fromList [ statsAggRes "users" 10000]))
1225 it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
1226 _ <- insertData
1227 let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }
1228 let search = mkAggregateSearch Nothing $ mkAggregations "users" terms
1229 searchExpectAggs search
1230 searchValidBucketAgg search "users" toTerms
1232 -- One of these fails with 1.7.3
1233 it "can give execution hint parameters to term aggregations" $ when' (atmost es11) $ withTestEnv $ do
1234 _ <- insertData
1235 searchTermsAggHint [Map, Ordinals]
1237 it "can give execution hint parameters to term aggregations" $ when' (is es12) $ withTestEnv $ do
1238 _ <- insertData
1239 searchTermsAggHint [GlobalOrdinals, GlobalOrdinalsHash, GlobalOrdinalsLowCardinality, Map, Ordinals]
1241 it "can give execution hint parameters to term aggregations" $ when' (atleast es12) $ withTestEnv $ do
1242 _ <- insertData
1243 searchTermsAggHint [GlobalOrdinals, GlobalOrdinalsHash, GlobalOrdinalsLowCardinality, Map]
1244 -- One of the above.
1246 it "can execute value_count aggregations" $ withTestEnv $ do
1247 _ <- insertData
1248 _ <- insertOther
1249 let ags = mkAggregations "user_count" (ValueCountAgg (FieldValueCount (FieldName "user"))) <>
1250 mkAggregations "bogus_count" (ValueCountAgg (FieldValueCount (FieldName "bogus")))
1251 let search = mkAggregateSearch Nothing ags
1252 let docCountPair k n = (k, object ["value" .= Number n])
1253 res <- searchTweets search
1254 liftIO $
1255 fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "user_count" 2
1256 , docCountPair "bogus_count" 0
1257 ]))
1259 it "can execute date_range aggregations" $ withTestEnv $ do
1260 let now = fromGregorian 2015 3 14
1261 let ltAMonthAgo = UTCTime (fromGregorian 2015 3 1) 0
1262 let ltAWeekAgo = UTCTime (fromGregorian 2015 3 10) 0
1263 let oldDoc = exampleTweet { postDate = ltAMonthAgo }
1264 let newDoc = exampleTweet { postDate = ltAWeekAgo }
1265 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings oldDoc (DocId "1")
1266 _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings newDoc (DocId "2")
1267 _ <- refreshIndex testIndex
1268 let thisMonth = DateRangeFrom (DateMathExpr (DMDate now) [SubtractTime 1 DMMonth])
1269 let thisWeek = DateRangeFrom (DateMathExpr (DMDate now) [SubtractTime 1 DMWeek])
1270 let agg = DateRangeAggregation (FieldName "postDate") Nothing (thisMonth :| [thisWeek])
1271 let ags = mkAggregations "date_ranges" (DateRangeAgg agg)
1272 let search = mkAggregateSearch Nothing ags
1273 res <- searchTweets search
1274 liftIO $ hitsTotal . searchHits <$> res `shouldBe` Right 2
1275 let bucks = do magrs <- fmapL show (aggregations <$> res)
1276 agrs <- note "no aggregations returned" magrs
1277 rawBucks <- note "no date_ranges aggregation" $ M.lookup "date_ranges" agrs
1278 parseEither parseJSON rawBucks
1279 let fromMonthT = UTCTime (fromGregorian 2015 2 14) 0
1280 let fromWeekT = UTCTime (fromGregorian 2015 3 7) 0
1281 liftIO $ buckets <$> bucks `shouldBe` Right [ DateRangeResult "2015-02-14T00:00:00.000Z-*"
1282 (Just fromMonthT)
1283 (Just "2015-02-14T00:00:00.000Z")
1284 Nothing
1285 Nothing
1287 Nothing
1288 , DateRangeResult "2015-03-07T00:00:00.000Z-*"
1289 (Just fromWeekT)
1290 (Just "2015-03-07T00:00:00.000Z")
1291 Nothing
1292 Nothing
1294 Nothing
1297 it "returns date histogram aggregation results" $ withTestEnv $ do
1298 _ <- insertData
1299 let histogram = DateHistogramAgg $ mkDateHistogram (FieldName "postDate") Minute
1300 let search = mkAggregateSearch Nothing (mkAggregations "byDate" histogram)
1301 searchExpectAggs search
1302 searchValidBucketAgg search "byDate" toDateHistogram
1304 it "can execute missing aggregations" $ withTestEnv $ do
1305 _ <- insertData
1306 _ <- insertExtra
1307 let ags = mkAggregations "missing_agg" (MissingAgg (MissingAggregation "extra"))
1308 let search = mkAggregateSearch Nothing ags
1309 let docCountPair k n = (k, object ["doc_count" .= Number n])
1310 res <- searchTweets search
1311 liftIO $
1312 fmap aggregations res `shouldBe` Right (Just (M.fromList [docCountPair "missing_agg" 1]))
1314 describe "Highlights API" $ do
1316 it "returns highlight from query when there should be one" $ withTestEnv $ do
1317 _ <- insertData
1318 _ <- insertOther
1319 let query = QueryMatchQuery $ mkMatchQuery (FieldName "message") (QueryString "haskell")
1320 let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
1322 let search = mkHighlightSearch (Just query) testHighlight
1323 myHighlight <- searchTweetHighlight search
1324 liftIO $
1325 myHighlight `shouldBe` Right (Just (M.fromList [("message",["Use <em>haskell</em>!"])]))
1327 it "doesn't return highlight from a query when it shouldn't" $ withTestEnv $ do
1328 _ <- insertData
1329 _ <- insertOther
1330 let query = QueryMatchQuery $ mkMatchQuery (FieldName "message") (QueryString "haskell")
1331 let testHighlight = Highlights Nothing [FieldHighlight (FieldName "user") Nothing]
1333 let search = mkHighlightSearch (Just query) testHighlight
1334 myHighlight <- searchTweetHighlight search
1335 liftIO $
1336 myHighlight `shouldBe` Right Nothing
1338 describe "Source filtering" $ do
1340 it "doesn't include source when sources are disabled" $ withTestEnv $ do
1341 searchExpectSource
1342 NoSource
1343 (Left (EsError 500 "Source was missing"))
1345 it "includes a source" $ withTestEnv $ do
1346 searchExpectSource
1347 (SourcePatterns (PopPattern (Pattern "message")))
1348 (Right (Object (HM.fromList [("message", String "Use haskell!")])))
1350 it "includes sources" $ withTestEnv $ do
1351 searchExpectSource
1352 (SourcePatterns (PopPatterns [Pattern "user", Pattern "message"]))
1353 (Right (Object (HM.fromList [("user",String "bitemyapp"),("message", String "Use haskell!")])))
1355 it "includes source patterns" $ withTestEnv $ do
1356 searchExpectSource
1357 (SourcePatterns (PopPattern (Pattern "*ge")))
1358 (Right (Object (HM.fromList [("age", Number 10000),("message", String "Use haskell!")])))
1360 it "excludes source patterns" $ withTestEnv $ do
1361 searchExpectSource
1362 (SourceIncludeExclude (Include [])
1363 (Exclude [Pattern "l*", Pattern "*ge", Pattern "postDate", Pattern "extra"]))
1364 (Right (Object (HM.fromList [("user",String "bitemyapp")])))
1366 describe "ToJSON RegexpFlags" $ do
1367 it "generates the correct JSON for AllRegexpFlags" $
1368 toJSON AllRegexpFlags `shouldBe` String "ALL"
1370 it "generates the correct JSON for NoRegexpFlags" $
1371 toJSON NoRegexpFlags `shouldBe` String "NONE"
1373 it "generates the correct JSON for SomeRegexpFlags" $
1374 let flags = AnyString :| [ Automaton
1375 , Complement
1376 , Empty
1377 , Intersection
1378 , Interval ]
1379 in toJSON (SomeRegexpFlags flags) `shouldBe` String "ANYSTRING|AUTOMATON|COMPLEMENT|EMPTY|INTERSECTION|INTERVAL"
1381 prop "removes duplicates from flags" $ \(flags :: RegexpFlags) ->
1382 let String str = toJSON flags
1383 flagStrs = T.splitOn "|" str
1384 in noDuplicates flagStrs
1386 describe "omitNulls" $ do
1387 it "checks that omitNulls drops list elements when it should" $
1388 let dropped = omitNulls $ [ "test1" .= (toJSON ([] :: [Int]))
1389 , "test2" .= (toJSON ("some value" :: Text))]
1390 in dropped `shouldBe` Object (HM.fromList [("test2", String "some value")])
1392 it "checks that omitNulls doesn't drop list elements when it shouldn't" $
1393 let notDropped = omitNulls $ [ "test1" .= (toJSON ([1] :: [Int]))
1394 , "test2" .= (toJSON ("some value" :: Text))]
1395 in notDropped `shouldBe` Object (HM.fromList [ ("test1", Array (V.fromList [Number 1.0]))
1396 , ("test2", String "some value")])
1397 it "checks that omitNulls drops non list elements when it should" $
1398 let dropped = omitNulls $ [ "test1" .= (toJSON Null)
1399 , "test2" .= (toJSON ("some value" :: Text))]
1400 in dropped `shouldBe` Object (HM.fromList [("test2", String "some value")])
1401 it "checks that omitNulls doesn't drop non list elements when it shouldn't" $
1402 let notDropped = omitNulls $ [ "test1" .= (toJSON (1 :: Int))
1403 , "test2" .= (toJSON ("some value" :: Text))]
1404 in notDropped `shouldBe` Object (HM.fromList [ ("test1", Number 1.0)
1405 , ("test2", String "some value")])
1406 describe "Monoid (SearchHits a)" $ do
1407 prop "abides the monoid laws" $ eq $
1408 prop_Monoid (T :: T (SearchHits ()))
1410 describe "mkDocVersion" $ do
1411 prop "can never construct an out of range docVersion" $ \i ->
1412 let res = mkDocVersion i
1413 in case res of
1414 Nothing -> property True
1415 Just dv -> (dv >= minBound) .&&.
1416 (dv <= maxBound) .&&.
1417 docVersionNumber dv === i
1419 describe "FsSnapshotRepo" $ do
1420 prop "SnapshotRepo laws" $ \fsr ->
1421 fromGSnapshotRepo (toGSnapshotRepo fsr) === Right (fsr :: FsSnapshotRepo)
1423 describe "snapshot repos" $ do
1424 it "always parses all snapshot repos API" $ when' canSnapshot $ withTestEnv $ do
1425 res <- getSnapshotRepos AllSnapshotRepos
1426 liftIO $ case res of
1427 Left e -> expectationFailure ("Expected a right but got Left " <> show e)
1428 Right _ -> return ()
1430 it "finds an existing list of repos" $ when' canSnapshot $ withTestEnv $ do
1431 let r1n = SnapshotRepoName "bloodhound-repo1"
1432 let r2n = SnapshotRepoName "bloodhound-repo2"
1433 withSnapshotRepo r1n $ \r1 ->
1434 withSnapshotRepo r2n $ \r2 -> do
1435 repos <- getSnapshotRepos (SnapshotRepoList (ExactRepo r1n :| [ExactRepo r2n]))
1436 liftIO $ case repos of
1437 Right xs -> do
1438 let srt = L.sortBy (comparing gSnapshotRepoName)
1439 srt xs `shouldBe` srt [r1, r2]
1440 Left e -> expectationFailure (show e)
1442 it "creates and updates with updateSnapshotRepo" $ when' canSnapshot $ withTestEnv $ do
1443 let r1n = SnapshotRepoName "bloodhound-repo1"
1444 withSnapshotRepo r1n $ \r1 -> do
1445 let Just (String dir) = HM.lookup "location" (gSnapshotRepoSettingsObject (gSnapshotRepoSettings r1))
1446 let noCompression = FsSnapshotRepo r1n (T.unpack dir) False Nothing Nothing Nothing
1447 resp <- updateSnapshotRepo defaultSnapshotRepoUpdateSettings noCompression
1448 liftIO (validateStatus resp 200)
1449 Right [roundtrippedNoCompression] <- getSnapshotRepos (SnapshotRepoList (ExactRepo r1n :| []))
1450 liftIO (roundtrippedNoCompression `shouldBe` toGSnapshotRepo noCompression)
1452 -- verify came around in 1.4 it seems
1453 it "can verify existing repos" $ when' canSnapshot $ when' (atleast es14) $ withTestEnv $ do
1454 let r1n = SnapshotRepoName "bloodhound-repo1"
1455 withSnapshotRepo r1n $ \_ -> do
1456 res <- verifySnapshotRepo r1n
1457 liftIO $ case res of
1458 Right (SnapshotVerification vs)
1459 | null vs -> expectationFailure "Expected nonempty set of verifying nodes"
1460 | otherwise -> return ()
1461 Left e -> expectationFailure (show e)
1463 describe "snapshots" $ do
1464 it "always parses all snapshots API" $ when' canSnapshot $ withTestEnv $ do
1465 let r1n = SnapshotRepoName "bloodhound-repo1"
1466 withSnapshotRepo r1n $ \_ -> do
1467 res <- getSnapshots r1n AllSnapshots
1468 liftIO $ case res of
1469 Left e -> expectationFailure ("Expected a right but got Left " <> show e)
1470 Right _ -> return ()
1472 it "can parse a snapshot that it created" $ when' canSnapshot $ withTestEnv $ do
1473 let r1n = SnapshotRepoName "bloodhound-repo1"
1474 withSnapshotRepo r1n $ \_ -> do
1475 let s1n = SnapshotName "example-snapshot"
1476 withSnapshot r1n s1n $ do
1477 res <- getSnapshots r1n (SnapshotList (ExactSnap s1n :| []))
1478 liftIO $ case res of
1479 Right [snap]
1480 | snapInfoState snap == SnapshotSuccess &&
1481 snapInfoName snap == s1n -> return ()
1482 | otherwise -> expectationFailure (show snap)
1483 Right [] -> expectationFailure "There were no snapshots"
1484 Right snaps -> expectationFailure ("Expected 1 snapshot but got" <> show (length snaps))
1485 Left e -> expectationFailure (show e)
1487 describe "snapshot restore" $ do
1488 it "can restore a snapshot that we create" $ when' canSnapshot $ withTestEnv $ do
1489 let r1n = SnapshotRepoName "bloodhound-repo1"
1490 withSnapshotRepo r1n $ \_ -> do
1491 let s1n = SnapshotName "example-snapshot"
1492 withSnapshot r1n s1n $ do
1493 let settings = defaultSnapshotRestoreSettings { snapRestoreWaitForCompletion = True }
1494 -- have to close an index to restore it
1495 resp1 <- closeIndex testIndex
1496 liftIO (validateStatus resp1 200)
1497 resp2 <- restoreSnapshot r1n s1n settings
1498 liftIO (validateStatus resp2 200)
1500 it "can restore and rename" $ when' canSnapshot $ withTestEnv $ do
1501 let r1n = SnapshotRepoName "bloodhound-repo1"
1502 withSnapshotRepo r1n $ \_ -> do
1503 let s1n = SnapshotName "example-snapshot"
1504 withSnapshot r1n s1n $ do
1505 let pat = RestoreRenamePattern "bloodhound-tests-twitter-(\\d+)"
1506 let replace = RRTLit "restored-" :| [RRSubWholeMatch]
1507 let expectedIndex = IndexName "restored-bloodhound-tests-twitter-1"
1508 oldEnoughForOverrides <- liftIO (atleast es15)
1509 let overrides = RestoreIndexSettings { restoreOverrideReplicas = Just (ReplicaCount 0) }
1510 let settings = defaultSnapshotRestoreSettings { snapRestoreWaitForCompletion = True
1511 , snapRestoreRenamePattern = Just pat
1512 , snapRestoreRenameReplacement = Just replace
1513 , snapRestoreIndexSettingsOverrides = if oldEnoughForOverrides
1514 then Just overrides
1515 else Nothing
1517 -- have to close an index to restore it
1518 let go = do
1519 resp <- restoreSnapshot r1n s1n settings
1520 liftIO (validateStatus resp 200)
1521 exists <- indexExists expectedIndex
1522 liftIO (exists `shouldBe` True)
1523 go `finally` deleteIndex expectedIndex
1525 describe "getNodesInfo" $ do
1526 it "fetches the responding node when LocalNode is used" $ withTestEnv $ do
1527 res <- getNodesInfo LocalNode
1528 liftIO $ case res of
1529 -- This is really just a smoke test for response
1530 -- parsing. Node info is so variable, there's not much I can
1531 -- assert here.
1532 Right NodesInfo {..} -> length nodesInfo `shouldBe` 1
1533 Left e -> expectationFailure ("Expected NodesInfo but got " <> show e)
1535 describe "getNodesStats" $ do
1536 it "fetches the responding node when LocalNode is used" $ withTestEnv $ do
1537 res <- getNodesStats LocalNode
1538 liftIO $ case res of
1539 -- This is really just a smoke test for response
1540 -- parsing. Node stats is so variable, there's not much I can
1541 -- assert here.
1542 Right NodesStats {..} -> length nodesStats `shouldBe` 1
1543 Left e -> expectationFailure ("Expected NodesStats but got " <> show e)
1545 describe "Enum DocVersion" $ do
1546 it "follows the laws of Enum, Bounded" $ do
1547 evaluate (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall
1548 evaluate (pred minBound :: DocVersion) `shouldThrow` anyErrorCall
1549 evaluate (toEnum 0 :: DocVersion) `shouldThrow` anyErrorCall
1550 evaluate (toEnum 9200000000000000001 :: DocVersion) `shouldThrow` anyErrorCall
1551 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
1552 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
1553 enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound]
1555 describe "scan&scroll API" $ do
1556 it "returns documents using the scan&scroll API" $ withTestEnv $ do
1557 _ <- insertData
1558 _ <- insertOther
1559 let search = (mkSearch (Just $ MatchAllQuery Nothing) Nothing) { size = (Size 1) }
1560 regular_search <- searchTweet search
1561 scan_search' <- scanSearch testIndex testMapping search :: BH IO [Hit Tweet]
1562 let scan_search = map hitSource scan_search'
1563 liftIO $
1564 regular_search `shouldBe` Right exampleTweet -- Check that the size restrtiction is being honored
1565 liftIO $
1566 scan_search `shouldMatchList` [Just exampleTweet, Just otherTweet]
1568 describe "index aliases" $ do
1569 let aname = IndexAliasName (IndexName "bloodhound-tests-twitter-1-alias")
1570 let alias = IndexAlias (testIndex) aname
1571 let create = IndexAliasCreate Nothing Nothing
1572 let action = AddAlias alias create
1573 it "handles the simple case of aliasing an existing index" $ do
1574 withTestEnv $ do
1575 resetIndex
1576 resp <- updateIndexAliases (action :| [])
1577 liftIO $ validateStatus resp 200
1578 let cleanup = withTestEnv (updateIndexAliases (RemoveAlias alias :| []))
1579 (do aliases <- withTestEnv getIndexAliases
1580 let expected = IndexAliasSummary alias create
1581 case aliases of
1582 Right (IndexAliasesSummary summs) ->
1583 L.find ((== alias) . indexAliasSummaryAlias) summs `shouldBe` Just expected
1584 Left e -> expectationFailure ("Expected an IndexAliasesSummary but got " <> show e)) `finally` cleanup
1585 it "allows alias deletion" $ do
1586 aliases <- withTestEnv $ do
1587 resetIndex
1588 resp <- updateIndexAliases (action :| [])
1589 liftIO $ validateStatus resp 200
1590 deleteIndexAlias aname
1591 getIndexAliases
1592 let expected = IndexAliasSummary alias create
1593 case aliases of
1594 Right (IndexAliasesSummary summs) ->
1595 L.find ((== aname) . indexAlias . indexAliasSummaryAlias) summs `shouldBe` Nothing
1596 Left e -> expectationFailure ("Expected an IndexAliasesSummary but got " <> show e)
1598 describe "Index Listing" $ do
1599 it "returns a list of index names" $ withTestEnv $ do
1600 _ <- createExampleIndex
1601 ixns <- listIndices
1602 liftIO (ixns `shouldContain` [testIndex])
1604 describe "Index Settings" $ do
1605 it "persists settings" $ withTestEnv $ do
1606 _ <- deleteExampleIndex
1607 _ <- createExampleIndex
1608 let updates = BlocksWrite False :| []
1609 updateResp <- updateIndexSettings updates testIndex
1610 liftIO $ validateStatus updateResp 200
1611 getResp <- getIndexSettings testIndex
1612 liftIO $
1613 getResp `shouldBe` Right (IndexSettingsSummary
1614 testIndex
1615 (IndexSettings (ShardCount 1) (ReplicaCount 0))
1616 (NE.toList updates))
1618 it "allows total fields to be set" $ when' (atleast es50) $ withTestEnv $ do
1619 _ <- deleteExampleIndex
1620 _ <- createExampleIndex
1621 let updates = MappingTotalFieldsLimit 2500 :| []
1622 updateResp <- updateIndexSettings updates testIndex
1623 liftIO $ validateStatus updateResp 200
1624 getResp <- getIndexSettings testIndex
1625 liftIO $
1626 getResp `shouldBe` Right (IndexSettingsSummary
1627 testIndex
1628 (IndexSettings (ShardCount 1) (ReplicaCount 0))
1629 (NE.toList updates))
1631 it "accepts customer analyzers" $ when' (atleast es50) $ withTestEnv $ do
1632 _ <- deleteExampleIndex
1633 let analysis = Analysis
1634 (M.singleton "ex_analyzer" (AnalyzerDefinition (Just (Tokenizer "ex_tokenizer"))))
1635 (M.singleton "ex_tokenizer"
1636 ( TokenizerDefinitionNgram
1637 ( Ngram 3 4 [TokenLetter,TokenDigit])
1640 updates = [AnalysisSetting analysis]
1641 createResp <- createIndexWith (updates ++ [NumberOfReplicas (ReplicaCount 0)]) 1 testIndex
1642 liftIO $ validateStatus createResp 200
1643 getResp <- getIndexSettings testIndex
1644 liftIO $
1645 getResp `shouldBe` Right (IndexSettingsSummary
1646 testIndex
1647 (IndexSettings (ShardCount 1) (ReplicaCount 0))
1648 updates
1651 describe "Index Optimization" $ do
1652 it "returns a successful response upon completion" $ withTestEnv $ do
1653 _ <- createExampleIndex
1654 resp <- forceMergeIndex (IndexList (testIndex :| [])) defaultForceMergeIndexSettings
1655 liftIO $ validateStatus resp 200
1657 describe "Suggest" $ do
1658 it "returns a search suggestion using the phrase suggester" $ withTestEnv $ do
1659 _ <- insertData
1660 let query = QueryMatchNoneQuery
1661 phraseSuggester = mkPhraseSuggester (FieldName "message")
1662 namedSuggester = Suggest "Use haskel" "suggest_name" (SuggestTypePhraseSuggester phraseSuggester)
1663 search' = mkSearch (Just query) Nothing
1664 search = search' { suggestBody = Just namedSuggester }
1665 expectedText = Just "use haskell"
1666 resp <- searchByIndex testIndex search
1667 parsed <- parseEsResponse resp :: BH IO (Either EsError (SearchResult Tweet))
1668 case parsed of
1669 Left e -> liftIO $ expectationFailure ("Expected an search suggestion but got " <> show e)
1670 Right sr -> liftIO $ (suggestOptionsText . head . suggestResponseOptions . head . nsrResponses <$> suggest sr) `shouldBe` expectedText
1672 describe "JSON instances" $ do
1673 propJSON (Proxy :: Proxy Version)
1674 propJSON (Proxy :: Proxy IndexName)
1675 propJSON (Proxy :: Proxy MappingName)
1676 propJSON (Proxy :: Proxy DocId)
1677 propJSON (Proxy :: Proxy IndexAliasRouting)
1678 propJSON (Proxy :: Proxy RoutingValue)
1679 propJSON (Proxy :: Proxy ShardCount)
1680 propJSON (Proxy :: Proxy ReplicaCount)
1681 propJSON (Proxy :: Proxy TemplateName)
1682 propJSON (Proxy :: Proxy TemplatePattern)
1683 propJSON (Proxy :: Proxy QueryString)
1684 propJSON (Proxy :: Proxy FieldName)
1685 propJSON (Proxy :: Proxy CacheName)
1686 propJSON (Proxy :: Proxy CacheKey)
1687 propJSON (Proxy :: Proxy Existence)
1688 propJSON (Proxy :: Proxy CutoffFrequency)
1689 propJSON (Proxy :: Proxy Analyzer)
1690 propJSON (Proxy :: Proxy MaxExpansions)
1691 propJSON (Proxy :: Proxy Lenient)
1692 propJSON (Proxy :: Proxy Tiebreaker)
1693 propJSON (Proxy :: Proxy Boost)
1694 propJSON (Proxy :: Proxy BoostTerms)
1695 propJSON (Proxy :: Proxy MinimumMatch)
1696 propJSON (Proxy :: Proxy DisableCoord)
1697 propJSON (Proxy :: Proxy IgnoreTermFrequency)
1698 propJSON (Proxy :: Proxy MinimumTermFrequency)
1699 propJSON (Proxy :: Proxy MaxQueryTerms)
1700 propJSON (Proxy :: Proxy Fuzziness)
1701 propJSON (Proxy :: Proxy PrefixLength)
1702 propJSON (Proxy :: Proxy TypeName)
1703 propJSON (Proxy :: Proxy PercentMatch)
1704 propJSON (Proxy :: Proxy StopWord)
1705 propJSON (Proxy :: Proxy QueryPath)
1706 propJSON (Proxy :: Proxy AllowLeadingWildcard)
1707 propJSON (Proxy :: Proxy LowercaseExpanded)
1708 propJSON (Proxy :: Proxy EnablePositionIncrements)
1709 propJSON (Proxy :: Proxy AnalyzeWildcard)
1710 propJSON (Proxy :: Proxy GeneratePhraseQueries)
1711 propJSON (Proxy :: Proxy Locale)
1712 propJSON (Proxy :: Proxy MaxWordLength)
1713 propJSON (Proxy :: Proxy MinWordLength)
1714 propJSON (Proxy :: Proxy PhraseSlop)
1715 propJSON (Proxy :: Proxy MinDocFrequency)
1716 propJSON (Proxy :: Proxy MaxDocFrequency)
1717 propJSON (Proxy :: Proxy Filter)
1718 propJSON (Proxy :: Proxy Query)
1719 propJSON (Proxy :: Proxy SimpleQueryStringQuery)
1720 propJSON (Proxy :: Proxy FieldOrFields)
1721 propJSON (Proxy :: Proxy SimpleQueryFlag)
1722 propJSON (Proxy :: Proxy RegexpQuery)
1723 propJSON (Proxy :: Proxy QueryStringQuery)
1724 propJSON (Proxy :: Proxy RangeQuery)
1725 propJSON (Proxy :: Proxy PrefixQuery)
1726 propJSON (Proxy :: Proxy NestedQuery)
1727 propJSON (Proxy :: Proxy MoreLikeThisFieldQuery)
1728 propJSON (Proxy :: Proxy MoreLikeThisQuery)
1729 propJSON (Proxy :: Proxy IndicesQuery)
1730 propJSON (Proxy :: Proxy HasParentQuery)
1731 propJSON (Proxy :: Proxy HasChildQuery)
1732 propJSON (Proxy :: Proxy FuzzyQuery)
1733 propJSON (Proxy :: Proxy FuzzyLikeFieldQuery)
1734 propJSON (Proxy :: Proxy FuzzyLikeThisQuery)
1735 propJSON (Proxy :: Proxy DisMaxQuery)
1736 propJSON (Proxy :: Proxy CommonTermsQuery)
1737 propJSON (Proxy :: Proxy CommonMinimumMatch)
1738 propJSON (Proxy :: Proxy BoostingQuery)
1739 propJSON (Proxy :: Proxy BoolQuery)
1740 propJSON (Proxy :: Proxy MatchQuery)
1741 propJSON (Proxy :: Proxy MultiMatchQueryType)
1742 propJSON (Proxy :: Proxy BooleanOperator)
1743 propJSON (Proxy :: Proxy ZeroTermsQuery)
1744 propJSON (Proxy :: Proxy MatchQueryType)
1745 propJSON (Proxy :: Proxy AliasRouting)
1746 propJSON (Proxy :: Proxy IndexAliasCreate)
1747 propJSON (Proxy :: Proxy SearchAliasRouting)
1748 propJSON (Proxy :: Proxy ScoreType)
1749 propJSON (Proxy :: Proxy Distance)
1750 propJSON (Proxy :: Proxy DistanceUnit)
1751 propJSON (Proxy :: Proxy DistanceType)
1752 propJSON (Proxy :: Proxy OptimizeBbox)
1753 propJSON (Proxy :: Proxy GeoBoundingBoxConstraint)
1754 propJSON (Proxy :: Proxy GeoFilterType)
1755 propJSON (Proxy :: Proxy GeoBoundingBox)
1756 propJSON (Proxy :: Proxy LatLon)
1757 propJSON (Proxy :: Proxy RangeExecution)
1758 prop "RegexpFlags FromJSON/ToJSON roundtrips, removing dups " $ \rfs ->
1759 let expected = case rfs of
1760 SomeRegexpFlags fs -> SomeRegexpFlags (NE.fromList (nub (NE.toList fs)))
1761 x -> x
1762 in parseEither parseJSON (toJSON rfs) === Right expected
1763 propJSON (Proxy :: Proxy BoolMatch)
1764 propJSON (Proxy :: Proxy Term)
1765 propJSON (Proxy :: Proxy MultiMatchQuery)
1766 propJSON (Proxy :: Proxy IndexSettings)
1767 propJSON (Proxy :: Proxy UpdatableIndexSetting')
1768 propJSON (Proxy :: Proxy ReplicaBounds)
1769 propJSON (Proxy :: Proxy Bytes)
1770 propJSON (Proxy :: Proxy AllocationPolicy)
1771 propJSON (Proxy :: Proxy InitialShardCount)
1772 propJSON (Proxy :: Proxy FSType)
1773 propJSON (Proxy :: Proxy CompoundFormat)
1774 propJSON (Proxy :: Proxy TemplateQueryInline)
1775 propJSON (Proxy :: Proxy Suggest)
1777-- Temporary solution for lacking of generic derivation of Arbitrary
1778-- We use generics-sop, as it's much more concise than directly using GHC.Generics
1780-- This will be unneeded after https://github.com/nick8325/quickcheck/pull/112
1781-- is merged and released
1782sopArbitrary :: forall a. (Generic a, SOP.GTo a, SOP.All SOP.SListI (SOP.GCode a), SOP.All2 Arbitrary (SOP.GCode a)) => Gen a
1783sopArbitrary = fmap SOP.gto sopArbitrary'
1785sopArbitrary' :: forall xss. (SOP.All SOP.SListI xss, SOP.All2 Arbitrary xss) => Gen (SOP.SOP SOP.I xss)
1786sopArbitrary' = SOP.hsequence =<< elements (SOP.apInjs_POP $ SOP.hcpure p arbitrary)
1787 where
1788 p :: Proxy Arbitrary
1789 p = Proxy