fix: custom field substring search - pass id as parameter
All checks were successful
continuous-integration/drone/push Build is passing

Fragment 'member_id = id' did not resolve correctly. Now passes id as
Ash expression. Also changed LIKE to ILIKE for case-insensitive search.
This commit is contained in:
Moritz 2025-12-11 14:04:13 +01:00
parent ca5fad0dcc
commit 00fe471bc0
2 changed files with 160 additions and 3 deletions

View file

@ -561,14 +561,16 @@ defmodule Mv.Membership.Member do
)
end
# Builds search filter for custom field values using LIKE on JSONB
# Note: LIKE on JSONB is not index-optimized, may be slow with many custom fields
# Builds search filter for custom field values using ILIKE on JSONB
# Note: ILIKE on JSONB is not index-optimized, may be slow with many custom fields
# This is a fallback for substring matching in custom fields (e.g., phone numbers)
# Uses ->> operator which always returns TEXT directly (no need for -> + ::text fallback)
# Important: `id` must be passed as parameter to correctly reference the outer members table
defp build_custom_field_filter(pattern) do
expr(
fragment(
"EXISTS (SELECT 1 FROM custom_field_values WHERE member_id = id AND (value->>'_union_value' LIKE ? OR value->>'value' LIKE ?))",
"EXISTS (SELECT 1 FROM custom_field_values WHERE member_id = ? AND (value->>'_union_value' ILIKE ? OR value->>'value' ILIKE ?))",
id,
^pattern,
^pattern
)