git.iko.soy/typeable/bloodhound
on master help sign in
some lost precision. Do I care?
842822aChris Allen
Database/Bloodhound/Client.hs+28−10
@@ -11,6 +11,7 @@ module Database.Bloodhound.Client
11 , getDocument
11 , getDocument
12 , documentExists
12 , documentExists
13 , deleteDocument
13 , deleteDocument
14+ , EsResult(..)
14 )
15 )
15 where
16 where
16
17
@@ -21,25 +22,25 @@ import Data.Aeson.TH (deriveJSON)
21import qualified Data.ByteString.Lazy.Char8 as L
22import qualified Data.ByteString.Lazy.Char8 as L
22import Data.List (intersperse)
23import Data.List (intersperse)
23import Data.Maybe (fromMaybe)
24import Data.Maybe (fromMaybe)
24−import qualified Data.Text as T
25+import Data.Text (Text)
25−import Data.Time.Clock as DTC
26+import Data.Time.Clock (UTCTime)
26import GHC.Generics (Generic)
27import GHC.Generics (Generic)
27import Network.HTTP.Conduit
28import Network.HTTP.Conduit
28import qualified Network.HTTP.Types.Method as NHTM
29import qualified Network.HTTP.Types.Method as NHTM
29import qualified Network.HTTP.Types.Status as NHTS
30import qualified Network.HTTP.Types.Status as NHTS
30
31
31−data Version = Version { number :: T.Text
32+data Version = Version { number :: Text
32− , build_hash :: T.Text
33+ , build_hash :: Text
33− , build_timestamp :: DTC.UTCTime
34+ , build_timestamp :: UTCTime
34 , build_snapshot :: Bool
35 , build_snapshot :: Bool
35− , lucene_version :: T.Text } deriving (Show, Generic)
36+ , lucene_version :: Text } deriving (Show, Generic)
36
37
37data (FromJSON a, ToJSON a) => Status a =
38data (FromJSON a, ToJSON a) => Status a =
38 Status { ok :: Bool
39 Status { ok :: Bool
39 , status :: Int
40 , status :: Int
40− , name :: T.Text
41+ , name :: Text
41 , version :: a
42 , version :: a
42− , tagline :: T.Text } deriving (Show)
43+ , tagline :: Text } deriving (Show)
43
44
44instance ToJSON Version
45instance ToJSON Version
45instance FromJSON Version
46instance FromJSON Version
@@ -71,7 +72,7 @@ instance (FromJSON a, ToJSON a) => FromJSON (Status a) where
71
72
72main = simpleHttp "http://localhost:9200/events/event/_search?q=hostname:localhost&size=1" >>= L.putStrLn
73main = simpleHttp "http://localhost:9200/events/event/_search?q=hostname:localhost&size=1" >>= L.putStrLn
73
74
74−-- data Response = Response { blah :: T.Text } deriving (Show)
75+-- data Response = Response { blah :: Text } deriving (Show)
75
76
76-- indexDocument :: ToJSON a => a -> IO Response
77-- indexDocument :: ToJSON a => a -> IO Response
77-- indexDocument doc = ToJSON a
78-- indexDocument doc = ToJSON a
@@ -116,7 +117,7 @@ newtype Server = Server String deriving (Show)
116
117
117-- data ElasticsearchError = ElasticsearchError
118-- data ElasticsearchError = ElasticsearchError
118-- { status :: Int
119-- { status :: Int
119−-- , error :: T.Text } deriving (Show, Generic)
120+-- , error :: Text } deriving (Show, Generic)
120
121
121-- instance FromJSON ElasticsearchError
122-- instance FromJSON ElasticsearchError
122
123
@@ -239,6 +240,23 @@ deleteDocument (Server server) indexName mappingName docId =
239 method = NHTM.methodDelete
240 method = NHTM.methodDelete
240 body = Nothing
241 body = Nothing
241
242
243+data EsResult a = EsResult { _index :: Text
244+ , _type :: Text
245+ , _id :: Text
246+ , _version :: Int
247+ , found :: Bool
248+ , _source :: a } deriving (Eq, Show)
249+
250+instance (FromJSON a, ToJSON a) => FromJSON (EsResult a) where
251+ parseJSON (Object v) = EsResult <$>
252+ v .: "_index" <*>
253+ v .: "_type" <*>
254+ v .: "_id" <*>
255+ v .: "_version" <*>
256+ v .: "found" <*>
257+ v .: "_source"
258+ parseJSON _ = empty
259+
242getDocument :: Server -> IndexName -> MappingName
260getDocument :: Server -> IndexName -> MappingName
243 -> DocumentID -> IO Reply
261 -> DocumentID -> IO Reply
244getDocument (Server server) indexName mappingName docId =
262getDocument (Server server) indexName mappingName docId =
tests/tests.hs+3−2
@@ -55,7 +55,8 @@ main = hspec $ do
55 let encoded = encode tweet
55 let encoded = encode tweet
56 created <- createExampleIndex
56 created <- createExampleIndex
57 docCreated <- indexDocument (Server "http://localhost:9200") "twitter" "tweet" tweet "1"
57 docCreated <- indexDocument (Server "http://localhost:9200") "twitter" "tweet" tweet "1"
58+
58 docInserted <- getDocument (Server "http://localhost:9200") "twitter" "tweet" "1"
59 docInserted <- getDocument (Server "http://localhost:9200") "twitter" "tweet" "1"
59− let newTweet = decode (responseBody docInserted) :: Maybe Tweet
60+ let newTweet = decode (responseBody docInserted) :: Maybe (EsResult Tweet)
60 deleted <- deleteExampleIndex
61 deleted <- deleteExampleIndex
61− (Just tweet) `shouldBe` newTweet
62+ (Just tweet) `shouldBe` (fmap _source newTweet)