@@ -37,6 +37,7 @@ module Database.Bloodhound.Client
37 , putMapping
37 , putMapping
38 , deleteMapping
38 , deleteMapping
39 , indexDocument
39 , indexDocument
40 , getDocument
41 , getDocument
41 , documentExists
42 , documentExists
42 , deleteDocument
43 , deleteDocument
@@ -474,6 +475,20 @@ deleteMapping (IndexName indexName)
474 -- erroneously. The correct API call is: "/INDEX/_mapping/MAPPING_NAME"
475 -- erroneously. The correct API call is: "/INDEX/_mapping/MAPPING_NAME"
475 delete =<< joinPath [indexName, "_mapping", mappingName]
476 delete =<< joinPath [indexName, "_mapping", mappingName]
478+versionCtlParams :: IndexDocumentSettings -> [(Text, Maybe Text)]
479+versionCtlParams cfg =
480+ case idsVersionControl cfg of
481+ NoVersionControl -> []
482+ InternalVersion v -> versionParams v "internal"
483+ ExternalGT (ExternalDocVersion v) -> versionParams v "external_gt"
484+ ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
485+ ForceVersion (ExternalDocVersion v) -> versionParams v "force"
487+ vt = T.pack . show . docVersionNumber
488+ versionParams v t = [ ("version", Just $ vt v)
489+ , ("version_type", Just t)
477-- | 'indexDocument' is the primary way to save a single document in
492-- | 'indexDocument' is the primary way to save a single document in
478-- Elasticsearch. The document itself is simply something we can
493-- Elasticsearch. The document itself is simply something we can
479-- convert into a JSON 'Value'. The 'DocId' will function as the
494-- convert into a JSON 'Value'. The 'DocId' will function as the
@@ -488,22 +503,23 @@ indexDocument (IndexName indexName)
488 (MappingName mappingName) cfg document (DocId docId) =
503 (MappingName mappingName) cfg document (DocId docId) =
489 bindM2 put url (return body)
504 bindM2 put url (return body)
490 where url = addQuery params <$> joinPath [indexName, mappingName, docId]
505 where url = addQuery params <$> joinPath [indexName, mappingName, docId]
491− versionCtlParams = case idsVersionControl cfg of
492− NoVersionControl -> []
493− InternalVersion v -> versionParams v "internal"
494− ExternalGT (ExternalDocVersion v) -> versionParams v "external_gt"
495− ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
496− ForceVersion (ExternalDocVersion v) -> versionParams v "force"
497− vt = T.pack . show . docVersionNumber
498− versionParams v t = [ ("version", Just $ vt v)
499− , ("version_type", Just t)
501 parentParams = case idsParent cfg of
506 parentParams = case idsParent cfg of
502 Nothing -> []
507 Nothing -> []
503 Just (DocumentParent (DocId p)) -> [ ("parent", Just p) ]
508 Just (DocumentParent (DocId p)) -> [ ("parent", Just p) ]
504− params = versionCtlParams ++ parentParams
509+ params = versionCtlParams cfg ++ parentParams
505 body = Just (encode document)
510 body = Just (encode document)
512+-- | 'updateDocument' provides a way to perform an partial update of a
513+-- an already indexed document.
514+updateDocument :: (ToJSON patch, MonadBH m) => IndexName -> MappingName
515+ -> IndexDocumentSettings -> patch -> DocId -> m Reply
516+updateDocument (IndexName indexName)
517+ (MappingName mappingName) cfg patch (DocId docId) =
518+ bindM2 post url (return body)
519+ where url = addQuery (versionCtlParams cfg) <$>
520+ joinPath [indexName, mappingName, docId, "_update"]
521+ body = Just (encode $ object ["doc" .= toJSON patch])
507-- | 'deleteDocument' is the primary way to delete a single document.
523-- | 'deleteDocument' is the primary way to delete a single document.
509-- >>> _ <- runBH' $ deleteDocument testIndex testMapping (DocId "1")
525-- >>> _ <- runBH' $ deleteDocument testIndex testMapping (DocId "1")