feat: use gin index for message search (#11107)
This PR updates the search implementation to better utilize the GIN indexes. The option is toggled behind a feature flag for us to test it internally before making it available publicly
This commit is contained in:
@@ -35,12 +35,66 @@ class SearchService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter_messages
|
def filter_messages
|
||||||
@messages = current_account.messages.where(inbox_id: accessable_inbox_ids)
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
.where('messages.content ILIKE :search', search: "%#{search_query}%")
|
|
||||||
.where('created_at >= ?', 3.months.ago)
|
@messages = if use_gin_search
|
||||||
|
filter_messages_with_gin
|
||||||
|
else
|
||||||
|
filter_messages_with_like
|
||||||
|
end
|
||||||
|
|
||||||
|
log_search_performance(start_time)
|
||||||
|
@messages
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_messages_with_gin
|
||||||
|
base_query = message_base_query
|
||||||
|
|
||||||
|
if search_query.present?
|
||||||
|
# Use the @@ operator with to_tsquery for better GIN index utilization
|
||||||
|
# Convert search query to tsquery format with prefix matching
|
||||||
|
|
||||||
|
# Use this if we wanna match splitting the words
|
||||||
|
# split_query = search_query.split.map { |term| "#{term} | #{term}:*" }.join(' & ')
|
||||||
|
|
||||||
|
# This will do entire sentence matching using phrase distance operator
|
||||||
|
tsquery = search_query.split.join(' <-> ')
|
||||||
|
|
||||||
|
# Apply the text search using the GIN index
|
||||||
|
base_query.where('content @@ to_tsquery(?)', tsquery)
|
||||||
.reorder('created_at DESC')
|
.reorder('created_at DESC')
|
||||||
.page(params[:page])
|
.page(params[:page])
|
||||||
.per(15)
|
.per(15)
|
||||||
|
else
|
||||||
|
base_query.reorder('created_at DESC')
|
||||||
|
.page(params[:page])
|
||||||
|
.per(15)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_messages_with_like
|
||||||
|
message_base_query
|
||||||
|
.where('messages.content ILIKE :search', search: "%#{search_query}%")
|
||||||
|
.reorder('created_at DESC')
|
||||||
|
.page(params[:page])
|
||||||
|
.per(15)
|
||||||
|
end
|
||||||
|
|
||||||
|
def message_base_query
|
||||||
|
current_account.messages.where(inbox_id: accessable_inbox_ids)
|
||||||
|
.where('created_at >= ?', 3.months.ago)
|
||||||
|
end
|
||||||
|
|
||||||
|
def log_search_performance(start_time)
|
||||||
|
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
|
search_type = use_gin_search ? 'GIN' : 'ILIKE'
|
||||||
|
duration_ms = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
Rails.logger.info "[SearchService][#{current_account.id}] #{search_type} search query time: #{duration_ms}ms for #{search_query}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def use_gin_search
|
||||||
|
current_account.feature_enabled?('search_with_gin')
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_contacts
|
def filter_contacts
|
||||||
|
|||||||
@@ -154,3 +154,6 @@
|
|||||||
display_name: Contact Chatwoot Support Team
|
display_name: Contact Chatwoot Support Team
|
||||||
enabled: true
|
enabled: true
|
||||||
chatwoot_internal: true
|
chatwoot_internal: true
|
||||||
|
- name: search_with_gin
|
||||||
|
display_name: Search messages with GIN
|
||||||
|
enabled: false
|
||||||
|
|||||||
@@ -67,16 +67,61 @@ describe SearchService do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when message search' do
|
context 'when message search' do
|
||||||
|
let!(:message2) { create(:message, account: account, inbox: inbox, content: 'harry is cool') }
|
||||||
|
|
||||||
it 'searches across message content and return in created_at desc' do
|
it 'searches across message content and return in created_at desc' do
|
||||||
# random messages in another account
|
# random messages in another account
|
||||||
create(:message, content: 'Harry Potter is a wizard')
|
create(:message, content: 'Harry Potter is a wizard')
|
||||||
# random messsage in inbox with out access
|
# random messsage in inbox with out access
|
||||||
create(:message, account: account, inbox: create(:inbox, account: account), content: 'Harry Potter is a wizard')
|
create(:message, account: account, inbox: create(:inbox, account: account), content: 'Harry Potter is a wizard')
|
||||||
message2 = create(:message, account: account, inbox: inbox, content: 'harry is cool')
|
|
||||||
params = { q: 'Harry' }
|
params = { q: 'Harry' }
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Message')
|
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Message')
|
||||||
expect(search.perform[:messages].map(&:id)).to eq([message2.id, message.id])
|
expect(search.perform[:messages].map(&:id)).to eq([message2.id, message.id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'with feature flag for search type' do
|
||||||
|
let(:params) { { q: 'Harry' } }
|
||||||
|
let(:search_type) { 'Message' }
|
||||||
|
|
||||||
|
it 'uses LIKE search when search_with_gin feature is disabled' do
|
||||||
|
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)
|
||||||
|
|
||||||
|
expect(search_service).to receive(:filter_messages_with_like).and_call_original
|
||||||
|
expect(search_service).not_to receive(:filter_messages_with_gin)
|
||||||
|
|
||||||
|
search_service.perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses GIN search when search_with_gin feature is enabled' do
|
||||||
|
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
||||||
|
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||||
|
|
||||||
|
expect(search_service).to receive(:filter_messages_with_gin).and_call_original
|
||||||
|
expect(search_service).not_to receive(:filter_messages_with_like)
|
||||||
|
|
||||||
|
search_service.perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns same results regardless of search type' do
|
||||||
|
# Create test messages
|
||||||
|
message3 = create(:message, account: account, inbox: inbox, content: 'Harry is a wizard apprentice')
|
||||||
|
|
||||||
|
# Test with GIN search
|
||||||
|
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
||||||
|
gin_search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||||
|
gin_results = gin_search.perform[:messages].map(&:id)
|
||||||
|
|
||||||
|
# Test with LIKE search
|
||||||
|
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
||||||
|
like_search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||||
|
like_results = like_search.perform[:messages].map(&:id)
|
||||||
|
|
||||||
|
# Both search types should return the same messages
|
||||||
|
expect(gin_results).to match_array(like_results)
|
||||||
|
expect(gin_results).to include(message.id, message2.id, message3.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when conversation search' do
|
context 'when conversation search' do
|
||||||
@@ -99,4 +144,23 @@ describe SearchService do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#use_gin_search' do
|
||||||
|
let(:params) { { q: 'test' } }
|
||||||
|
|
||||||
|
it 'checks if the account has the search_with_gin feature enabled' do
|
||||||
|
expect(account).to receive(:feature_enabled?).with('search_with_gin')
|
||||||
|
search.send(:use_gin_search)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns true when search_with_gin feature is enabled' do
|
||||||
|
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
||||||
|
expect(search.send(:use_gin_search)).to be true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns false when search_with_gin feature is disabled' do
|
||||||
|
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
||||||
|
expect(search.send(:use_gin_search)).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user