@@ -21,15 +21,18 @@ module Database.Bloodhound.Client
21 , mkShardCount
21 , mkShardCount
22 , mkReplicaCount
22 , mkReplicaCount
23 , getStatus
23 , getStatus
24+ , encodeBulkOperations
25+ , encodeBulkOperation
27import Data.Aeson
29import Data.Aeson
28import qualified Data.ByteString.Lazy.Char8 as L
30import qualified Data.ByteString.Lazy.Char8 as L
29import Data.ByteString.Builder
31import Data.ByteString.Builder
30−import Data.List (foldl', intercalate, intersperse)
32+import Data.List (intercalate)
31import Data.Maybe (fromMaybe)
33import Data.Maybe (fromMaybe)
32import Data.Text (Text)
34import Data.Text (Text)
35+import qualified Data.Vector as V
33import Network.HTTP.Client
36import Network.HTTP.Client
34import qualified Network.HTTP.Types.Method as NHTM
37import qualified Network.HTTP.Types.Method as NHTM
35import qualified Network.HTTP.Types.Status as NHTS
38import qualified Network.HTTP.Types.Status as NHTS
@@ -167,52 +170,53 @@ deleteDocument (Server server) (IndexName indexName)
167 (MappingName mappingName) (DocId docId) =
170 (MappingName mappingName) (DocId docId) =
168 delete $ joinPath [server, indexName, mappingName, docId]
171 delete $ joinPath [server, indexName, mappingName, docId]
170−bulk :: Server -> [BulkOperation] -> IO Reply
173+bulk :: Server -> V.Vector BulkOperation -> IO Reply
171bulk (Server server) bulkOps = post url body where
174bulk (Server server) bulkOps = post url body where
172 url = joinPath [server, "_bulk"]
175 url = joinPath [server, "_bulk"]
173− body = Just $ collapseStream bulkOps
176+ body = Just $ encodeBulkOperations bulkOps
175−collapseStream :: [BulkOperation] -> L.ByteString
178+encodeBulkOperations :: V.Vector BulkOperation -> L.ByteString
176−collapseStream stream = collapsed where
179+encodeBulkOperations stream = collapsed where
177− blobs = intersperse "\n" $ concat $ fmap getStreamChunk stream
180+ blobs = fmap encodeBulkOperation stream
178 mashedTaters = mash (mempty :: Builder) blobs
181 mashedTaters = mash (mempty :: Builder) blobs
179 collapsed = toLazyByteString $ mappend mashedTaters (byteString "\n")
182 collapsed = toLazyByteString $ mappend mashedTaters (byteString "\n")
181−mash :: Builder -> [L.ByteString] -> Builder
184+mash :: Builder -> V.Vector L.ByteString -> Builder
182−mash = foldl' (\b x -> mappend b (lazyByteString x))
185+mash = V.foldl' (\b x -> b `mappend` "\n" `mappend` (lazyByteString x))
184−mkMetadataValue :: Text -> String -> String -> String -> Value
187+mkBulkStreamValue :: Text -> String -> String -> String -> Value
185−mkMetadataValue operation indexName mappingName docId =
188+mkBulkStreamValue operation indexName mappingName docId =
186 object [operation .=
189 object [operation .=
187 object [ "_index" .= indexName
190 object [ "_index" .= indexName
188 , "_type" .= mappingName
191 , "_type" .= mappingName
189 , "_id" .= docId]]
192 , "_id" .= docId]]
191−getStreamChunk :: BulkOperation -> [L.ByteString]
194+encodeBulkOperation :: BulkOperation -> L.ByteString
192−getStreamChunk (BulkIndex (IndexName indexName)
195+encodeBulkOperation (BulkIndex (IndexName indexName)
193 (MappingName mappingName)
196 (MappingName mappingName)
194− (DocId docId) value) = blob where
197+ (DocId docId) value) = blob
195− metadata = mkMetadataValue "index" indexName mappingName docId
198+ where metadata = mkBulkStreamValue "index" indexName mappingName docId
196− blob = [encode metadata, encode value]
199+ blob = encode metadata `mappend` "\n" `mappend` encode value
198−getStreamChunk (BulkCreate (IndexName indexName)
201+encodeBulkOperation (BulkCreate (IndexName indexName)
199 (MappingName mappingName)
202 (MappingName mappingName)
200− (DocId docId) value) = blob where
203+ (DocId docId) value) = blob
201− metadata = mkMetadataValue "create" indexName mappingName docId
204+ where metadata = mkBulkStreamValue "create" indexName mappingName docId
202− blob = [encode metadata, encode value]
205+ blob = encode metadata `mappend` "\n" `mappend` encode value
204−getStreamChunk (BulkDelete (IndexName indexName)
207+encodeBulkOperation (BulkDelete (IndexName indexName)
205 (MappingName mappingName)
208 (MappingName mappingName)
206− (DocId docId)) = blob where
209+ (DocId docId)) = blob
207− metadata = mkMetadataValue "delete" indexName mappingName docId
210+ where metadata = mkBulkStreamValue "delete" indexName mappingName docId
208− blob = [encode metadata]
211+ blob = encode metadata
210−getStreamChunk (BulkUpdate (IndexName indexName)
213+encodeBulkOperation (BulkUpdate (IndexName indexName)
211 (MappingName mappingName)
214 (MappingName mappingName)
212− (DocId docId) value) = blob where
215+ (DocId docId) value) = blob
213− metadata = mkMetadataValue "update" indexName mappingName docId
216+ where metadata = mkBulkStreamValue "update" indexName mappingName docId
214− doc = object ["doc" .= value]
217+ doc = object ["doc" .= value]
215− blob = [encode metadata, encode doc]
218+ blob = encode metadata `mappend` "\n" `mappend` encode doc
217getDocument :: Server -> IndexName -> MappingName
221getDocument :: Server -> IndexName -> MappingName
218 -> DocId -> IO Reply
222 -> DocId -> IO Reply