From e3020fbe2ccaabf1d596c01a629bd93b3094e920 Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 19 Sep 2025 00:09:17 -0700 Subject: [PATCH] fix: Use case sensitive filter for phone_numbers (#12470) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The contact filter APIs were timing out due to the case‑insensitive filter. There is no index for lower case phone numbers, so it would perform a table scan, potentially examining 8 million records or more at a time. This change should fix the issue. I am changing the filter to use direct comparison without lower‑case. **Previous:** ```sql SELECT contacts.* FROM contacts WHERE contacts.account_id = $1 AND ( LOWER(contacts.phone_number) = '' OR LOWER(contacts.phone_number) = '' ) ORDER BY contacts.created_at DESC NULLS LAST LIMIT $2 OFFSET $3 ``` **Updated:** ```sql SELECT contacts.* FROM contacts WHERE contacts.account_id = $1 AND ( contacts.phone_number = '' OR contacts.phone_number = '' ) ORDER BY contacts.created_at DESC NULLS LAST LIMIT $2 OFFSET $3 ``` Fixes: https://linear.app/chatwoot/issue/CW-5582/contact-filter-timing-out --- Gemfile.lock | 2 +- lib/filters/filter_keys.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f9e253e6c..16e57d4f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -727,7 +727,7 @@ GEM retriable (3.1.2) reverse_markdown (2.1.1) nokogiri - rexml (3.4.1) + rexml (3.4.4) rotp (6.3.0) rspec-core (3.13.0) rspec-support (~> 3.13.0) diff --git a/lib/filters/filter_keys.yml b/lib/filters/filter_keys.yml index 902b5f219..626a0cbd5 100644 --- a/lib/filters/filter_keys.yml +++ b/lib/filters/filter_keys.yml @@ -4,7 +4,7 @@ # 3. Automation Filters (app/services/automation_rules/conditions_filter_service.rb), (app/services/automation_rules/condition_validation_service.rb) -# Format +# Format # - Parent Key (conversation, contact, messages) # - Key (attribute_name) # - attribute_type: "standard" : supported ["standard", "additional_attributes (only for conversations and messages)"] @@ -138,7 +138,7 @@ contacts: - "does_not_contain" phone_number: attribute_type: "standard" - data_type: "text_case_insensitive" + data_type: "text" # Text is not explicity defined in filters, default filter will be used filter_operators: - "equal_to" - "not_equal_to"