Skip to content

Instantly share code, notes, and snippets.

@dawei-dev
Created June 3, 2018 13:42
Show Gist options
  • Save dawei-dev/74c6283915bfec561ef3fa2fa224a798 to your computer and use it in GitHub Desktop.
Save dawei-dev/74c6283915bfec561ef3fa2fa224a798 to your computer and use it in GitHub Desktop.
{-# 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