diff --git a/apps/backend/src/backend/postgres/queries.gleam b/apps/backend/src/backend/postgres/queries.gleam index 509366d..8af12c8 100644 --- a/apps/backend/src/backend/postgres/queries.gleam +++ b/apps/backend/src/backend/postgres/queries.gleam @@ -421,29 +421,26 @@ pub fn upsert_package_type_fun_signature( pub fn name_search(db: pgo.Connection, query: String) { let query = pgo.text(query) - "SELECT DISTINCT ON (type_name, kind, module_name) * - FROM ( - SELECT - s.name type_name, - s.documentation, - s.kind, - s.metadata, - s.json_signature, - m.name module_name, - p.name, - r.version, - string_to_array(r.version, '.')::int[] AS ordering - FROM package_type_fun_signature s - JOIN package_module m - ON m.id = s.package_module_id - JOIN package_release r - ON m.package_release_id = r.id - JOIN package p - ON p.id = r.package_id - WHERE s.name = $1 - ORDER BY m.name ASC, ordering DESC - LIMIT 100 - ) i" + "SELECT DISTINCT ON (type_name, kind, module_name) + s.name type_name, + s.documentation, + s.kind, + s.metadata, + s.json_signature, + m.name module_name, + p.name, + r.version, + string_to_array(r.version, '.')::int[] AS ordering + FROM package_type_fun_signature s + JOIN package_module m + ON m.id = s.package_module_id + JOIN package_release r + ON m.package_release_id = r.id + JOIN package p + ON p.id = r.package_id + WHERE s.name = $1 + ORDER BY s.name, s.kind, m.name, ordering DESC + LIMIT 100" |> pgo.execute(db, [query], decode_type_search) |> result.map_error(error.DatabaseError) |> result.map(fn(r) { r.rows }) @@ -451,32 +448,32 @@ pub fn name_search(db: pgo.Connection, query: String) { pub fn content_search(db: pgo.Connection, query: String) { let query = pgo.text(query) - "SELECT DISTINCT ON (type_name, kind, module_name) * - FROM ( - SELECT - s.name type_name, - s.documentation, - s.kind, - s.metadata, - s.json_signature, - m.name module_name, - p.name, - r.version, - string_to_array(r.version, '.')::int[] AS ordering - FROM package_type_fun_signature s - JOIN package_module m - ON m.id = s.package_module_id - JOIN package_release r - ON m.package_release_id = r.id - JOIN package p - ON p.id = r.package_id - WHERE s.name ILIKE '%' || $1 || '%' - OR s.signature_ ILIKE '%' || $1 || '%' - OR replace(s.signature_, ' ', '') ILIKE '%' || $1 || '%' - OR replace(s.signature_, ' ', '') ILIKE '%' || replace($1, ' ', '') || '%' - ORDER BY m.name ASC, ordering DESC - LIMIT 100 - ) i" + "SELECT DISTINCT ON (type_name, kind, module_name) + s.name type_name, + s.documentation, + s.kind, + s.metadata, + s.json_signature, + m.name module_name, + p.name, + r.version, + string_to_array(r.version, '.')::int[] AS ordering + FROM package_type_fun_signature s + JOIN package_module m + ON m.id = s.package_module_id + JOIN package_release r + ON m.package_release_id = r.id + JOIN package p + ON p.id = r.package_id + WHERE s.name <> $1 + AND ( + s.name ILIKE '%' || $1 || '%' + OR s.signature_ ILIKE '%' || $1 || '%' + OR replace(s.signature_, ' ', '') ILIKE '%' || $1 || '%' + OR replace(s.signature_, ' ', '') ILIKE '%' || replace($1, ' ', '') || '%' + ) + ORDER BY s.name, s.kind, m.name, ordering DESC + LIMIT 100" |> pgo.execute(db, [query], decode_type_search) |> result.map_error(error.DatabaseError) |> result.map(fn(r) { r.rows }) diff --git a/apps/backend/src/backend/router.gleam b/apps/backend/src/backend/router.gleam index b70e348..d61e977 100644 --- a/apps/backend/src/backend/router.gleam +++ b/apps/backend/src/backend/router.gleam @@ -21,13 +21,19 @@ fn empty_json() { } fn search(query: String, ctx: Context) { - [ - queries.name_search(ctx.db, query), - queries.content_search(ctx.db, query), - queries.search(ctx.db, query), - ] - |> list.map(fn(r) { result.unwrap(r, []) }) - |> list.concat() + json.object([ + #("exact-matches", { + queries.name_search(ctx.db, query) + |> result.unwrap([]) + |> json.preprocessed_array() + }), + #("matches", { + queries.content_search(ctx.db, query) + |> result.unwrap([]) + |> json.preprocessed_array() + }), + ]) + // queries.search(ctx.db, query), } pub fn handle_get(req: Request, ctx: Context) { @@ -37,8 +43,7 @@ pub fn handle_get(req: Request, ctx: Context) { |> list.find(fn(item) { item.0 == "q" }) |> result.replace_error(error.EmptyError) |> result.map(fn(item) { search(item.1, ctx) }) - |> result.unwrap([]) - |> json.preprocessed_array() + |> result.unwrap(json.object([#("error", json.string("internal"))])) |> json.to_string_builder() |> wisp.json_response(200) }