feat: Separate indexing with the search feature (#12503)

With this change, the indexing would be separate from the search, so you
need to enable indexing on the cloud and run it. It should start
indexing the messages to ElasticSearch/OpenSearch. Once indexing is
completed, we can turn on the feature for the customer.


Make sure that the following is done when you deploy.
Set POSTGRES_STATEMENT_TIMEOUT=600s before you run the indexing.

1. Make sure that the account with advanced_search has
advanced_search_indexing enabled
```rb
Account.feature_advanced_search.each do |account|
  account.enable_features(:advanced_search_indexing)
  account.save!
end
```

2. Enable indexing for all accounts with paid subscription.
```rb
Account.where("custom_attributes ->> 'plan_name' IN (?)", ['Enterprise', 'Startups', 'Business']).each do |account|
account.enable_features(:advanced_search_indexing)
  account.save!
end
```

3. Run indexing for all the messages.
```rb
Message.reindex
```

Co-authored-by: Vishnu Narayanan <iamwishnu@gmail.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Pranav
2025-09-24 01:41:15 -07:00
committed by GitHub
parent 9f14e6abb6
commit eadbddaa9f
4 changed files with 30 additions and 4 deletions

View File

@@ -244,8 +244,16 @@ class Message < ApplicationRecord
def should_index? def should_index?
return false unless ChatwootApp.advanced_search_allowed? return false unless ChatwootApp.advanced_search_allowed?
return false unless account.feature_enabled?('advanced_search')
return false unless incoming? || outgoing? return false unless incoming? || outgoing?
# For Chatwoot Cloud:
# - Enable indexing only if the account is paid.
# - The `advanced_search_indexing` feature flag is used only in the cloud.
#
# For Self-hosted:
# - Adding an extra feature flag here would cause confusion.
# - If the user has configured Elasticsearch, enabling `advanced_search`
# should automatically work without any additional flags.
return false if ChatwootApp.chatwoot_cloud? && !account.feature_enabled?('advanced_search_indexing')
true true
end end

View File

@@ -209,3 +209,8 @@
display_name: SAML display_name: SAML
enabled: false enabled: false
premium: true premium: true
- name: advanced_search_indexing
display_name: Advanced Search Indexing
enabled: false
premium: true
chatwoot_internal: true

View File

@@ -15,6 +15,7 @@ class Enterprise::Billing::HandleStripeEventService
channel_email channel_email
channel_instagram channel_instagram
captain_integration captain_integration
advanced_search_indexing
].freeze ].freeze
# Additional features available starting with the Business plan # Additional features available starting with the Business plan

View File

@@ -621,7 +621,7 @@ RSpec.describe Message do
before do before do
allow(ChatwootApp).to receive(:advanced_search_allowed?).and_return(true) allow(ChatwootApp).to receive(:advanced_search_allowed?).and_return(true)
account.enable_features('advanced_search') account.enable_features('advanced_search_indexing')
end end
context 'when advanced search is not allowed globally' do context 'when advanced search is not allowed globally' do
@@ -634,9 +634,10 @@ RSpec.describe Message do
end end
end end
context 'when advanced search feature is not enabled for account' do context 'when advanced search feature is not enabled for account on chatwoot cloud' do
before do before do
account.disable_features('advanced_search') allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
account.disable_features('advanced_search_indexing')
end end
it 'returns false' do it 'returns false' do
@@ -644,6 +645,17 @@ RSpec.describe Message do
end end
end end
context 'when advanced search feature is not enabled for account on self-hosted' do
before do
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(false)
account.disable_features('advanced_search_indexing')
end
it 'returns true' do
expect(message.should_index?).to be true
end
end
context 'when message type is not incoming or outgoing' do context 'when message type is not incoming or outgoing' do
before do before do
message.message_type = 'activity' message.message_type = 'activity'