git.iko.soy/typeable/bloodhound
on master help sign in
some lost precision. Do I care?
commit842822a784c0e335c54202ab1cc90dfdacfefd48 authorChris Allen <cma@bitemyapp.com> dateSun 6 Apr 2014, 18:43 (13 years ago) parent886ef3f
2 files changed+31−12
Database/Bloodhound/Client.hs+28−10 tests/tests.hs+3−2
Database/Bloodhound/Client.hs+28−10
@@ -11,6 +11,7 @@ module Database.Bloodhound.Client
1111 , getDocument
1212 , documentExists
1313 , deleteDocument
14+ , EsResult(..)
1415 )
1516 where
1617
@@ -21,25 +22,25 @@ import Data.Aeson.TH (deriveJSON)
2122import qualified Data.ByteString.Lazy.Char8 as L
2223import Data.List (intersperse)
2324import Data.Maybe (fromMaybe)
24−import qualified Data.Text as T
25−import Data.Time.Clock as DTC
25+import Data.Text (Text)
26+import Data.Time.Clock (UTCTime)
2627import GHC.Generics (Generic)
2728import Network.HTTP.Conduit
2829import qualified Network.HTTP.Types.Method as NHTM
2930import qualified Network.HTTP.Types.Status as NHTS
3031
31−data Version = Version { number :: T.Text
32− , build_hash :: T.Text
33− , build_timestamp :: DTC.UTCTime
32+data Version = Version { number :: Text
33+ , build_hash :: Text
34+ , build_timestamp :: UTCTime
3435 , build_snapshot :: Bool
35− , lucene_version :: T.Text } deriving (Show, Generic)
36+ , lucene_version :: Text } deriving (Show, Generic)
3637
3738data (FromJSON a, ToJSON a) => Status a =
3839 Status { ok :: Bool
3940 , status :: Int
40− , name :: T.Text
41+ , name :: Text
4142 , version :: a
42− , tagline :: T.Text } deriving (Show)
43+ , tagline :: Text } deriving (Show)
4344
4445instance ToJSON Version
4546instance FromJSON Version
@@ -71,7 +72,7 @@ instance (FromJSON a, ToJSON a) => FromJSON (Status a) where
7172
7273main = simpleHttp "http://localhost:9200/events/event/_search?q=hostname:localhost&size=1" >>= L.putStrLn
7374
74−-- data Response = Response { blah :: T.Text } deriving (Show)
75+-- data Response = Response { blah :: Text } deriving (Show)
7576
7677-- indexDocument :: ToJSON a => a -> IO Response
7778-- indexDocument doc = ToJSON a
@@ -116,7 +117,7 @@ newtype Server = Server String deriving (Show)
116117
117118-- data ElasticsearchError = ElasticsearchError
118119-- { status :: Int
119−-- , error :: T.Text } deriving (Show, Generic)
120+-- , error :: Text } deriving (Show, Generic)
120121
121122-- instance FromJSON ElasticsearchError
122123
@@ -239,6 +240,23 @@ deleteDocument (Server server) indexName mappingName docId =
239240 method = NHTM.methodDelete
240241 body = Nothing
241242
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+
242260getDocument :: Server -> IndexName -> MappingName
243261 -> DocumentID -> IO Reply
244262getDocument (Server server) indexName mappingName docId =
tests/tests.hs+3−2
@@ -55,7 +55,8 @@ main = hspec $ do
5555 let encoded = encode tweet
5656 created <- createExampleIndex
5757 docCreated <- indexDocument (Server "http://localhost:9200") "twitter" "tweet" tweet "1"
58+
5859 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)
6061 deleted <- deleteExampleIndex
61− (Just tweet) `shouldBe` newTweet
62+ (Just tweet) `shouldBe` (fmap _source newTweet)