Created
June 11, 2018 09:25
-
-
Save dawei-dev/796eaf163e69ca6a83b44a436117c9e6 to your computer and use it in GitHub Desktop.
Query Elasticsearch with Raw JSON in Haskell
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.Aeson.QQ | |
import Data.Maybe | |
import Data.Proxy | |
import Network.HTTP.Client (defaultManagerSettings, newManager) | |
import Servant.API | |
import Servant.Client | |
myqueryV1 :: Value | |
myqueryV1 = [aesonQQ| | |
{ | |
"query": { | |
"bool": { | |
"should": [ | |
{ | |
"constant_score": { | |
"filter": { | |
"match": { | |
"bodyText": "functional" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"match": { | |
"bodyText": "programming" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"match": { | |
"bodyText": "rocks" | |
} | |
} | |
} | |
} | |
] | |
} | |
} | |
}|] | |
type ESAPI = "myindex" -- index name | |
:> "_search" -- ES search end point | |
:> ReqBody '[JSON] Value | |
:> Post '[JSON] Value | |
-- The only boilerplate code | |
esAPI :: Proxy ESAPI | |
esAPI = Proxy | |
-- Generate the Haskell Client | |
esquery = client esAPI | |
main :: IO () | |
main = do | |
manager' <- newManager defaultManagerSettings | |
res <- runClientM | |
(esquery myqueryV1) | |
(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