@@ -15,6 +15,7 @@ module Database.Bloodhound.Client
1515 , searchByType
1616 , refreshIndex
1717 , mkSearch
18+ , bulk
1819 , IndexSettings(..)
1920 , Server(..)
2021 , Reply(..)
@@ -54,6 +55,7 @@ module Database.Bloodhound.Client
5455 , DocId(..)
5556 , CacheName(..)
5657 , CacheKey(..)
58+ , BulkOperation(..)
5759 )
5860 where
5961
@@ -63,7 +65,7 @@ import Data.Aeson
6365import Data.Aeson.TH (deriveJSON)
6466import qualified Data.ByteString.Lazy.Char8 as L
6567import Data.ByteString.Builder
66−import Data.List (intercalate)
68+import Data.List (foldl', intercalate, intersperse)
6769import Data.Maybe (fromMaybe)
6870import Data.Monoid
6971import Data.Text (Text)
@@ -267,8 +269,16 @@ bulk :: Server -> [BulkOperation] -> IO Reply
267269bulk (Server server) bulkOps = dispatch url method body where
268270 url = joinPath [server, "_bulk"]
269271 method = NHTM.methodPost
270− blobs = concat $ fmap getStreamChunk bulkOps
271− body = Just $ toLazyByteString $ mash (mempty :: Builder) blobs
272+ body = Just $ collapseStream bulkOps
273+
274+collapseStream :: [BulkOperation] -> L.ByteString
275+collapseStream stream = collapsed where
276+ blobs = intersperse "\n" $ concat $ fmap getStreamChunk stream
277+ mashedTaters = mash (mempty :: Builder) blobs
278+ collapsed = toLazyByteString $ mappend mashedTaters "\n"
279+
280+mash :: Builder -> [L.ByteString] -> Builder
281+mash builder xs = foldl' (\b x -> mappend b (lazyByteString x)) builder xs
272282
273283data BulkOperation =
274284 BulkIndex IndexName MappingName DocId Value
@@ -276,9 +286,6 @@ data BulkOperation =
276286 | BulkDelete IndexName MappingName DocId
277287 | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Show)
278288
279−mash :: Builder -> [L.ByteString] -> Builder
280−mash builder xs = foldr (\x b -> mappend b (lazyByteString x)) builder xs
281−
282289mkMetadataValue :: Text -> String -> String -> String -> Value
283290mkMetadataValue operation indexName mappingName docId =
284291 object [operation .=