@@ -15,6 +15,7 @@ module Database.Bloodhound.Client
15 , searchByType
15 , searchByType
16 , refreshIndex
16 , refreshIndex
17 , mkSearch
17 , mkSearch
18 , IndexSettings(..)
19 , IndexSettings(..)
19 , Server(..)
20 , Server(..)
20 , Reply(..)
21 , Reply(..)
@@ -54,6 +55,7 @@ module Database.Bloodhound.Client
54 , DocId(..)
55 , DocId(..)
55 , CacheName(..)
56 , CacheName(..)
56 , CacheKey(..)
57 , CacheKey(..)
@@ -63,7 +65,7 @@ import Data.Aeson
63import Data.Aeson.TH (deriveJSON)
65import Data.Aeson.TH (deriveJSON)
64import qualified Data.ByteString.Lazy.Char8 as L
66import qualified Data.ByteString.Lazy.Char8 as L
65import Data.ByteString.Builder
67import Data.ByteString.Builder
66−import Data.List (intercalate)
68+import Data.List (foldl', intercalate, intersperse)
67import Data.Maybe (fromMaybe)
69import Data.Maybe (fromMaybe)
68import Data.Monoid
70import Data.Monoid
69import Data.Text (Text)
71import Data.Text (Text)
@@ -267,8 +269,16 @@ bulk :: Server -> [BulkOperation] -> IO Reply
267bulk (Server server) bulkOps = dispatch url method body where
269bulk (Server server) bulkOps = dispatch url method body where
268 url = joinPath [server, "_bulk"]
270 url = joinPath [server, "_bulk"]
269 method = NHTM.methodPost
271 method = NHTM.methodPost
272+ body = Just $ collapseStream bulkOps
274+collapseStream :: [BulkOperation] -> L.ByteString
275+collapseStream stream = collapsed where
270− blobs = concat $ fmap getStreamChunk bulkOps
276+ blobs = intersperse "\n" $ concat $ fmap getStreamChunk stream
271− body = Just $ toLazyByteString $ mash (mempty :: Builder) blobs
277+ mashedTaters = mash (mempty :: Builder) blobs
278+ collapsed = toLazyByteString $ mappend mashedTaters "\n"
280+mash :: Builder -> [L.ByteString] -> Builder
281+mash builder xs = foldl' (\b x -> mappend b (lazyByteString x)) builder xs
273data BulkOperation =
283data BulkOperation =
274 BulkIndex IndexName MappingName DocId Value
284 BulkIndex IndexName MappingName DocId Value
@@ -276,9 +286,6 @@ data BulkOperation =
276 | BulkDelete IndexName MappingName DocId
286 | BulkDelete IndexName MappingName DocId
277 | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Show)
287 | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Show)
279−mash :: Builder -> [L.ByteString] -> Builder
280−mash builder xs = foldr (\x b -> mappend b (lazyByteString x)) builder xs
282mkMetadataValue :: Text -> String -> String -> String -> Value
289mkMetadataValue :: Text -> String -> String -> String -> Value
283mkMetadataValue operation indexName mappingName docId =
290mkMetadataValue operation indexName mappingName docId =
284 object [operation .=
291 object [operation .=