Commit 847b0b60 authored by Dmitry Gruzd's avatar Dmitry Gruzd

Add a FF for ES default_operator change

parent 3630fe77
---
name: elasticsearch_use_or_default_operator
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/41664
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/246500
group: group::global search
type: development
default_enabled: false
...@@ -33,6 +33,12 @@ module Elastic ...@@ -33,6 +33,12 @@ module Elastic
private private
def default_operator
return :or if Feature.enabled?(:elasticsearch_use_or_default_operator)
:and
end
def highlight_options(fields) def highlight_options(fields)
es_fields = fields.map { |field| field.split('^').first }.each_with_object({}) do |field, memo| es_fields = fields.map { |field| field.split('^').first }.each_with_object({}) do |field, memo|
memo[field.to_sym] = {} memo[field.to_sym] = {}
...@@ -51,7 +57,7 @@ module Elastic ...@@ -51,7 +57,7 @@ module Elastic
simple_query_string: { simple_query_string: {
fields: fields, fields: fields,
query: query, query: query,
default_operator: :and default_operator: default_operator
} }
}], }],
filter: [{ filter: [{
......
...@@ -8,7 +8,7 @@ RSpec.describe Project, :elastic do ...@@ -8,7 +8,7 @@ RSpec.describe Project, :elastic do
end end
context 'when limited indexing is on' do context 'when limited indexing is on' do
let_it_be(:project) { create :project, name: 'test1' } let_it_be(:project) { create :project, name: 'main_project' }
before do before do
stub_ee_application_setting(elasticsearch_limit_indexing: true) stub_ee_application_setting(elasticsearch_limit_indexing: true)
...@@ -47,14 +47,14 @@ RSpec.describe Project, :elastic do ...@@ -47,14 +47,14 @@ RSpec.describe Project, :elastic do
it 'only indexes enabled projects' do it 'only indexes enabled projects' do
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
create :project, path: 'test2', description: 'awesome project' create :project, path: 'test_two', description: 'awesome project'
create :project create :project
ensure_elasticsearch_index! ensure_elasticsearch_index!
end end
expect(described_class.elastic_search('test*', options: { project_ids: :any }).total_count).to eq(1) expect(described_class.elastic_search('main_pro*', options: { project_ids: :any }).total_count).to eq(1)
expect(described_class.elastic_search('test2', options: { project_ids: :any }).total_count).to eq(0) expect(described_class.elastic_search('test_two', options: { project_ids: :any }).total_count).to eq(0)
end end
end end
...@@ -84,9 +84,60 @@ RSpec.describe Project, :elastic do ...@@ -84,9 +84,60 @@ RSpec.describe Project, :elastic do
end end
expect(described_class.elastic_search('group_test*', options: { project_ids: :any }).total_count).to eq(2) expect(described_class.elastic_search('group_test*', options: { project_ids: :any }).total_count).to eq(2)
expect(described_class.elastic_search('group_test3', options: { project_ids: :any }).total_count).to eq(1) expect(described_class.elastic_search('"group_test3"', options: { project_ids: :any }).total_count).to eq(1)
expect(described_class.elastic_search('group_test2', options: { project_ids: :any }).total_count).to eq(0) expect(described_class.elastic_search('"group_test2"', options: { project_ids: :any }).total_count).to eq(0)
expect(described_class.elastic_search('group_test4', options: { project_ids: :any }).total_count).to eq(0) expect(described_class.elastic_search('"group_test4"', options: { project_ids: :any }).total_count).to eq(0)
end
context 'default_operator' do
RSpec.shared_examples 'use correct default_operator' do |operator|
before do
Sidekiq::Testing.inline! do
create :project, name: 'project1', group: group, description: 'test foo'
create :project, name: 'project2', group: group, description: 'test'
create :project, name: 'project3', group: group, description: 'foo'
ensure_elasticsearch_index!
end
end
it 'uses correct operator' do
count_for_or = described_class.elastic_search('test | foo', options: { project_ids: :any }).total_count
expect(count_for_or).to be > 0
count_for_and = described_class.elastic_search('test + foo', options: { project_ids: :any }).total_count
expect(count_for_and).to be > 0
expect(count_for_or).not_to be equal(count_for_and)
expected_count = case operator
when :or
count_for_or
when :and
count_for_and
else
raise ArgumentError, 'Invalid operator'
end
expect(described_class.elastic_search('test foo', options: { project_ids: :any }).total_count).to eq(expected_count)
end
end
context 'feature flag is enabled' do
before do
stub_feature_flags(elasticsearch_use_or_default_operator: true)
end
include_examples 'use correct default_operator', :or
end
context 'feature flag is disabled' do
before do
stub_feature_flags(elasticsearch_use_or_default_operator: false)
end
include_examples 'use correct default_operator', :and
end
end end
end end
end end
...@@ -125,12 +176,12 @@ RSpec.describe Project, :elastic do ...@@ -125,12 +176,12 @@ RSpec.describe Project, :elastic do
ensure_elasticsearch_index! ensure_elasticsearch_index!
end end
expect(described_class.elastic_search('test1', options: { project_ids: project_ids }).total_count).to eq(1) expect(described_class.elastic_search('"test1"', options: { project_ids: project_ids }).total_count).to eq(1)
expect(described_class.elastic_search('test2', options: { project_ids: project_ids }).total_count).to eq(1) expect(described_class.elastic_search('"test2"', options: { project_ids: project_ids }).total_count).to eq(1)
expect(described_class.elastic_search('awesome', options: { project_ids: project_ids }).total_count).to eq(1) expect(described_class.elastic_search('"awesome"', options: { project_ids: project_ids }).total_count).to eq(1)
expect(described_class.elastic_search('test*', options: { project_ids: project_ids }).total_count).to eq(2) expect(described_class.elastic_search('test*', options: { project_ids: project_ids }).total_count).to eq(2)
expect(described_class.elastic_search('test*', options: { project_ids: :any }).total_count).to eq(3) expect(described_class.elastic_search('test*', options: { project_ids: :any }).total_count).to eq(3)
expect(described_class.elastic_search('someone_elses_project', options: { project_ids: project_ids }).total_count).to eq(0) expect(described_class.elastic_search('"someone_elses_project"', options: { project_ids: project_ids }).total_count).to eq(0)
end end
it "finds partial matches in project names" do it "finds partial matches in project names" do
......
...@@ -20,13 +20,13 @@ RSpec.describe Search::GroupService, :elastic do ...@@ -20,13 +20,13 @@ RSpec.describe Search::GroupService, :elastic do
end end
describe 'group search' do describe 'group search' do
let(:term) { "Project Name" } let(:term) { "RandomName" }
let(:nested_group) { create(:group, :nested) } let(:nested_group) { create(:group, :nested) }
# These projects shouldn't be found # These projects shouldn't be found
let(:outside_project) { create(:project, :public, name: "Outside #{term}") } let(:outside_project) { create(:project, :public, name: "Outside #{term}") }
let(:private_project) { create(:project, :private, namespace: nested_group, name: "Private #{term}" )} let(:private_project) { create(:project, :private, namespace: nested_group, name: "Private #{term}" )}
let(:other_project) { create(:project, :public, namespace: nested_group, name: term.reverse) } let(:other_project) { create(:project, :public, namespace: nested_group, name: 'OtherProject') }
# These projects should be found # These projects should be found
let(:project1) { create(:project, :internal, namespace: nested_group, name: "Inner #{term} 1") } let(:project1) { create(:project, :internal, namespace: nested_group, name: "Inner #{term} 1") }
......
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