Implement fuzzy search #187

Merged
carla merged 10 commits from feature/162_fuzzy_search into main 2025-11-12 13:10:32 +01:00
3 changed files with 31 additions and 1 deletions
Showing only changes of commit 3481b9dadf - Show all commits

View file

@ -138,6 +138,7 @@ defmodule Mv.Membership.Member do
contains(postal_code, ^q2) or
contains(house_number, ^q2) or
contains(phone_number, ^q2) or
contains(city, ^q2) or ilike(city, ^pat) or
fragment("? % first_name", ^q2) or
fragment("? % last_name", ^q2) or
fragment("? % street", ^q2) or

View file

@ -192,7 +192,10 @@ defmodule MvWeb.MemberLive.Index do
defp apply_search_filter(query, search_query) do
if search_query && String.trim(search_query) != "" do
query
|> filter(expr(fragment("search_vector @@ plainto_tsquery('simple', ?)", ^search_query)))
|> Mv.Membership.Member.fuzzy_search(%{
query: search_query,
fields: [:first_name, :last_name, :street]
carla marked this conversation as resolved

these fields are passed to fuzzy_serach->search function but don't have any effect, as the search function ignores them.

these fields are passed to `fuzzy_serach`->`search` function but don't have any effect, as the `search` function ignores them.
})
else
query
end

View file

@ -161,4 +161,30 @@ defmodule Mv.Membership.FuzzySearchTest do
ids = Enum.map(result, & &1.id)
assert s1.id in ids
end
test "substring in city matches mid-string" do
{:ok, b} =
Mv.Membership.create_member(%{
first_name: "City",
last_name: "One",
email: "city1@example.com",
city: "Berlin"
})
{:ok, _m} =
Mv.Membership.create_member(%{
first_name: "City",
last_name: "Two",
email: "city2@example.com",
city: "München"
})
result =
Mv.Membership.Member
|> Mv.Membership.Member.fuzzy_search(%{query: "erl"})
|> Ash.read!()
ids = Enum.map(result, & &1.id)
assert b.id in ids
end
end