Skip to content

Commit e33b731

Browse files
committed
revert Json use for hits
1 parent 7404f22 commit e33b731

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

src/Database/Bloodhound/Raw.hs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import Data.Text qualified as T
2424
import Data.Vector qualified as V
2525
import Database.Bloodhound qualified as BH
2626
import Database.Bloodhound.Common.Requests qualified as Query
27+
import Json.Extras qualified as Json
2728
import Monocle.Prelude
29+
import Network.HTTP.Client qualified as HTTP
2830
import Network.HTTP.Types.Method qualified as HTTP
31+
import Network.HTTP.Types.Status qualified as HTTP
2932

3033
data ScrollRequest = NoScroll | GetScroll ByteString
3134

@@ -76,13 +79,28 @@ search index payload scrollRequest = do
7679
-- | A special purpose search implementation that uses the faster json-syntax
7780
searchHit ::
7881
MonadBH m =>
79-
Aeson.FromJSON a =>
82+
(Json.Value -> Either Text a) ->
8083
BH.IndexName ->
8184
BH.Search ->
82-
m (BH.SearchResult a)
83-
searchHit index payload = do
84-
let query = Query.searchByIndex index payload
85-
resp <- BH.tryEsError $ BH.performBHRequest query
85+
m [a]
86+
searchHit parseHit index payload = do
87+
let query = Query.searchByIndex @Value index payload
88+
resp <-
89+
BH.tryEsError $
90+
BH.performBHRequest @_ @BH.StatusIndependant query {
91+
BH.bhRequestParser = \(BH.BHResponse rawResp) ->
92+
let
93+
decodeHits :: Json.Value -> Maybe [Json.Value]
94+
decodeHits value = do
95+
hits <- Json.getAttr "hits" =<< Json.getAttr "hits" value
96+
fmap getSource <$> Json.getArray hits
97+
getSource value = case Json.getAttr "_source" value of
98+
Nothing -> error $ "No source found in: " <> show value
99+
Just v -> v
100+
in case decodeHits (Json.decodeThrow $ HTTP.responseBody rawResp) of
101+
Just xs -> pure $ first (BH.EsError $ Just $ HTTP.statusCode $ HTTP.responseStatus rawResp) $ traverse parseHit xs
102+
Nothing -> error $ "Could not find hits in " <> show rawResp
103+
}
86104
case resp of
87105
Right xs -> pure xs
88106
Left e -> throwEsError "Could not find hits" e

src/Monocle/Backend/Queries.hs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ doAdvanceScrollBH scroll = do
6464
measureQueryM (Aeson.object ["scrolling" .= ("advancing..." :: Text)]) do
6565
esAdvance scroll
6666

67-
doSearchHitBH :: QEffects es => FromJSON a => BH.Search -> Eff es (BH.SearchResult a)
68-
doSearchHitBH payload = do
67+
doSearchHitBH :: QEffects es => (Json.Value -> Either Text a) -> BH.Search -> Eff es [a]
68+
doSearchHitBH parseHit payload = do
6969
measureQueryM payload do
7070
index <- getIndexName
71-
esSearchHit index payload
71+
esSearchHit parseHit index payload
7272

7373
-- | Call the count endpoint
7474
doCountBH :: QEffects es => BH.Query -> Eff es Count
@@ -156,10 +156,11 @@ doSearch orderM limit = do
156156
SearchPB.Order_DirectionDESC -> BH.Descending
157157

158158
-- | Get search results hits, as fast as possible
159-
doFastSearch :: QEffects es => FromJSON a => Word32 -> Eff es (BH.SearchResult a)
160-
doFastSearch limit = do
159+
doFastSearch :: QEffects es => (Json.Value -> Either Text a) -> Word32 -> Eff es [a]
160+
doFastSearch parseHit limit = do
161161
query <- getQueryBH
162162
doSearchHitBH
163+
parseHit
163164
(BH.mkSearch query Nothing)
164165
{ BH.size = BH.Size $ fromInteger $ toInteger $ max 50 limit
165166
}
@@ -448,14 +449,13 @@ data JsonChangeEvent = JsonChangeEvent
448449
, jceAuthor :: Json.ShortText
449450
}
450451

451-
instance FromJSON JsonChangeEvent where
452-
parseJSON =
453-
Aeson.withObject "JsonChangeEvent" $ \o ->
454-
JsonChangeEvent
455-
<$> (o .: "created_at")
456-
<*> (o .: "on_created_at")
457-
<*> (o .: "change_id")
458-
<*> ((.: "muid") =<< o .: "author")
452+
decodeJsonChangeEvent :: Json.Value -> Maybe JsonChangeEvent
453+
decodeJsonChangeEvent v = do
454+
jceCreatedAt <- Json.getDate =<< Json.getAttr "created_at" v
455+
jceOnCreatedAt <- Json.getDate =<< Json.getAttr "on_created_at" v
456+
jceChangeId <- Json.getString =<< Json.getAttr "change_id" v
457+
jceAuthor <- Json.getString =<< Json.getAttr "muid" =<< Json.getAttr "author" v
458+
pure $ JsonChangeEvent {..}
459459

460460
firstEventDuration :: FirstEvent -> Pico
461461
firstEventDuration FirstEvent {..} = elapsedSeconds feChangeCreatedAt feCreatedAt
@@ -468,7 +468,7 @@ firstEventOnChanges = do
468468
(minDate, _) <- getQueryBound
469469

470470
-- Collect all the events
471-
result <- mapMaybe BH.hitSource . BH.hits . BH.searchHits <$> doFastSearch 10000
471+
result <- catMaybes <$> doFastSearch (Right . decodeJsonChangeEvent) 10000
472472

473473
-- Group by change_id
474474
let changeMap :: [NonEmpty JsonChangeEvent]

src/Monocle/Effects.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import Control.Exception (finally, throwIO)
6262
import Control.Exception.Base (ErrorCall (ErrorCall))
6363
import Control.Monad.Catch (catches)
6464
import Data.Text qualified as T
65+
import Json.Extras qualified as Json
6566
import Monocle.Client qualified
6667
import Monocle.Config qualified
6768
import Network.HTTP.Client (HttpException (..))
@@ -466,17 +467,17 @@ esAdvance :: (Error ElasticError :> es, ElasticEffect :> es, FromJSON resp) => B
466467
esAdvance scroll = do
467468
runBHIOSafe "esAdvance" scroll $ BHR.advance scroll
468469

469-
esGetDocument :: (Error ElasticError :> es, ElasticEffect :> es) => FromJSON a => BH.IndexName -> BH.DocId -> Eff es (Either BH.EsError (BH.EsResult a))
470+
esGetDocument :: (Error ElasticError :> es, ElasticEffect :> es) => FromJSON a => BH.IndexName -> BH.DocId -> Eff es (Either BH.EsError (BH.EsResult a))
470471
esGetDocument iname doc = do
471472
runBHIOUnsafe "esGetDocument" doc $ BH.getDocument iname doc
472473

473474
esCountByIndex :: (Error ElasticError :> es, ElasticEffect :> es) => BH.IndexName -> BH.CountQuery -> Eff es (Either BH.EsError BH.CountResponse)
474475
esCountByIndex iname q = do
475476
runBHIOUnsafe "esCountByIndex" q $ BH.countByIndex iname q
476477

477-
esSearchHit :: (Error ElasticError :> es, ElasticEffect :> es) => FromJSON a => BH.IndexName -> BH.Search -> Eff es (BH.SearchResult a)
478-
esSearchHit iname payload = do
479-
runBHIOSafe "esSearchHit" payload $ BHR.searchHit iname payload
478+
esSearchHit :: (Error ElasticError :> es, ElasticEffect :> es) => (Json.Value -> Either Text a) -> BH.IndexName -> BH.Search -> Eff es [a]
479+
esSearchHit parseHit iname payload = do
480+
runBHIOSafe "esSearchHit" payload $ BHR.searchHit parseHit iname payload
480481

481482
esScanSearch :: (Error ElasticError :> es, ElasticEffect :> es) => FromJSON body => BH.IndexName -> BH.Search -> Eff es [BH.Hit body]
482483
esScanSearch iname search = do

0 commit comments

Comments
 (0)