Skip to content

Commit

Permalink
feat: add searches from tsvector
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 14, 2024
1 parent 8a8815a commit c863357
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
7 changes: 6 additions & 1 deletion apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn empty_json() {
}

fn search(query: String, ctx: Context) {
wisp.log_notice("Searching for " <> query)
json.object([
#("exact-matches", {
queries.name_search(ctx.db, query)
Expand All @@ -31,8 +32,12 @@ fn search(query: String, ctx: Context) {
|> result.unwrap([])
|> json.preprocessed_array()
}),
#("searches", {
queries.search(ctx.db, query)
|> result.unwrap([])
|> json.preprocessed_array()
}),
])
// queries.search(ctx.db, query),
}

pub fn handle_get(req: Request, ctx: Context) {
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/data/model.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ fn compute_index(search_results: SearchResults) -> Index {
case search_results {
search_result.Start -> []
search_result.NoSearchResults -> []
search_result.SearchResults(exact, others) -> {
search_result.SearchResults(exact, others, searches) -> {
[]
|> insert_module_names(exact)
|> insert_module_names(others)
|> insert_module_names(searches)
|> list.map(fn(i) { pair.map_second(i, list.reverse) })
}
}
Expand Down
9 changes: 7 additions & 2 deletions apps/frontend/src/data/search_result.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ pub type SearchResult {

pub type SearchResults {
Start
SearchResults(exact_matches: List(SearchResult), matches: List(SearchResult))
SearchResults(
exact_matches: List(SearchResult),
matches: List(SearchResult),
searches: List(SearchResult),
)
NoSearchResults
}

Expand All @@ -42,10 +46,11 @@ pub fn decode_search_results(dyn) {
dynamic.decode1(fn(_) { NoSearchResults }, {
dynamic.field("error", dynamic.string)
}),
dynamic.decode2(
dynamic.decode3(
SearchResults,
dynamic.field("exact-matches", dynamic.list(decode_search_result)),
dynamic.field("matches", dynamic.list(decode_search_result)),
dynamic.field("searches", dynamic.list(decode_search_result)),
),
])(dyn)
}
Expand Down
6 changes: 4 additions & 2 deletions apps/frontend/src/frontend/strings.gleam
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub const gloogle_description = "Gloogle can search through all public gleam packages, to help you find the function you're looking for! Enter a type or a function name to get some results."

pub const exact_match = "Matched directly from the name of the functions, constants or types."
pub const exact_match = "Matched directly from the name of functions, constants or types."

pub const partial_match = "Partly matched from the signature of the functions, constants or types."
pub const partial_match = "Partly matched from the signature of functions, constants or types."

pub const searches_match = "Matched from a document search in functions, constants or types."

pub const retry_query = "Retry with a different query. You can match functions, types or constants names, as well as functions types directly!"

Expand Down
16 changes: 13 additions & 3 deletions apps/frontend/src/frontend/view/body/body.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,30 @@ pub fn body(model: Model) {
title: "Internal server error",
content: frontend_strings.internal_server_error,
)
search_result.SearchResults([], []) ->
search_result.SearchResults([], [], []) ->
empty_state(
image: images.shadow_lucy,
title: "No match found!",
content: frontend_strings.retry_query,
)
search_result.SearchResults(exact, others) ->
search_result.SearchResults(exact, others, searches) ->
s.search_results_wrapper([], [
sidebar(model.index),
s.items_wrapper([], [
match_title(exact, "Exact matches", frontend_strings.exact_match),
view_search_results(exact),
match_title(others, "Other matches", frontend_strings.partial_match),
match_title(
others,
"Signature matches",
frontend_strings.partial_match,
),
view_search_results(others),
match_title(
others,
"Searches matches",
frontend_strings.searches_match,
),
view_search_results(searches),
]),
])
},
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/frontend/view/navbar/navbar.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn navbar(model: Model) {
s.navbar([a.class("navbar")], [
case model.search_results {
search_result.Start -> h.div([], [])
search_result.NoSearchResults | search_result.SearchResults(_, _) ->
search_result.NoSearchResults | search_result.SearchResults(_, _, _) ->
s.navbar_search([], [
s.navbar_search_title([e.on_click(msg.Reset)], [
s.search_lucy([a.src("/images/lucy.svg")]),
Expand Down

0 comments on commit c863357

Please sign in to comment.