Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

fix: fall back to SQL search on OpenSearch errors (#16185)

Vishnu Narayanan4fb7c05f95e0
diff --git a/app/services/search_service.rb b/app/services/search_service.rb
index 439b3f115a7..165a0d13136 100644
--- a/app/services/search_service.rb
+++ b/app/services/search_service.rb
@@ -58,9 +58,10 @@ class SearchService
end
def advanced_search_with_fallback
- advanced_search
- rescue Faraday::ConnectionFailed, Searchkick::Error, Elasticsearch::Transport::Transport::Error => e
+ advanced_search.load
+ rescue Faraday::Error, Searchkick::Error, OpenSearch::Transport::Transport::Error => e
Rails.logger.warn("Elasticsearch unavailable, falling back to SQL search: #{e.message}")
+ ChatwootExceptionTracker.new(e, user: current_user, account: current_account).capture_exception
use_gin_search ? filter_messages_with_gin : filter_messages_with_like
end
diff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rb
index 1670482e9cd..f3bda55a340 100644
--- a/spec/services/search_service_spec.rb
+++ b/spec/services/search_service_spec.rb
@@ -497,12 +497,13 @@ describe SearchService do
describe '#advanced_search with filters', if: Message.respond_to?(:search) do
let(:params) { { q: 'test' } }
let(:search_type) { 'Message' }
+ let(:search_results) { instance_double(Searchkick::Relation, load: []) }
before do
allow(ChatwootApp).to receive(:advanced_search_allowed?).and_return(true)
allow(account).to receive(:feature_enabled?).and_call_original
allow(account).to receive(:feature_enabled?).with('advanced_search').and_return(true)
- allow(Message).to receive(:search).and_return([])
+ allow(Message).to receive(:search).and_return(search_results)
end
context 'when advanced_search feature flag is disabled' do
@@ -528,9 +529,11 @@ describe SearchService do
hash_including(
order: { created_at: :desc, id: { order: :desc, unmapped_type: 'long' } }
)
- ).and_return([])
+ ).and_return(search_results)
- search_service.perform
+ expect(search_results).to receive(:load).and_return(search_results)
+ expect(ChatwootExceptionTracker).not_to receive(:new)
+ expect(search_service.perform[:messages]).to eq(search_results)
end
end
@@ -550,7 +553,7 @@ describe SearchService do
sender_id: contact.id
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -567,7 +570,7 @@ describe SearchService do
sender_id: agent.id
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -581,7 +584,7 @@ describe SearchService do
hash_including(
where: hash_not_including(:sender_type, :sender_id)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -599,7 +602,7 @@ describe SearchService do
created_at: hash_including(gte: be_within(1.second).of(Limits.search_time_range_limit_days.days.ago))
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -616,7 +619,7 @@ describe SearchService do
created_at: hash_including(gte: be_within(1.second).of(Limits.search_time_range_limit_days.days.ago))
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -652,7 +655,7 @@ describe SearchService do
created_at: hash_including(gte: Time.zone.at(since_timestamp))
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -669,7 +672,7 @@ describe SearchService do
created_at: hash_including(lte: Time.zone.at(until_timestamp))
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -690,7 +693,7 @@ describe SearchService do
)
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -707,7 +710,7 @@ describe SearchService do
created_at: hash_including(lte: be_within(1.second).of(90.days.from_now))
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -729,7 +732,7 @@ describe SearchService do
hash_including(
where: hash_including(inbox_id: inbox2.id)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -744,7 +747,7 @@ describe SearchService do
hash_including(
where: hash_not_including(inbox_id: restricted_inbox.id)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
@@ -770,16 +773,23 @@ describe SearchService do
created_at: hash_including(gte: Time.zone.at(since_timestamp))
)
)
- ).and_return([])
+ ).and_return(search_results)
search_service.perform
end
end
end
- describe '#advanced_search_with_fallback' do
+ describe '#advanced_search_with_fallback', if: Message.respond_to?(:search) do
let(:params) { { q: 'test' } }
let(:search_type) { 'Message' }
+ let(:query) { instance_double(Searchkick::Query) }
+ let(:exception_tracker) { instance_double(ChatwootExceptionTracker, capture_exception: nil) }
+
+ before do
+ allow(ChatwootApp).to receive(:advanced_search_allowed?).and_return(true)
+ allow(Searchkick::Query).to receive(:new).and_return(query)
+ end
context 'when Elasticsearch is unavailable' do
it 'falls back to LIKE search when Elasticsearch connection fails' do
@@ -790,13 +800,16 @@ describe SearchService do
params = { q: 'test' }
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
- allow(search_service).to receive(:advanced_search).and_raise(Faraday::ConnectionFailed.new('Connection refused'))
+ error = Faraday::ConnectionFailed.new('Connection refused')
+ expect(query).to receive(:execute).and_raise(error)
+ expect(ChatwootExceptionTracker).to receive(:new).with(error, user: user, account: account).and_return(exception_tracker)
+ expect(exception_tracker).to receive(:capture_exception)
expect(search_service).to receive(:filter_messages_with_like).and_call_original
expect { search_service.perform }.not_to raise_error
end
- it 'falls back to GIN search when Elasticsearch is unavailable and GIN is enabled' do
+ it 'uses GIN search without querying Elasticsearch when GIN is enabled' do
allow(account).to receive(:feature_enabled?).and_call_original
allow(account).to receive(:feature_enabled?).with('advanced_search').and_return(true)
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
@@ -804,12 +817,45 @@ describe SearchService do
params = { q: 'test' }
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
- allow(search_service).to receive(:advanced_search).and_raise(Searchkick::Error.new('Elasticsearch unavailable'))
+ expect(search_service).not_to receive(:advanced_search)
+ expect(ChatwootExceptionTracker).not_to receive(:new)
expect(search_service).to receive(:filter_messages_with_gin).and_call_original
expect { search_service.perform }.not_to raise_error
end
+ it 'falls back to LIKE search when the request times out' do
+ allow(account).to receive(:feature_enabled?).and_call_original
+ allow(account).to receive(:feature_enabled?).with('advanced_search').and_return(true)
+ allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
+
+ search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
+
+ error = Faraday::TimeoutError.new('timeout')
+ expect(query).to receive(:execute).and_raise(error)
+ expect(ChatwootExceptionTracker).to receive(:new).with(error, user: user, account: account).and_return(exception_tracker)
+ expect(exception_tracker).to receive(:capture_exception)
+
+ expect(search_service).to receive(:filter_messages_with_like).and_call_original
+ expect { search_service.perform }.not_to raise_error
+ end
+
+ it 'falls back to LIKE search when OpenSearch returns an error response' do
+ allow(account).to receive(:feature_enabled?).and_call_original
+ allow(account).to receive(:feature_enabled?).with('advanced_search').and_return(true)
+ allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
+
+ search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
+
+ error = OpenSearch::Transport::Transport::Errors::ServiceUnavailable.new('[503] unavailable')
+ expect(query).to receive(:execute).and_raise(error)
+ expect(ChatwootExceptionTracker).to receive(:new).with(error, user: user, account: account).and_return(exception_tracker)
+ expect(exception_tracker).to receive(:capture_exception)
+
+ expect(search_service).to receive(:filter_messages_with_like).and_call_original
+ expect { search_service.perform }.not_to raise_error
+ end
+
it 'applies filters correctly in SQL fallback when Elasticsearch fails' do
allow(account).to receive(:feature_enabled?).and_call_original
allow(account).to receive(:feature_enabled?).with('advanced_search').and_return(true)
@@ -821,7 +867,10 @@ describe SearchService do
params = { q: 'test', from: "contact:#{test_contact.id}", since: 2.days.ago.to_i }
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
- allow(search_service).to receive(:advanced_search).and_raise(Faraday::ConnectionFailed.new('Connection refused'))
+ error = Searchkick::Error.new('Search unavailable')
+ expect(query).to receive(:execute).and_raise(error)
+ expect(ChatwootExceptionTracker).to receive(:new).with(error, user: user, account: account).and_return(exception_tracker)
+ expect(exception_tracker).to receive(:capture_exception)
results = search_service.perform[:messages]
expect(results).not_to be_empty