@@ -25,10 +25,9 @@ import Data.Text (Text)
25import Network.HTTP.Conduit
25import Network.HTTP.Conduit
26import qualified Network.HTTP.Types.Method as NHTM
26import qualified Network.HTTP.Types.Method as NHTM
27import qualified Network.HTTP.Types.Status as NHTS
27import qualified Network.HTTP.Types.Status as NHTS
28−import Prelude hiding (head)
28+import Prelude hiding (head, filter)
30import Database.Bloodhound.Types
30import Database.Bloodhound.Types
31−import Database.Bloodhound.Types.Class
32import Database.Bloodhound.Types.Instances
31import Database.Bloodhound.Types.Instances
34-- find way to avoid destructuring Servers and Indexes?
33-- find way to avoid destructuring Servers and Indexes?
@@ -51,14 +50,15 @@ mkReplicaCount n
51responseIsError :: Reply -> Bool
50responseIsError :: Reply -> Bool
52responseIsError resp = NHTS.statusCode (responseStatus resp) > 299
51responseIsError resp = NHTS.statusCode (responseStatus resp) > 299
53+emptyBody :: L.ByteString
54emptyBody = L.pack ""
54emptyBody = L.pack ""
56dispatch :: Method -> String -> Maybe L.ByteString
56dispatch :: Method -> String -> Maybe L.ByteString
57 -> IO Reply
57 -> IO Reply
58−dispatch method url body = do
58+dispatch dMethod url body = do
59 initReq <- parseUrl url
59 initReq <- parseUrl url
60 let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
60 let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
61− let req = initReq { method = method
61+ let req = initReq { method = dMethod
62 , requestBody = reqBody
62 , requestBody = reqBody
63 , checkStatus = \_ _ _ -> Nothing}
63 , checkStatus = \_ _ _ -> Nothing}
64 withManager $ httpLbs req
64 withManager $ httpLbs req
@@ -66,10 +66,16 @@ dispatch method url body = do
66joinPath :: [String] -> String
66joinPath :: [String] -> String
67joinPath = intercalate "/"
67joinPath = intercalate "/"
69+-- Shortcut functions for HTTP methods
70+delete :: String -> IO Reply
69delete = flip (dispatch NHTM.methodDelete) $ Nothing
71delete = flip (dispatch NHTM.methodDelete) $ Nothing
72+get :: String -> IO Reply
70get = flip (dispatch NHTM.methodGet) $ Nothing
73get = flip (dispatch NHTM.methodGet) $ Nothing
74+head :: String -> IO Reply
71head = flip (dispatch NHTM.methodHead) $ Nothing
75head = flip (dispatch NHTM.methodHead) $ Nothing
76+put :: String -> Maybe L.ByteString -> IO Reply
72put = dispatch NHTM.methodPost
77put = dispatch NHTM.methodPost
78+post :: String -> Maybe L.ByteString -> IO Reply
73post = dispatch NHTM.methodPost
79post = dispatch NHTM.methodPost
75-- indexDocument s ix name doc = put (root </> s </> ix </> name </> doc) (Just encode doc)
81-- indexDocument s ix name doc = put (root </> s </> ix </> name </> doc) (Just encode doc)
@@ -95,13 +101,14 @@ deleteIndex (Server server) (IndexName indexName) =
95respIsTwoHunna :: Reply -> Bool
101respIsTwoHunna :: Reply -> Bool
96respIsTwoHunna resp = NHTS.statusCode (responseStatus resp) == 200
102respIsTwoHunna resp = NHTS.statusCode (responseStatus resp) == 200
104+existentialQuery :: String -> IO (Reply, Bool)
98existentialQuery url = do
105existentialQuery url = do
99 reply <- head url
106 reply <- head url
100 return (reply, respIsTwoHunna reply)
107 return (reply, respIsTwoHunna reply)
102indexExists :: Server -> IndexName -> IO Bool
109indexExists :: Server -> IndexName -> IO Bool
103indexExists (Server server) (IndexName indexName) = do
110indexExists (Server server) (IndexName indexName) = do
104− (reply, exists) <- existentialQuery url
111+ (_, exists) <- existentialQuery url
105 return exists
112 return exists
106 where url = joinPath [server, indexName]
113 where url = joinPath [server, indexName]
@@ -110,6 +117,7 @@ refreshIndex (Server server) (IndexName indexName) =
110 post url Nothing
117 post url Nothing
111 where url = joinPath [server, indexName, "_refresh"]
118 where url = joinPath [server, indexName, "_refresh"]
120+stringifyOCIndex :: OpenCloseIndex -> String
113stringifyOCIndex oci = case oci of
121stringifyOCIndex oci = case oci of
114 OpenIndex -> "_open"
122 OpenIndex -> "_open"
115 CloseIndex -> "_close"
123 CloseIndex -> "_close"
@@ -207,7 +215,7 @@ documentExists :: Server -> IndexName -> MappingName
207 -> DocId -> IO Bool
215 -> DocId -> IO Bool
208documentExists (Server server) (IndexName indexName)
216documentExists (Server server) (IndexName indexName)
209 (MappingName mappingName) (DocId docId) = do
217 (MappingName mappingName) (DocId docId) = do
210− (reply, exists) <- existentialQuery url
218+ (_, exists) <- existentialQuery url
211 return exists where
219 return exists where
212 url = joinPath [server, indexName, mappingName, docId]
220 url = joinPath [server, indexName, mappingName, docId]
@@ -231,4 +239,4 @@ mkSearch :: Maybe Query -> Maybe Filter -> Search
231mkSearch query filter = Search query filter Nothing False 0 10
239mkSearch query filter = Search query filter Nothing False 0 10
233pageSearch :: Int -> Int -> Search -> Search
241pageSearch :: Int -> Int -> Search -> Search
234−pageSearch from size search = search { from = from, size = size }
242+pageSearch pageFrom pageSize search = search { from = pageFrom, size = pageSize }