Skip to content

Commit

Permalink
feat: add content search
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 58a8d4c commit 11d81de
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
33 changes: 33 additions & 0 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,39 @@ pub fn name_search(db: pgo.Connection, query: String) {
|> 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"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
|> result.map(fn(r) { r.rows })
}

fn decode_type_search(dyn) {
dynamic.decode8(
fn(a, b, c, d, e, f, g, h) {
Expand Down
10 changes: 7 additions & 3 deletions apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ fn empty_json() {
}

fn search(query: String, ctx: Context) {
let a = result.unwrap(queries.name_search(ctx.db, query), [])
let b = result.unwrap(queries.search(ctx.db, query), [])
list.append(a, b)
[
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()
}

pub fn handle_get(req: Request, ctx: Context) {
Expand Down

0 comments on commit 11d81de

Please sign in to comment.