fix: added email serach and ommitted fields

This commit is contained in:
carla 2025-11-12 11:55:35 +01:00
parent 0c75776915
commit a69ccf0ff9
2 changed files with 6 additions and 16 deletions

View file

@ -6,17 +6,6 @@ defmodule Mv.Membership.Member do
require Ash.Query
import Ash.Expr
@default_fields [
:first_name,
:last_name,
:email,
:phone_number,
:city,
:street,
:house_number,
:postal_code
]
postgres do
table "members"
repo Mv.Repo
@ -123,21 +112,22 @@ defmodule Mv.Membership.Member do
end
end
# Action to handle fuzzy search on specific fields
read :search do
argument :query, :string, allow_nil?: true
argument :fields, {:array, :atom}, allow_nil?: true
argument :similarity_threshold, :float, allow_nil?: true
prepare fn query, _ctx ->
q = Ash.Query.get_argument(query, :query) || ""
fields = Ash.Query.get_argument(query, :fields) || @default_fields
# 0.2 as similarity threshold (recommended) - lower value can lead to more results but also to more unspecific results
threshold = Ash.Query.get_argument(query, :similarity_threshold) || 0.2
if is_binary(q) and String.trim(q) != "" do
q2 = String.trim(q)
pat = "%" <> q2 <> "%"
# FTS as main filter and fuzzy search just fo first name, last name and strees
# FTS as main filter and fuzzy search just for first name, last name and strees
query
|> Ash.Query.filter(
expr(
@ -147,6 +137,7 @@ defmodule Mv.Membership.Member do
contains(postal_code, ^q2) or
contains(house_number, ^q2) or
contains(phone_number, ^q2) or
contains(email, ^q2) or
contains(city, ^q2) or ilike(city, ^pat) or
fragment("? % first_name", ^q2) or
fragment("? % last_name", ^q2) or

View file

@ -193,8 +193,7 @@ defmodule MvWeb.MemberLive.Index do
if search_query && String.trim(search_query) != "" do
query
|> Mv.Membership.Member.fuzzy_search(%{
query: search_query,
fields: [:first_name, :last_name, :street]
query: search_query
})
else
query