Skip to content

Commit

Permalink
Try polymorphic functor to pick between [] and Vector
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanCacqueray committed Jan 18, 2024
1 parent a73fcd1 commit 764fabf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 13 additions & 6 deletions quickwit-client/src/Quickwit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module Quickwit (
-- * Search API
SearchQuery (..),
SearchResponse (..),
searchData,
searchDataList,
searchDataVector,

-- * Index API
IndexID,
Expand Down Expand Up @@ -142,25 +143,31 @@ instance ToJSON SearchQuery where
["query" .= sq.query, "format" .= ("json" :: Text)]
<> maybe [] (\v -> ["start_timestamp" .= v]) sq.start_timestamp

data SearchResponse a = SearchResponse
{ hits :: Vector a
data SearchResponse f a = SearchResponse
{ hits :: f a
, num_hits :: Word
, elapsed_time_micros :: Word
}
deriving (Show)

instance (FromJSON a) => FromJSON (SearchResponse a) where
instance (FromJSON a, FromJSON (f a)) => FromJSON (SearchResponse f a) where
parseJSON = withObject "SearchResponse" \obj ->
SearchResponse
<$> obj .: "hits"
<*> obj .: "num_hits"
<*> obj .: "elapsed_time_micros"

searchData :: (FromJSON a) => APIClient -> IndexID -> SearchQuery -> QIO (SearchResponse a)
searchData :: (FromJSON a, FromJSON (f a)) => APIClient -> IndexID -> SearchQuery -> QIO (SearchResponse f a)
searchData client indexID body = readJSON client req
where
req = requestJSON methodPost (searchPath indexID) [] body

searchDataList :: (FromJSON a) => APIClient -> IndexID -> SearchQuery -> QIO (SearchResponse [] a)
searchDataList = searchData

searchDataVector :: (FromJSON a) => APIClient -> IndexID -> SearchQuery -> QIO (SearchResponse Vector a)
searchDataVector = searchData

demo :: IO ()
demo =
withAPIClient local \client -> do
Expand Down Expand Up @@ -189,7 +196,7 @@ demo =

putStrLn "\n[+] Searching"
let query = SearchQuery "*" Nothing
print =<< searchData @Value client indexID query
print =<< searchDataList @Value client indexID query

local :: Endpoint
local = ("127.0.0.1", 7280)
5 changes: 2 additions & 3 deletions quickwit-ui/QuickwitUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Data.ByteString.Lazy (toStrict)
import Data.Generics.Labels ()
import Data.Text (Text, pack)
import Data.Text.Encoding (decodeUtf8)
import Data.Vector qualified as V
import GHC.Generics (Generic)
import Monomer
import Quickwit qualified as Q
Expand Down Expand Up @@ -56,10 +55,10 @@ handleEvent _wenv _node model evt = case evt of
encodeJSON = decodeUtf8 . toStrict . encode
fetchResult :: IO AppEvent
fetchResult = Q.withAPIClient Q.local \client -> do
eResp <- Q.searchData client [Q.index|log-base|] $ Q.SearchQuery model.search Nothing
eResp <- Q.searchDataList client [Q.index|log-base|] $ Q.SearchQuery model.search Nothing
pure $ AppDisplay $ case eResp of
Left err -> [pack (show err)]
Right resp -> V.toList $ encodeJSON @Value <$> resp.hits
Right resp -> encodeJSON @Value <$> resp.hits

main :: IO ()
main = do
Expand Down

0 comments on commit 764fabf

Please sign in to comment.