Created
June 3, 2018 13:42
-
-
Save dawei-dev/74c6283915bfec561ef3fa2fa224a798 to your computer and use it in GitHub Desktop.
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 OverloadedStrings #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE TypeOperators #-} | |
import Data.Aeson | |
import Data.Maybe | |
import Data.Proxy | |
import Data.String.QQ | |
import Network.HTTP.Client (defaultManagerSettings, newManager) | |
import Servant.API | |
import Servant.Client | |
-- elasticsearch API | |
guardianMapping :: Maybe Value | |
guardianMapping = decode [s| | |
{ | |
"mappings": { | |
"articles": { | |
"properties": { | |
"wordcount": { | |
"type": "integer" | |
}, | |
"bodyText": { | |
"type": "text" | |
} | |
} | |
} | |
} | |
}|] | |
-- Create a new index called "guardian" with a mapping in the ReqBody | |
type ESAPI = "guardian" | |
:> ReqBody '[JSON] Value | |
:> Put '[JSON] Value | |
-- The only boilerplate code | |
esAPI :: Proxy ESAPI | |
esAPI = Proxy | |
-- Generate the Haskell Client | |
createMapping = client esAPI | |
main :: IO () | |
main = do | |
manager' <- newManager defaultManagerSettings | |
res <- runClientM | |
(createMapping (fromJust guardianMapping)) | |
(mkClientEnv manager' (BaseUrl Http "localhost" 9200 "")) | |
case res of | |
Right r -> print r | |
Left x -> print x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment