Commit c8aad010 authored by Dylan Griffith's avatar Dylan Griffith

Allow fallback to basic non-ES search

Setting query param basic_search=true on any search now should not use
Elasticsearch. Introduce Search::Elasticsearchable to not duplicate the
logic of whether or not to use elasticsearch.
parent 30b26d73
module Search
module Elasticsearchable
def use_elasticsearch?
return false if params[:basic_search]
::Gitlab::CurrentSettings.search_using_elasticsearch?(scope: elasticsearchable_scope)
end
def elasticsearchable_scope
raise NotImplementedError
end
end
end
...@@ -5,6 +5,7 @@ module EE ...@@ -5,6 +5,7 @@ module EE
module GlobalService module GlobalService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::Gitlab::Utils::StrongMemoize include ::Gitlab::Utils::StrongMemoize
include ::Search::Elasticsearchable
override :execute override :execute
def execute def execute
...@@ -19,8 +20,8 @@ module EE ...@@ -19,8 +20,8 @@ module EE
) )
end end
def use_elasticsearch? def elasticsearchable_scope
::Gitlab::CurrentSettings.search_using_elasticsearch?(scope: nil) nil
end end
def elastic_projects def elastic_projects
......
...@@ -5,9 +5,9 @@ module EE ...@@ -5,9 +5,9 @@ module EE
module GroupService module GroupService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
override :use_elasticsearch? override :elasticsearchable_scope
def use_elasticsearch? def elasticsearchable_scope
::Gitlab::CurrentSettings.search_using_elasticsearch?(scope: group) group
end end
override :elastic_projects override :elastic_projects
......
...@@ -4,6 +4,7 @@ module EE ...@@ -4,6 +4,7 @@ module EE
module Search module Search
module ProjectService module ProjectService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::Search::Elasticsearchable
override :execute override :execute
def execute def execute
...@@ -17,9 +18,8 @@ module EE ...@@ -17,9 +18,8 @@ module EE
) )
end end
# This method is used in the top-level SearchService, so cannot be in-lined into #execute def elasticsearchable_scope
def use_elasticsearch? project
::Gitlab::CurrentSettings.search_using_elasticsearch?(scope: project)
end end
end end
end end
......
...@@ -12,9 +12,9 @@ module EE ...@@ -12,9 +12,9 @@ module EE
::Gitlab::Elastic::SnippetSearchResults.new(current_user, params[:search], elastic_projects, nil, true) ::Gitlab::Elastic::SnippetSearchResults.new(current_user, params[:search], elastic_projects, nil, true)
end end
# This method is used in the top-level SearchService, so cannot be in-lined into #execute override :elasticsearchable_scope
def use_elasticsearch? def elasticsearchable_scope
::Gitlab::CurrentSettings.search_using_elasticsearch?(scope: nil) nil
end end
end end
end end
......
...@@ -15,7 +15,7 @@ describe Search::GlobalService do ...@@ -15,7 +15,7 @@ describe Search::GlobalService do
it_behaves_like 'EE search service shared examples', ::Gitlab::SearchResults, ::Gitlab::Elastic::SearchResults do it_behaves_like 'EE search service shared examples', ::Gitlab::SearchResults, ::Gitlab::Elastic::SearchResults do
let(:scope) { nil } let(:scope) { nil }
let(:service) { described_class.new(user, search: '*') } let(:service) { described_class.new(user, params) }
end end
context 'visibility', :elastic do context 'visibility', :elastic do
......
...@@ -16,7 +16,7 @@ describe Search::GroupService, :elastic do ...@@ -16,7 +16,7 @@ describe Search::GroupService, :elastic do
it_behaves_like 'EE search service shared examples', ::Gitlab::GroupSearchResults, ::Gitlab::Elastic::GroupSearchResults do it_behaves_like 'EE search service shared examples', ::Gitlab::GroupSearchResults, ::Gitlab::Elastic::GroupSearchResults do
let(:scope) { create(:group) } let(:scope) { create(:group) }
let(:service) { described_class.new(user, scope, search: '*') } let(:service) { described_class.new(user, scope, params) }
end end
describe 'group search' do describe 'group search' do
......
...@@ -14,7 +14,7 @@ describe Search::ProjectService do ...@@ -14,7 +14,7 @@ describe Search::ProjectService do
it_behaves_like 'EE search service shared examples', ::Gitlab::ProjectSearchResults, ::Gitlab::Elastic::ProjectSearchResults do it_behaves_like 'EE search service shared examples', ::Gitlab::ProjectSearchResults, ::Gitlab::Elastic::ProjectSearchResults do
let(:user) { scope.owner } let(:user) { scope.owner }
let(:scope) { create(:project) } let(:scope) { create(:project) }
let(:service) { described_class.new(scope, user, search: '*') } let(:service) { described_class.new(scope, user, params) }
end end
context 'visibility', :elastic do context 'visibility', :elastic do
......
...@@ -6,6 +6,6 @@ describe Search::SnippetService do ...@@ -6,6 +6,6 @@ describe Search::SnippetService do
it_behaves_like 'EE search service shared examples', ::Gitlab::SnippetSearchResults, ::Gitlab::Elastic::SnippetSearchResults do it_behaves_like 'EE search service shared examples', ::Gitlab::SnippetSearchResults, ::Gitlab::Elastic::SnippetSearchResults do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:scope) { nil } let(:scope) { nil }
let(:service) { described_class.new(user, search: '*') } let(:service) { described_class.new(user, params) }
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'EE search service shared examples' do |normal_results, elasticsearch_results| RSpec.shared_examples 'EE search service shared examples' do |normal_results, elasticsearch_results|
let(:params) { { search: '*' } }
describe '#use_elasticsearch?' do describe '#use_elasticsearch?' do
it 'delegates to Gitlab::CurrentSettings.search_using_elasticsearch?' do it 'delegates to Gitlab::CurrentSettings.search_using_elasticsearch?' do
expect(Gitlab::CurrentSettings) expect(Gitlab::CurrentSettings)
...@@ -10,6 +12,17 @@ RSpec.shared_examples 'EE search service shared examples' do |normal_results, el ...@@ -10,6 +12,17 @@ RSpec.shared_examples 'EE search service shared examples' do |normal_results, el
expect(service.use_elasticsearch?).to eq(:value) expect(service.use_elasticsearch?).to eq(:value)
end end
context 'when requesting basic_search' do
let(:params) { { search: '*', basic_search: 'true' } }
it 'returns false' do
expect(Gitlab::CurrentSettings)
.not_to receive(:search_using_elasticsearch?)
expect(service.use_elasticsearch?).to eq(false)
end
end
end end
describe '#execute' do describe '#execute' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment