git.iko.soy/typeable/bloodhound
on master help sign in
Fixed bulk tests
dd63d9cChris Allen
src/Database/V5/Bloodhound/Client.hs+10−4
@@ -215,6 +215,7 @@ dispatch dMethod url body = do
215 $ setRequestIgnoreStatus
215 $ setRequestIgnoreStatus
216 $ initReq { method = dMethod
216 $ initReq { method = dMethod
217 , requestHeaders =
217 , requestHeaders =
218+ -- "application/x-ndjson" for bulk
218 ("Content-Type", "application/json") : requestHeaders initReq
219 ("Content-Type", "application/json") : requestHeaders initReq
219 , requestBody = reqBody }
220 , requestBody = reqBody }
220 -- req <- liftIO $ reqHook $ setRequestIgnoreStatus $ initReq { method = dMethod
221 -- req <- liftIO $ reqHook $ setRequestIgnoreStatus $ initReq { method = dMethod
@@ -856,7 +857,9 @@ deleteDocument (IndexName indexName)
856-- >>> _ <- runBH' $ bulk stream
857-- >>> _ <- runBH' $ bulk stream
857-- >>> _ <- runBH' $ refreshIndex testIndex
858-- >>> _ <- runBH' $ refreshIndex testIndex
858bulk :: MonadBH m => V.Vector BulkOperation -> m Reply
859bulk :: MonadBH m => V.Vector BulkOperation -> m Reply
860+bulk bulkOps = do
861+ liftIO $ print body
859−bulk bulkOps = bindM2 post url (return body)
862+ bindM2 post url (return body)
860 where url = joinPath ["_bulk"]
863 where url = joinPath ["_bulk"]
861 body = Just $ encodeBulkOperations bulkOps
864 body = Just $ encodeBulkOperations bulkOps
862
865
@@ -868,9 +871,12 @@ bulk bulkOps = bindM2 post url (return body)
868-- "\n{\"index\":{\"_type\":\"tweet\",\"_id\":\"2\",\"_index\":\"twitter\"}}\n{\"name\":\"blah\"}\n"
871-- "\n{\"index\":{\"_type\":\"tweet\",\"_id\":\"2\",\"_index\":\"twitter\"}}\n{\"name\":\"blah\"}\n"
869encodeBulkOperations :: V.Vector BulkOperation -> L.ByteString
872encodeBulkOperations :: V.Vector BulkOperation -> L.ByteString
870encodeBulkOperations stream = collapsed where
873encodeBulkOperations stream = collapsed where
874+ blobs =
871− blobs = fmap encodeBulkOperation stream
875+ fmap encodeBulkOperation stream
876+ mashedTaters =
872− mashedTaters = mash (mempty :: Builder) blobs
877+ mash (mempty :: Builder) blobs
878+ collapsed =
873− collapsed = toLazyByteString $ mappend mashedTaters (byteString "\n")
879+ toLazyByteString $ mappend mashedTaters (byteString "\n")
874
880
875mash :: Builder -> V.Vector L.ByteString -> Builder
881mash :: Builder -> V.Vector L.ByteString -> Builder
876mash = V.foldl' (\b x -> b `mappend` (byteString "\n") `mappend` (lazyByteString x))
882mash = V.foldl' (\b x -> b `mappend` (byteString "\n") `mappend` (lazyByteString x))
tests/V5/tests.hs+12−1
@@ -435,7 +435,18 @@ withSnapshot srn sn = bracket_ alloc free
435
435
436newtype BulkTest =
436newtype BulkTest =
437 BulkTest { name :: Text }
437 BulkTest { name :: Text }
438− deriving (Eq, Show, FromJSON, ToJSON)
438+ deriving (Eq, Show)
439+
440+instance ToJSON BulkTest where
441+ toJSON (BulkTest name) =
442+ object ["name" .= name]
443+
444+instance FromJSON BulkTest where
445+ parseJSON = withObject "BulkTest" parse
446+ where
447+ parse o = do
448+ t <- o .: "name"
449+ BulkTest <$> parseJSON t
439
450
440-- | Typeclass for "equal where it matters". Use this to specify
451-- | Typeclass for "equal where it matters". Use this to specify
441-- less-strict equivalence for things such as lists that can wind up
452-- less-strict equivalence for things such as lists that can wind up