Commit e9dcf27c authored by Changzheng Liu's avatar Changzheng Liu Committed by Kerri Miller

Ignore Issue and MR IID search out of range error

parent 43667b2f
---
title: Ignore Issue and MR IID search out of range error
merge_request: 49284
author:
type: fixed
......@@ -67,6 +67,7 @@ module Elastic
_name: context.name(self.es_type, :match, :search_terms),
fields: fields,
query: query,
lenient: true,
default_operator: default_operator
}
}],
......
......@@ -10,13 +10,9 @@ module Elastic
if query =~ /#(\d+)\z/
iid_query_hash(Regexp.last_match(1))
else
fields = %w(title^2 description)
# We can only allow searching the iid field if the query is
# just a number, otherwise Elasticsearch will error since this
# field is type integer.
fields << "iid^3" if query =~ /\A\d+\z/
# iid field can be added here as lenient option will
# pardon format errors, like integer out of range.
fields = %w(iid^3 title^2 description)
basic_query_hash(fields, query)
end
......
......@@ -10,13 +10,9 @@ module Elastic
if query =~ /\!(\d+)\z/
iid_query_hash(Regexp.last_match(1))
else
fields = %w(title^2 description)
# We can only allow searching the iid field if the query is
# just a number, otherwise Elasticsearch will error since this
# field is type integer.
fields << "iid^3" if query =~ /\A\d+\z/
# iid field can be added here as lenient option will
# pardon format errors, like integer out of range.
fields = %w(iid^3 title^2 description)
basic_query_hash(fields, query)
end
......
......@@ -204,7 +204,7 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need
describe 'issues' do
let(:scope) { 'issues' }
let!(:issue_1) { create(:issue, project: project_1, title: 'Hello world, here I am!', iid: 1) }
let!(:issue_1) { create(:issue, project: project_1, title: 'Hello world, here I am!', description: '20200623170000, see details in issue 287661', iid: 1) }
let!(:issue_2) { create(:issue, project: project_1, title: 'Issue Two', description: 'Hello world, here I am!', iid: 2) }
let!(:issue_3) { create(:issue, project: project_2, title: 'Issue Three', iid: 2) }
......@@ -245,6 +245,14 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need
expect(results.issues_count).to eq 1
end
it 'finds the issue with an out of integer range number in its description without exception' do
results = described_class.new(user, '20200623170000', limit_project_ids, public_and_internal_projects: false)
issues = results.objects('issues')
expect(issues).to contain_exactly(issue_1)
expect(results.issues_count).to eq 1
end
it 'returns empty list when search by invalid iid' do
results = described_class.new(user, '#222', limit_project_ids)
......@@ -546,6 +554,7 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need
source_project: project_1,
target_project: project_1,
title: 'Hello world, here I am!',
description: '20200623170000, see details in issue 287661',
iid: 1
)
@merge_request_2 = create(
......@@ -601,6 +610,14 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need
expect(results.merge_requests_count).to eq 1
end
it 'finds the MR with an out of integer range number in its description without exception' do
results = described_class.new(user, '20200623170000', limit_project_ids, public_and_internal_projects: false)
merge_requests = results.objects('merge_requests')
expect(merge_requests).to contain_exactly(@merge_request_1)
expect(results.merge_requests_count).to eq 1
end
it 'returns empty list when search by invalid iid' do
results = described_class.new(user, '#222', limit_project_ids)
......
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