Commit a73020ba authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '197878-unify-es_type-as-symbol-or-string' into 'master'

Resolve "Unify es_type as symbol or string"

See merge request gitlab-org/gitlab!29620
parents 35e2e428 a80dc339
......@@ -5,18 +5,18 @@ module Elastic
module GitClassProxy
SHA_REGEX = /\A[0-9a-f]{5,40}\z/i.freeze
def elastic_search(query, type: :all, page: 1, per: 20, options: {})
def elastic_search(query, type: 'all', page: 1, per: 20, options: {})
results = { blobs: [], commits: [] }
case type.to_sym
when :all
case type
when 'all'
results[:blobs] = search_blob(query, page: page, per: per, options: options)
results[:commits] = search_commit(query, page: page, per: per, options: options)
results[:wiki_blobs] = search_blob(query, type: :wiki_blob, page: page, per: per, options: options)
when :commit
results[:wiki_blobs] = search_blob(query, type: 'wiki_blob', page: page, per: per, options: options)
when 'commit'
results[:commits] = search_commit(query, page: page, per: per, options: options)
when :blob, :wiki_blob
results[type.to_s.pluralize.to_sym] = search_blob(query, type: type, page: page, per: per, options: options)
when 'blob', 'wiki_blob'
results[type.pluralize.to_sym] = search_blob(query, type: type, page: page, per: per, options: options)
end
results
......@@ -103,7 +103,7 @@ module Elastic
}
end
def search_blob(query, type: :blob, page: 1, per: 20, options: {})
def search_blob(query, type: 'blob', page: 1, per: 20, options: {})
page ||= 1
query = ::Gitlab::Search::Query.new(query) do
......@@ -170,7 +170,7 @@ module Elastic
}
end
options[:project_ids] = repository_ids.map { |id| id.to_s[/\d+/].to_i } if type.to_sym == :wiki_blob && repository_ids.any?
options[:project_ids] = repository_ids.map { |id| id.to_s[/\d+/].to_i } if type == 'wiki_blob' && repository_ids.any?
res = search(query_hash, options)
......@@ -184,8 +184,6 @@ module Elastic
#
# @return [Kaminari::PaginatableArray]
def elastic_search_and_wrap(query, type:, page: 1, per: 20, options: {}, &blk)
type = type.to_s
response = elastic_search(
query,
type: type,
......
......@@ -15,7 +15,7 @@ module Elastic
"project_#{project_id}"
end
def elastic_search(query, type: :all, page: 1, per: 20, options: {})
def elastic_search(query, type: 'all', page: 1, per: 20, options: {})
options = repository_specific_options(options)
self.class.elastic_search(query, type: type, page: page, per: per, options: options)
......
......@@ -11,7 +11,7 @@ module Elastic
# @return [Kaminari::PaginatableArray]
def find_commits_by_message_with_elastic(query, page: 1, per_page: 20, options: {})
elastic_search_and_wrap(query, type: :commit, page: page, per: per_page, options: options) do |result, project|
elastic_search_and_wrap(query, type: 'commit', page: page, per: per_page, options: options) do |result, project|
raw_commit = Gitlab::Git::Commit.new(
project.repository.raw,
prepare_commit(result['_source']['commit']),
......
......@@ -9,7 +9,7 @@ module Elastic
delegate :id, to: :project, prefix: true
def find_commits_by_message_with_elastic(query, page: 1, per_page: 20)
response = elastic_search(query, type: :commit, page: page, per: per_page)[:commits][:results]
response = elastic_search(query, type: 'commit', page: page, per: per_page)[:commits][:results]
commits = response.map do |result|
commit result["_source"]["commit"]["sha"]
......
......@@ -23,7 +23,7 @@ describe 'Repository index', :elastic do
end
def indexed_file_paths_for(term)
blobs = Repository.elastic_search(term, type: :blob)[:blobs][:results].response
blobs = Repository.elastic_search(term, type: 'blob')[:blobs][:results].response
blobs.map do |blob|
blob['_source']['blob']['path']
end
......
......@@ -26,7 +26,7 @@ describe Elastic::Latest::GitInstanceProxy do
describe '#elastic_search' do
let(:params) do
{
type: :fake_type,
type: 'fake_type',
page: 2,
per: 30,
options: { foo: :bar }
......
......@@ -82,7 +82,7 @@ describe Gitlab::Elastic::Indexer do
def indexed_wiki_paths_for(term)
blobs = ProjectWiki.elastic_search(
term,
type: :wiki_blob
type: 'wiki_blob'
)[:wiki_blobs][:results].response
blobs.map do |blob|
......@@ -216,7 +216,7 @@ describe Gitlab::Elastic::Indexer do
def indexed_file_paths_for(term)
blobs = Repository.elastic_search(
term,
type: :blob
type: 'blob'
)[:blobs][:results].response
blobs.map do |blob|
......
......@@ -18,8 +18,8 @@ describe ProjectWiki, :elastic do
end
it "searches wiki page" do
expect(project.wiki.elastic_search('term1', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(1)
expect(project.wiki.elastic_search('term1 | term2', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(2)
expect(project.wiki.elastic_search('term1', type: 'wiki_blob')[:wiki_blobs][:total_count]).to eq(1)
expect(project.wiki.elastic_search('term1 | term2', type: 'wiki_blob')[:wiki_blobs][:total_count]).to eq(2)
end
it 'indexes' do
......@@ -29,7 +29,7 @@ describe ProjectWiki, :elastic do
end
it 'can delete wiki pages' do
expect(project.wiki.elastic_search('term2', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(1)
expect(project.wiki.elastic_search('term2', type: 'wiki_blob')[:wiki_blobs][:total_count]).to eq(1)
Sidekiq::Testing.inline! do
project.wiki.find_page('omega_page').delete
......@@ -44,6 +44,6 @@ describe ProjectWiki, :elastic do
ensure_elasticsearch_index!
end
expect(project.wiki.elastic_search('term2', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(0)
expect(project.wiki.elastic_search('term2', type: 'wiki_blob')[:wiki_blobs][:total_count]).to eq(0)
end
end
......@@ -54,10 +54,10 @@ describe Repository, :elastic do
blobs, commits = results.partition { |result| result['_source']['blob'].present? }
case type
when :blob
when 'blob'
expect(blobs).not_to be_empty
expect(commits).to be_empty
when :commit
when 'commit'
expect(blobs).to be_empty
expect(commits).not_to be_empty
else
......@@ -71,10 +71,10 @@ describe Repository, :elastic do
project = create :project, :repository
index!(project)
search_and_check!(Repository, '-foo', type: :blob)
search_and_check!(Repository, '-foo', type: :commit)
search_and_check!(project.repository, '-foo', type: :blob)
search_and_check!(project.repository, '-foo', type: :commit)
search_and_check!(Repository, '-foo', type: 'blob')
search_and_check!(Repository, '-foo', type: 'commit')
search_and_check!(project.repository, '-foo', type: 'blob')
search_and_check!(project.repository, '-foo', type: 'commit')
end
describe 'class method find_commits_by_message_with_elastic', :sidekiq_might_not_need_inline do
......
......@@ -121,7 +121,7 @@ describe ElasticIndexerWorker, :elastic do
expect do
subject.perform("index", 'Project', object.id, object.es_id)
end.to raise_error
end.to raise_error(Elastic::IndexRecordService::ImportError)
end
it 'ignores Elasticsearch::Transport::Transport::Errors::NotFound error' 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