Skip to content

Commit

Permalink
fuzzy searching functionality backend
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushmau5 committed May 7, 2024
1 parent 8899da5 commit 040b9e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# log lines will bump the console prompt down.
if function_exported?(Mix, :__info__, 1) and Mix.env() == :dev do
# if statement guards you from running it in prod, which could result in loss of logs.
Logger.configure_backend(:console, device: Process.group_leader())
end
11 changes: 11 additions & 0 deletions lib/accumulator/notes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ defmodule Accumulator.Notes do
|> Repo.delete()
end

def search(search_term) do
like = "%#{search_term}%"

query =
from(n in Note,
where: like(n.text, ^like)
)

Repo.all(query) |> group_and_sort_notes()
end

defp group_and_sort_notes(notes) do
notes
|> Enum.group_by(fn %{inserted_at: inserted_at} ->
Expand Down
13 changes: 13 additions & 0 deletions priv/repo/migrations/20240505175513_notes_search.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Accumulator.Repo.Migrations.NotesSearch do
use Ecto.Migration

def change do
execute("""
CREATE EXTENSION IF NOT EXISTS pg_trgm;
""")

execute("""
CREATE INDEX notes_searchable_idx ON notes USING GIN (text gin_trgm_ops);
""")
end
end

0 comments on commit 040b9e7

Please sign in to comment.