Skip to content

Commit

Permalink
refactor: improve searches format
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 12, 2024
1 parent 11d81de commit 6ce4c5f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
95 changes: 46 additions & 49 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -421,62 +421,59 @@ 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 })
}

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 })
Expand Down
23 changes: 14 additions & 9 deletions apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down

0 comments on commit 6ce4c5f

Please sign in to comment.