@@ -1,5 +1,6 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE TupleSections #-}
4-------------------------------------------------------------------------------
5-------------------------------------------------------------------------------
@@ -26,6 +27,7 @@ module Database.Bloodhound.Client
26 , deleteIndex
27 , deleteIndex
27 , updateIndexSettings
28 , updateIndexSettings
28 , getIndexSettings
29 , getIndexSettings
29 , indexExists
31 , indexExists
30 , openIndex
32 , openIndex
31 , closeIndex
33 , closeIndex
@@ -81,7 +83,7 @@ import qualified Data.HashMap.Strict as HM
81import Data.Ix
83import Data.Ix
82import qualified Data.List as LS (filter, foldl')
84import qualified Data.List as LS (filter, foldl')
83import Data.List.NonEmpty (NonEmpty (..))
85import Data.List.NonEmpty (NonEmpty (..))
84−import Data.Maybe (fromMaybe, isJust)
86+import Data.Maybe (catMaybes, fromMaybe, isJust)
85import Data.Monoid
87import Data.Monoid
86import Data.Text (Text)
88import Data.Text (Text)
87import qualified Data.Text as T
89import qualified Data.Text as T
@@ -302,6 +304,48 @@ getIndexSettings (IndexName indexName) = do
302 where url = joinPath [indexName, "_settings"]
304 where url = joinPath [indexName, "_settings"]
307+-- | 'optimizeIndex' will optimize a single index, list of indexes or
308+-- all indexes. Note that this call will block until finishing but
309+-- will continue even if the request times out. Concurrent requests to
310+-- optimize an index while another is performing will block until the
311+-- previous one finishes. For more information see
312+-- <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-optimize.html>. Nothing
313+-- worthwhile comes back in the reply body, so matching on the status
316+-- 'optimizeIndex' with a maxNumSegments of 1 and onlyExpungeDeletes
317+-- to True is the main way to release disk space back to the OS being
318+-- held by deleted documents.
320+-- Note that this API was deprecated in ElasticSearch 2.1 for the
321+-- almost completely identical forcemerge API. Adding support to that
322+-- API would be trivial but due to the significant breaking changes,
323+-- this library cannot currently be used with >= 2.0, so that feature was omitted.
325+-- >>> let ixn = IndexName "unoptimizedindex"
326+-- >>> _ <- runBH' $ deleteIndex ixn >> createIndex defaultIndexSettings ixn
327+-- >>> response <- runBH' $ optimizeIndex (IndexList (ixn :| [])) (defaultIndexOptimizationSettings { maxNumSegments = Just 1, onlyExpungeDeletes = True })
328+-- >>> respIsTwoHunna response
330+optimizeIndex :: MonadBH m => IndexSelection -> IndexOptimizationSettings -> m Reply
331+optimizeIndex ixs IndexOptimizationSettings {..} =
332+ bindM2 post url (return body)
333+ where url = addQuery params <$> joinPath [indexName, "_optimize"]
334+ params = catMaybes [ ("max_num_segments",) . Just . showText <$> maxNumSegments
335+ , Just ("only_expunge_deletes", Just (boolQP onlyExpungeDeletes))
336+ , Just ("flush", Just (boolQP flushAfterOptimize))
338+ indexName = indexSelectionName ixs
339+ boolQP True = "true"
340+ boolQP False = "false"
344+-------------------------------------------------------------------------------
345+indexSelectionName :: IndexSelection -> Text
346+indexSelectionName (IndexList names) = T.intercalate "," [n | IndexName n <- toList names]
347+indexSelectionName AllIndexes = "_all"
305deepMerge :: [Object] -> Object
349deepMerge :: [Object] -> Object
306deepMerge = LS.foldl' go mempty
350deepMerge = LS.foldl' go mempty
307 where go acc = LS.foldl' go' acc . HM.toList
351 where go acc = LS.foldl' go' acc . HM.toList
@@ -395,6 +439,7 @@ closeIndex = openOrCloseIndexes CloseIndex
395-- >>> let aliasName = IndexName "an-alias"
439-- >>> let aliasName = IndexName "an-alias"
396-- >>> let iAlias = IndexAlias src (IndexAliasName aliasName)
440-- >>> let iAlias = IndexAlias src (IndexAliasName aliasName)
397-- >>> let aliasCreate = IndexAliasCreate Nothing Nothing
441-- >>> let aliasCreate = IndexAliasCreate Nothing Nothing
442+-- >>> _ <- runBH' $ deleteIndex src
398-- >>> respIsTwoHunna <$> runBH' (createIndex defaultIndexSettings src)
443-- >>> respIsTwoHunna <$> runBH' (createIndex defaultIndexSettings src)
400-- >>> runBH' $ indexExists src
445-- >>> runBH' $ indexExists src
@@ -484,7 +529,7 @@ versionCtlParams cfg =
484 ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
529 ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
485 ForceVersion (ExternalDocVersion v) -> versionParams v "force"
530 ForceVersion (ExternalDocVersion v) -> versionParams v "force"
487− vt = T.pack . show . docVersionNumber
532+ vt = showText . docVersionNumber
488 versionParams v t = [ ("version", Just $ vt v)
533 versionParams v t = [ ("version", Just $ vt v)
489 , ("version_type", Just t)
534 , ("version_type", Just t)