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:
@@ -67,16 +67,61 @@ describe SearchService do
|
||||
end
|
||||
|
||||
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
|
||||
# random messages in another account
|
||||
create(:message, content: 'Harry Potter is a wizard')
|
||||
# random messsage in inbox with out access
|
||||
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' }
|
||||
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])
|
||||
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
|
||||
|
||||
context 'when conversation search' do
|
||||
@@ -99,4 +144,23 @@ describe SearchService do
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user