Last active
June 28, 2018 12:59
-
-
Save dawei-dev/f29046b46afc607d74aad14dbddd42e7 to your computer and use it in GitHub Desktop.
servant client for Elasticsearch bulk index API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE TypeOperators #-} | |
import Data.Aeson | |
import qualified Data.ByteString.Lazy.Char8 as LBS | |
import Data.Proxy | |
import qualified Data.Text as T | |
import qualified Data.Vector as V | |
import GHC.Generics | |
import Network.HTTP.Client (defaultManagerSettings, newManager) | |
import Network.HTTP.Client.TLS (tlsManagerSettings) | |
import Network.HTTP.Media ((//), (/:)) | |
import Servant.API | |
import Servant.Client | |
-- Describe the Data | |
data Person = Person | |
{ | |
name :: T.Text, | |
age :: Int | |
} deriving (Eq, Show,Generic) | |
instance ToJSON Person | |
data XNDJSON | |
instance Accept XNDJSON where | |
contentType _ = "application" // "x-ndjson" | |
instance ToJSON a => MimeRender XNDJSON [a] where | |
mimeRender _ val = LBS.concat $ map ((\v-> LBS.concat ["{\"index\":{}}\n",v,"\n"]) . encode) val | |
-- elasticsearch API | |
type ESAPI = "myindex" -- index name | |
:> "person" -- type name | |
:> "_bulk" -- bulk endpoint | |
:> ReqBody '[XNDJSON] [Person] | |
:> Post '[JSON] Value | |
esAPI :: Proxy ESAPI | |
esAPI = Proxy | |
bulkIndex = client esAPI | |
dumpToES :: [Person] -> IO () | |
dumpToES ps = do | |
manager' <- newManager defaultManagerSettings | |
res <- runClientM (bulkIndex ps) (mkClientEnv manager' (BaseUrl Http "localhost" 9200 "")) | |
case res of | |
Right r -> print r | |
Left x -> print x | |
main = dumpToES [Person "david" 18, Person "anna" 17] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment