From b9f6b12825ddafea93b7d34e1d7a933473752294 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 24 Jul 2024 22:03:42 +0300 Subject: [PATCH] Start parsing --- Application/Helper/Elasticsearch.hs | 24 ++++++++++++++++++++++-- Web/Controller/News.hs | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Application/Helper/Elasticsearch.hs b/Application/Helper/Elasticsearch.hs index 61f513b..9dcb1f3 100644 --- a/Application/Helper/Elasticsearch.hs +++ b/Application/Helper/Elasticsearch.hs @@ -1,6 +1,7 @@ module Application.Helper.Elasticsearch where import IHP.Prelude +import IHP.ModelSupport import Database.Bloodhound import Network.HTTP.Client (Manager, Response(..)) import Control.Lens ((^.)) @@ -8,6 +9,7 @@ import qualified Data.ByteString.Lazy as LBS import Data.Aeson (ToJSON, FromJSON, Value, toJSON, parseJSON, withObject, (.:), decode, object, (.=), eitherDecode) import IHP.ControllerSupport import qualified Data.Text as T +import Prelude (read) import Generated.Types @@ -49,12 +51,30 @@ deleteIndexNews newsId = do searchNews :: (?context :: ControllerContext) => Text -> IO [Id News] searchNews queryText = do -- Execute the search request - result <- runBH (mkBHEnv esServer esManager) $ searchByIndex indexName (mkSearch (Just query) Nothing) + response <- runBH (mkBHEnv esServer esManager) $ searchByIndex indexName (mkSearch (Just query) Nothing) + -- Parse the response + result <- parseEsResponse response + case result of + Left esError -> do + -- Handle the error (log it, return empty list, or throw an exception) + liftIO $ putStrLn $ "Error occurred: " ++ show esError + return [] + Right (searchResult :: SearchResult Value) -> do + -- Extract the News IDs from the search result + + let newsIds :: [Id News] = map (textToId . unDocId . hitDocId) $ hits $ searchHits searchResult + liftIO $ putStrLn $ "newsIds: " ++ show newsIds + return [] + -- Parse result and extract the News ids. The News IDs is the Doc pure [] where (esServer, esManager) = getAppConfig @(Server, Manager) indexName = IndexName "news" - query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString queryText) + -- @todo: Search all fields + query = QueryMatchQuery $ mkMatchQuery (FieldName "body") (QueryString queryText) + + unDocId :: DocId -> Text + unDocId (DocId t) = t diff --git a/Web/Controller/News.hs b/Web/Controller/News.hs index 382f2bf..34876b8 100644 --- a/Web/Controller/News.hs +++ b/Web/Controller/News.hs @@ -11,6 +11,7 @@ import Application.Helper.Elasticsearch instance Controller NewsController where action NewsAction = do news <- query @News |> fetch + searchNews "bar" render IndexView { .. } action NewNewsAction = do