-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Search with elasticsearch and facets #57
Draft
amitaibu
wants to merge
19
commits into
main
Choose a base branch
from
50-elasticsearch-and-facets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
45ccd9e
Enable Elasticsearch
amitaibu d19c355
Add bloodhound
amitaibu 2069e34
Use overmind
amitaibu 0469291
Add News record
amitaibu a6bb37c
More copy from AI
amitaibu cb88e30
Update from AI
amitaibu 03f672f
Fix compile
amitaibu 8dfe865
Fix getAppConfig
amitaibu 505ce75
ELASTICSEARCH_HOST=http://localhost
amitaibu c8f7eff
Merge branch 'main' into 50-elasticsearch-and-facets
amitaibu 36dad88
delete from Index
amitaibu 006cba1
Conditionally set process.implementation based on the presence of GIT…
amitaibu d12dd17
Start working on searchNews
amitaibu 1d5937e
more work
amitaibu 70280e8
Remove `id` from object
amitaibu e6093a7
Fix config
amitaibu b9f6b12
Start parsing
amitaibu 36ea162
Finally parse
amitaibu c842e8a
Multiple fields
amitaibu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module Application.Helper.Elasticsearch where | ||
|
||
import IHP.Prelude | ||
import IHP.ModelSupport | ||
import Database.Bloodhound | ||
import Network.HTTP.Client | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as TE | ||
import Data.Aeson | ||
import IHP.ControllerPrelude | ||
|
||
import Generated.Types | ||
|
||
-- Make News an instance of ToJSON to allow serialization | ||
instance ToJSON News where | ||
toJSON news = object | ||
[ "id" .= (show news.id) | ||
, "title" .= news.title | ||
, "body" .= news.body | ||
-- Add other fields as necessary | ||
] | ||
|
||
-- Initialize Elasticsearch connection | ||
initES :: (?context :: ControllerContext) => IO BHEnv | ||
initES = do | ||
let server = ?context.frameworkConfig.esServer | ||
manager <- newManager defaultManagerSettings | ||
return $ mkBHEnv server manager | ||
|
||
-- Index a news item in Elasticsearch | ||
indexNews :: (?modelContext :: ModelContext, ?context :: ControllerContext) => News -> IO (Either BHError IndexResponse) | ||
indexNews news = do | ||
bhenv <- initES | ||
let indexName = IndexName "news_index" | ||
docId = DocId $ T.pack $ "news_" <> show news.id | ||
runBH bhenv $ indexDocument indexName (MappingName "document") (toJSON news) docId | ||
|
||
-- Search for news | ||
searchNews :: (?modelContext :: ModelContext, ?context :: ControllerContext) => Text -> IO [SearchResult Value] | ||
searchNews query = do | ||
bhenv <- initES | ||
let indexName = IndexName "news_index" | ||
searchQuery = MultiMatchQuery ["title", "body"] (TE.encodeUtf8 query) | ||
search = mkSearch (Just searchQuery) Nothing | ||
result <- runBH bhenv $ searchByIndex indexName search | ||
case result of | ||
Left err -> do | ||
putStrLn $ "Error: " ++ show err | ||
return [] | ||
Right searchResult -> return $ hits $ searchHits searchResult | ||
|
||
-- Helper function to use in your controllers | ||
searchNewsHandler :: (?modelContext :: ModelContext, ?context :: ControllerContext) => Text -> IO [News] | ||
searchNewsHandler query = do | ||
results <- searchNews query | ||
return $ mapMaybe (decode . encode . sourceAsJSON) results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Web.Controller.News where | ||
|
||
import Web.Controller.Prelude | ||
import Web.View.News.Index | ||
import Web.View.News.New | ||
import Web.View.News.Edit | ||
import Web.View.News.Show | ||
|
||
instance Controller NewsController where | ||
action NewsAction = do | ||
news <- query @News |> fetch | ||
render IndexView { .. } | ||
|
||
action NewNewsAction = do | ||
let news = newRecord | ||
render NewView { .. } | ||
|
||
action ShowNewsAction { newsId } = do | ||
news <- fetch newsId | ||
render ShowView { .. } | ||
|
||
action EditNewsAction { newsId } = do | ||
news <- fetch newsId | ||
render EditView { .. } | ||
|
||
action UpdateNewsAction { newsId } = do | ||
news <- fetch newsId | ||
news | ||
|> buildNews | ||
|> ifValid \case | ||
Left news -> render EditView { .. } | ||
Right news -> do | ||
news <- news |> updateRecord | ||
setSuccessMessage "News updated" | ||
redirectTo EditNewsAction { .. } | ||
|
||
action CreateNewsAction = do | ||
let news = newRecord @News | ||
news | ||
|> buildNews | ||
|> ifValid \case | ||
Left news -> render NewView { .. } | ||
Right news -> do | ||
news <- news |> createRecord | ||
setSuccessMessage "News created" | ||
redirectTo NewsAction | ||
|
||
action DeleteNewsAction { newsId } = do | ||
news <- fetch newsId | ||
deleteRecord news | ||
setSuccessMessage "News deleted" | ||
redirectTo NewsAction | ||
|
||
buildNews news = news | ||
|> fill @'[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ instance AutoRoute UsersController | |
|
||
instance AutoRoute StyleGuideController | ||
|
||
|
||
instance AutoRoute NewsController | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Web.View.News.Edit where | ||
import Web.View.Prelude | ||
|
||
data EditView = EditView { news :: News } | ||
|
||
instance View EditView where | ||
html EditView { .. } = [hsx| | ||
{breadcrumb} | ||
<h1>Edit News</h1> | ||
{renderForm news} | ||
|] | ||
where | ||
breadcrumb = renderBreadcrumb | ||
[ breadcrumbLink "News" NewsAction | ||
, breadcrumbText "Edit News" | ||
] | ||
|
||
renderForm :: News -> Html | ||
renderForm news = formFor news [hsx| | ||
|
||
{submitButton} | ||
|
||
|] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Web.View.News.Index where | ||
import Web.View.Prelude | ||
|
||
data IndexView = IndexView { news :: [News] } | ||
|
||
instance View IndexView where | ||
html IndexView { .. } = [hsx| | ||
{breadcrumb} | ||
|
||
<h1>Index<a href={pathTo NewNewsAction} class="btn btn-primary ms-4">+ New</a></h1> | ||
<div class="table-responsive"> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>News</th> | ||
<th></th> | ||
<th></th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody>{forEach news renderNews}</tbody> | ||
</table> | ||
|
||
</div> | ||
|] | ||
where | ||
breadcrumb = renderBreadcrumb | ||
[ breadcrumbLink "News" NewsAction | ||
] | ||
|
||
renderNews :: News -> Html | ||
renderNews news = [hsx| | ||
<tr> | ||
<td>{news}</td> | ||
<td><a href={ShowNewsAction news.id}>Show</a></td> | ||
<td><a href={EditNewsAction news.id} class="text-muted">Edit</a></td> | ||
<td><a href={DeleteNewsAction news.id} class="js-delete text-muted">Delete</a></td> | ||
</tr> | ||
|] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Web.View.News.New where | ||
import Web.View.Prelude | ||
|
||
data NewView = NewView { news :: News } | ||
|
||
instance View NewView where | ||
html NewView { .. } = [hsx| | ||
{breadcrumb} | ||
<h1>New News</h1> | ||
{renderForm news} | ||
|] | ||
where | ||
breadcrumb = renderBreadcrumb | ||
[ breadcrumbLink "News" NewsAction | ||
, breadcrumbText "New News" | ||
] | ||
|
||
renderForm :: News -> Html | ||
renderForm news = formFor news [hsx| | ||
|
||
{submitButton} | ||
|
||
|] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Web.View.News.Show where | ||
import Web.View.Prelude | ||
|
||
data ShowView = ShowView { news :: News } | ||
|
||
instance View ShowView where | ||
html ShowView { .. } = [hsx| | ||
{breadcrumb} | ||
<h1>Show News</h1> | ||
<p>{news}</p> | ||
|
||
|] | ||
where | ||
breadcrumb = renderBreadcrumb | ||
[ breadcrumbLink "News" NewsAction | ||
, breadcrumbText "Show News" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how Claude AI told me to do it