fix: added email serach and ommitted fields
This commit is contained in:
parent
0c75776915
commit
a69ccf0ff9
2 changed files with 6 additions and 16 deletions
|
|
@ -6,17 +6,6 @@ defmodule Mv.Membership.Member do
|
||||||
require Ash.Query
|
require Ash.Query
|
||||||
import Ash.Expr
|
import Ash.Expr
|
||||||
|
|
||||||
@default_fields [
|
|
||||||
:first_name,
|
|
||||||
:last_name,
|
|
||||||
:email,
|
|
||||||
:phone_number,
|
|
||||||
:city,
|
|
||||||
:street,
|
|
||||||
:house_number,
|
|
||||||
:postal_code
|
|
||||||
]
|
|
||||||
|
|
||||||
postgres do
|
postgres do
|
||||||
table "members"
|
table "members"
|
||||||
repo Mv.Repo
|
repo Mv.Repo
|
||||||
|
|
@ -123,21 +112,22 @@ defmodule Mv.Membership.Member do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Action to handle fuzzy search on specific fields
|
||||||
read :search do
|
read :search do
|
||||||
argument :query, :string, allow_nil?: true
|
argument :query, :string, allow_nil?: true
|
||||||
argument :fields, {:array, :atom}, allow_nil?: true
|
|
||||||
argument :similarity_threshold, :float, allow_nil?: true
|
argument :similarity_threshold, :float, allow_nil?: true
|
||||||
|
|
||||||
prepare fn query, _ctx ->
|
prepare fn query, _ctx ->
|
||||||
q = Ash.Query.get_argument(query, :query) || ""
|
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
|
threshold = Ash.Query.get_argument(query, :similarity_threshold) || 0.2
|
||||||
|
|
||||||
if is_binary(q) and String.trim(q) != "" do
|
if is_binary(q) and String.trim(q) != "" do
|
||||||
q2 = String.trim(q)
|
q2 = String.trim(q)
|
||||||
pat = "%" <> q2 <> "%"
|
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
|
query
|
||||||
|> Ash.Query.filter(
|
|> Ash.Query.filter(
|
||||||
expr(
|
expr(
|
||||||
|
|
@ -147,6 +137,7 @@ defmodule Mv.Membership.Member do
|
||||||
contains(postal_code, ^q2) or
|
contains(postal_code, ^q2) or
|
||||||
contains(house_number, ^q2) or
|
contains(house_number, ^q2) or
|
||||||
contains(phone_number, ^q2) or
|
contains(phone_number, ^q2) or
|
||||||
|
contains(email, ^q2) or
|
||||||
contains(city, ^q2) or ilike(city, ^pat) or
|
contains(city, ^q2) or ilike(city, ^pat) or
|
||||||
fragment("? % first_name", ^q2) or
|
fragment("? % first_name", ^q2) or
|
||||||
fragment("? % last_name", ^q2) or
|
fragment("? % last_name", ^q2) or
|
||||||
|
|
|
||||||
|
|
@ -193,8 +193,7 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
if search_query && String.trim(search_query) != "" do
|
if search_query && String.trim(search_query) != "" do
|
||||||
query
|
query
|
||||||
|> Mv.Membership.Member.fuzzy_search(%{
|
|> Mv.Membership.Member.fuzzy_search(%{
|
||||||
query: search_query,
|
query: search_query
|
||||||
fields: [:first_name, :last_name, :street]
|
|
||||||
})
|
})
|
||||||
else
|
else
|
||||||
query
|
query
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue