Skip to content

Commit

Permalink
feat: add fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Aug 1, 2024
1 parent 87b0375 commit ceb7f1b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
29 changes: 28 additions & 1 deletion apps/backend/src/backend/gleam/type_search/state.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn loop(msg: msg.Msg, state: State) -> actor.Next(msg.Msg, State) {
signature
|> parse.parse_function
|> result.nil_error
|> result.then(type_search.find(state.search, _, state.db))
|> result.then(permutation_search(state, _))
|> option.from_result
|> function.tap(fn(res) { process.send(subject, res) })
actor.continue(state)
Expand All @@ -59,6 +59,33 @@ fn loop(msg: msg.Msg, state: State) -> actor.Next(msg.Msg, State) {
actor.continue(state)
}

fn is_permutable(list: List(a), len: Int) {
case list {
_ if len > 4 -> False
[_, ..rest] -> is_permutable(rest, len + 1)
[] -> True
}
}

fn permutation_search(state: State, kind: parse.Kind) {
case kind {
parse.Function(params, return) -> {
let permutable = is_permutable(params, 0)
use <- bool.lazy_guard(when: !permutable, return: fn() {
type_search.find(state.search, kind, state.db)
})
Ok({
let permutations = list.permutations(params)
use permutation <- list.flat_map(permutations)
parse.Function(permutation, return)
|> type_search.find(state.search, _, state.db)
|> result.unwrap([])
})
}
_ -> Error(Nil)
}
}

fn compute_rows(
offset: Int,
db: pgo.Connection,
Expand Down
1 change: 0 additions & 1 deletion apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ pub fn find_similar_type_names(db: pgo.Connection, name: String) {
|> pgo.execute(db, [pgo.text(name)], dynamic.element(0, dynamic.string))
|> result.map_error(error.DatabaseError)
|> result.map(fn(r) { r.rows })
|> io.debug
}

pub fn name_search(db: pgo.Connection, query: String) {
Expand Down
1 change: 0 additions & 1 deletion apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import cors_builder as cors
import gleam/erlang/process
import gleam/http
import gleam/int
import gleam/io
import gleam/json
import gleam/list
import gleam/option
Expand Down

0 comments on commit ceb7f1b

Please sign in to comment.