Commit 8d43eb73 authored by Mark Chao's avatar Mark Chao

ES: Update search results

Lazy pagination is no longer applicable, since blob searches has to
be loaded and wrapped, similar to commit searches, so method signatures
are changed accordingly.
parent 2b0f6dce
......@@ -24,9 +24,9 @@ module Gitlab
when 'notes'
notes.page(page).per(per_page).records
when 'blobs'
blobs.page(page).per(per_page)
blobs(page: page, per_page: per_page)
when 'wiki_blobs'
wiki_blobs.page(page).per(per_page)
wiki_blobs(page: page, per_page: per_page)
when 'commits'
commits(page: page, per_page: per_page)
when 'users'
......@@ -42,7 +42,7 @@ module Gitlab
private
def blobs
def blobs(page: 1, per_page: 20)
return Kaminari.paginate_array([]) unless Ability.allowed?(@current_user, :download_code, project)
if project.empty_repo? || query.blank?
......@@ -50,11 +50,11 @@ module Gitlab
else
# We use elastic for default branch only
if root_ref?
project.repository.elastic_search(
project.repository.__elasticsearch__.elastic_search_as_found_blob(
query,
type: :blob,
options: { highlight: true }
)[:blobs][:results].response
page: (page || 1).to_i,
per: per_page
)
else
Kaminari.paginate_array(
Gitlab::FileFinder.new(project, repository_ref).find(query)
......@@ -63,15 +63,15 @@ module Gitlab
end
end
def wiki_blobs
def wiki_blobs(page: 1, per_page: 20)
return Kaminari.paginate_array([]) unless Ability.allowed?(@current_user, :read_wiki, project)
if project.wiki_enabled? && !project.wiki.empty? && query.present?
project.wiki.elastic_search(
project.wiki.__elasticsearch__.elastic_search_as_wiki_page(
query,
type: :wiki_blob,
options: { highlight: true }
)[:wiki_blobs][:results].response
page: (page || 1).to_i,
per: per_page
)
else
Kaminari.paginate_array([])
end
......
......@@ -37,9 +37,9 @@ module Gitlab
when 'notes'
eager_load(notes, page, eager: { project: [:route, :namespace] })
when 'blobs'
blobs.page(page).per(per_page)
blobs(page: page, per_page: per_page)
when 'wiki_blobs'
wiki_blobs.page(page).per(per_page)
wiki_blobs(page: page, per_page: per_page)
when 'commits'
commits(page: page, per_page: per_page)
when 'users'
......@@ -226,7 +226,7 @@ module Gitlab
end
end
def blobs
def blobs(page: 1, per_page: 20)
return Kaminari.paginate_array([]) if query.blank?
strong_memoize(:blobs) do
......@@ -234,15 +234,16 @@ module Gitlab
additional_filter: repository_filter(limit_project_ids)
)
Repository.elastic_search(
Repository.__elasticsearch__.elastic_search_as_found_blob(
query,
type: :blob,
options: options.merge({ highlight: true })
)[:blobs][:results].response
page: (page || 1).to_i,
per: per_page,
options: options
)
end
end
def wiki_blobs
def wiki_blobs(page: 1, per_page: 20)
return Kaminari.paginate_array([]) if query.blank?
strong_memoize(:wiki_blobs) do
......@@ -250,11 +251,12 @@ module Gitlab
additional_filter: wiki_filter(limit_project_ids)
)
ProjectWiki.elastic_search(
ProjectWiki.__elasticsearch__.elastic_search_as_wiki_page(
query,
type: :wiki_blob,
options: options.merge({ highlight: true })
)[:wiki_blobs][:results].response
page: (page || 1).to_i,
per: per_page,
options: options
)
end
end
......
......@@ -522,11 +522,7 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
end
def search_for(term)
blobs = described_class.new(user, term, [project_1.id]).objects('blobs')
blobs.map do |blob|
blob['_source']['blob']['path']
end
described_class.new(user, term, [project_1.id]).objects('blobs').map(&:path)
end
it_behaves_like 'a paginated object', 'blobs'
......@@ -535,7 +531,7 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
results = described_class.new(user, 'def', limit_project_ids)
blobs = results.objects('blobs')
expect(blobs.first['_source']['blob']['content']).to include('def')
expect(blobs.first.data).to include('def')
expect(results.blobs_count).to eq 7
end
......@@ -547,7 +543,7 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
results = described_class.new(user, 'def', [project_1.id])
expect(results.blobs_count).to eq 7
result_project_ids = results.objects('blobs').map { |r| r.dig('_source', 'project_id') }
result_project_ids = results.objects('blobs').map(&:project_id)
expect(result_project_ids.uniq).to eq([project_1.id])
results = described_class.new(user, 'def', [project_1.id, project_2.id])
......@@ -659,7 +655,7 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
it 'finds wiki blobs' do
blobs = results.objects('wiki_blobs')
expect(blobs.first['_source']['blob']['content']).to include("term")
expect(blobs.first.data).to include('term')
expect(results.wiki_blobs_count).to eq 1
end
......@@ -667,7 +663,7 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
project_1.add_guest(user)
blobs = results.objects('wiki_blobs')
expect(blobs.first['_source']['blob']['content']).to include("term")
expect(blobs.first.data).to include('term')
expect(results.wiki_blobs_count).to eq 1
end
......@@ -992,14 +988,14 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
results = described_class.new(user, 'term', limit_project_ids)
blobs = results.objects('wiki_blobs')
expect(blobs.map { |blob| blob.join_field.parent }).to match_array [internal_project.es_id, private_project2.es_id, public_project.es_id]
expect(blobs.map(&:project)).to match_array [internal_project, private_project2, public_project]
expect(results.wiki_blobs_count).to eq 3
# Unauthenticated search
results = described_class.new(nil, 'term', [])
blobs = results.objects('wiki_blobs')
expect(blobs.first.join_field.parent).to eq public_project.es_id
expect(blobs.first.project).to eq public_project
expect(results.wiki_blobs_count).to eq 1
end
end
......@@ -1056,14 +1052,14 @@ describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need_inlin
results = described_class.new(user, 'tesla', limit_project_ids)
blobs = results.objects('blobs')
expect(blobs.map { |blob| blob.join_field.parent }).to match_array [internal_project.es_id, private_project2.es_id, public_project.es_id]
expect(blobs.map(&:project)).to match_array [internal_project, private_project2, public_project]
expect(results.blobs_count).to eq 3
# Unauthenticated search
results = described_class.new(nil, 'tesla', [])
blobs = results.objects('blobs')
expect(blobs.first.join_field.parent).to eq public_project.es_id
expect(blobs.first.project).to eq public_project
expect(results.blobs_count).to eq 1
end
end
......
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