Commit a80dc339 authored by Terri Chu's avatar Terri Chu

Switch es_type to always use strings instead of symbols

Change es_type default args and args sent into the type field to strings
Update all checks to expect and use type field as string
Change tests to send in strings for type field
parent f8621f9c
...@@ -5,18 +5,18 @@ module Elastic ...@@ -5,18 +5,18 @@ module Elastic
module GitClassProxy module GitClassProxy
SHA_REGEX = /\A[0-9a-f]{5,40}\z/i.freeze 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: [] } results = { blobs: [], commits: [] }
case type.to_sym case type
when :all when 'all'
results[:blobs] = search_blob(query, page: page, per: per, options: options) results[:blobs] = search_blob(query, page: page, per: per, options: options)
results[:commits] = search_commit(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) results[:wiki_blobs] = search_blob(query, type: 'wiki_blob', page: page, per: per, options: options)
when :commit when 'commit'
results[:commits] = search_commit(query, page: page, per: per, options: options) results[:commits] = search_commit(query, page: page, per: per, options: options)
when :blob, :wiki_blob when 'blob', 'wiki_blob'
results[type.to_s.pluralize.to_sym] = search_blob(query, type: type, page: page, per: per, options: options) results[type.pluralize.to_sym] = search_blob(query, type: type, page: page, per: per, options: options)
end end
results results
...@@ -103,7 +103,7 @@ module Elastic ...@@ -103,7 +103,7 @@ module Elastic
} }
end 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 page ||= 1
query = ::Gitlab::Search::Query.new(query) do query = ::Gitlab::Search::Query.new(query) do
...@@ -170,7 +170,7 @@ module Elastic ...@@ -170,7 +170,7 @@ module Elastic
} }
end 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) res = search(query_hash, options)
...@@ -184,8 +184,6 @@ module Elastic ...@@ -184,8 +184,6 @@ module Elastic
# #
# @return [Kaminari::PaginatableArray] # @return [Kaminari::PaginatableArray]
def elastic_search_and_wrap(query, type:, page: 1, per: 20, options: {}, &blk) def elastic_search_and_wrap(query, type:, page: 1, per: 20, options: {}, &blk)
type = type.to_s
response = elastic_search( response = elastic_search(
query, query,
type: type, type: type,
......
...@@ -15,7 +15,7 @@ module Elastic ...@@ -15,7 +15,7 @@ module Elastic
"project_#{project_id}" "project_#{project_id}"
end 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) options = repository_specific_options(options)
self.class.elastic_search(query, type: type, page: page, per: per, options: options) self.class.elastic_search(query, type: type, page: page, per: per, options: options)
......
...@@ -11,7 +11,7 @@ module Elastic ...@@ -11,7 +11,7 @@ module Elastic
# @return [Kaminari::PaginatableArray] # @return [Kaminari::PaginatableArray]
def find_commits_by_message_with_elastic(query, page: 1, per_page: 20, options: {}) 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( raw_commit = Gitlab::Git::Commit.new(
project.repository.raw, project.repository.raw,
prepare_commit(result['_source']['commit']), prepare_commit(result['_source']['commit']),
......
...@@ -9,7 +9,7 @@ module Elastic ...@@ -9,7 +9,7 @@ module Elastic
delegate :id, to: :project, prefix: true delegate :id, to: :project, prefix: true
def find_commits_by_message_with_elastic(query, page: 1, per_page: 20) 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| commits = response.map do |result|
commit result["_source"]["commit"]["sha"] commit result["_source"]["commit"]["sha"]
......
...@@ -23,7 +23,7 @@ describe 'Repository index', :elastic do ...@@ -23,7 +23,7 @@ describe 'Repository index', :elastic do
end end
def indexed_file_paths_for(term) 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| blobs.map do |blob|
blob['_source']['blob']['path'] blob['_source']['blob']['path']
end end
......
...@@ -26,7 +26,7 @@ describe Elastic::Latest::GitInstanceProxy do ...@@ -26,7 +26,7 @@ describe Elastic::Latest::GitInstanceProxy do
describe '#elastic_search' do describe '#elastic_search' do
let(:params) do let(:params) do
{ {
type: :fake_type, type: 'fake_type',
page: 2, page: 2,
per: 30, per: 30,
options: { foo: :bar } options: { foo: :bar }
......
...@@ -82,7 +82,7 @@ describe Gitlab::Elastic::Indexer do ...@@ -82,7 +82,7 @@ describe Gitlab::Elastic::Indexer do
def indexed_wiki_paths_for(term) def indexed_wiki_paths_for(term)
blobs = ProjectWiki.elastic_search( blobs = ProjectWiki.elastic_search(
term, term,
type: :wiki_blob type: 'wiki_blob'
)[:wiki_blobs][:results].response )[:wiki_blobs][:results].response
blobs.map do |blob| blobs.map do |blob|
...@@ -216,7 +216,7 @@ describe Gitlab::Elastic::Indexer do ...@@ -216,7 +216,7 @@ describe Gitlab::Elastic::Indexer do
def indexed_file_paths_for(term) def indexed_file_paths_for(term)
blobs = Repository.elastic_search( blobs = Repository.elastic_search(
term, term,
type: :blob type: 'blob'
)[:blobs][:results].response )[:blobs][:results].response
blobs.map do |blob| blobs.map do |blob|
......
...@@ -18,8 +18,8 @@ describe ProjectWiki, :elastic do ...@@ -18,8 +18,8 @@ describe ProjectWiki, :elastic do
end end
it "searches wiki page" do 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', 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 | term2', type: 'wiki_blob')[:wiki_blobs][:total_count]).to eq(2)
end end
it 'indexes' do it 'indexes' do
...@@ -29,7 +29,7 @@ describe ProjectWiki, :elastic do ...@@ -29,7 +29,7 @@ describe ProjectWiki, :elastic do
end end
it 'can delete wiki pages' do 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 Sidekiq::Testing.inline! do
project.wiki.find_page('omega_page').delete project.wiki.find_page('omega_page').delete
...@@ -44,6 +44,6 @@ describe ProjectWiki, :elastic do ...@@ -44,6 +44,6 @@ describe ProjectWiki, :elastic do
ensure_elasticsearch_index! ensure_elasticsearch_index!
end 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
end end
...@@ -54,10 +54,10 @@ describe Repository, :elastic do ...@@ -54,10 +54,10 @@ describe Repository, :elastic do
blobs, commits = results.partition { |result| result['_source']['blob'].present? } blobs, commits = results.partition { |result| result['_source']['blob'].present? }
case type case type
when :blob when 'blob'
expect(blobs).not_to be_empty expect(blobs).not_to be_empty
expect(commits).to be_empty expect(commits).to be_empty
when :commit when 'commit'
expect(blobs).to be_empty expect(blobs).to be_empty
expect(commits).not_to be_empty expect(commits).not_to be_empty
else else
...@@ -71,10 +71,10 @@ describe Repository, :elastic do ...@@ -71,10 +71,10 @@ describe Repository, :elastic do
project = create :project, :repository project = create :project, :repository
index!(project) index!(project)
search_and_check!(Repository, '-foo', type: :blob) search_and_check!(Repository, '-foo', type: 'blob')
search_and_check!(Repository, '-foo', type: :commit) search_and_check!(Repository, '-foo', type: 'commit')
search_and_check!(project.repository, '-foo', type: :blob) search_and_check!(project.repository, '-foo', type: 'blob')
search_and_check!(project.repository, '-foo', type: :commit) search_and_check!(project.repository, '-foo', type: 'commit')
end end
describe 'class method find_commits_by_message_with_elastic', :sidekiq_might_not_need_inline do describe 'class method find_commits_by_message_with_elastic', :sidekiq_might_not_need_inline 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