Commit 40e6800d authored by Valery Sizov's avatar Valery Sizov

Fix ES search for non-default branches

parent cdb51842
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
- blob = parse_search_result(blob) - blob = parse_search_result(blob)
- file_name = blob.filename - file_name = blob.filename
- ref = blob.ref - ref = @search_results.repository_ref
- blob_link = namespace_project_blob_path(project.namespace, project, tree_join(ref, file_name)) - blob_link = namespace_project_blob_path(project.namespace, project, tree_join(ref, file_name))
.blob-result .blob-result
.file-holder .file-holder
......
...@@ -65,7 +65,7 @@ module Gitlab ...@@ -65,7 +65,7 @@ module Gitlab
)[:blobs][:results].response )[:blobs][:results].response
else else
Kaminari.paginate_array( Kaminari.paginate_array(
project.repository.search_files(query, repository_ref) Gitlab::FileFinder.new(project, repository_ref).find(query)
) )
end end
end end
...@@ -105,10 +105,12 @@ module Gitlab ...@@ -105,10 +105,12 @@ module Gitlab
per_page: per_page per_page: per_page
) )
else else
offset = per_page * ((page || 1) - 1)
Kaminari.paginate_array( Kaminari.paginate_array(
project.repository.find_commits_by_message(query).compact, project.repository.find_commits_by_message(query),
page: (page || 1).to_i, offset: offset,
per_page: per_page limit: per_page
) )
end end
end end
......
# This class finds files in a repository by name and content
# the result is joined and sorted by file name
module Gitlab
class FileFinder
BATCH_SIZE = 100
attr_reader :project, :ref
def initialize(project, ref)
@project = project
@ref = ref
end
def find(query)
blobs = project.repository.search_files_by_content(query, ref).first(BATCH_SIZE)
found_file_names = Set.new
results = blobs.map do |blob|
blob = Gitlab::ProjectSearchResults.parse_search_result(blob)
found_file_names << blob.filename
[blob.filename, blob]
end
project.repository.search_files_by_name(query, ref).first(BATCH_SIZE).each do |filename|
results << [filename, nil] unless found_file_names.include?(filename)
end
results.sort_by(&:first)
end
end
end
...@@ -74,23 +74,7 @@ module Gitlab ...@@ -74,23 +74,7 @@ module Gitlab
private private
def blobs def blobs
@blobs ||= begin @blobs ||= Gitlab::FileFinder.new(project, repository_ref).find(query)
blobs = project.repository.search_files_by_content(query, repository_ref).first(100)
found_file_names = Set.new
results = blobs.map do |blob|
blob = self.class.parse_search_result(blob)
found_file_names << blob.filename
[blob.filename, blob]
end
project.repository.search_files_by_name(query, repository_ref).first(100).each do |filename|
results << [filename, nil] unless found_file_names.include?(filename)
end
results.sort_by(&:first)
end
end end
def wiki_blobs def wiki_blobs
......
...@@ -46,30 +46,49 @@ describe Gitlab::Elastic::ProjectSearchResults, lib: true do ...@@ -46,30 +46,49 @@ describe Gitlab::Elastic::ProjectSearchResults, lib: true do
create :note, note: 'bla-bla term', project: project1 create :note, note: 'bla-bla term', project: project1
# Wiki # Wiki
project.wiki.create_page("index_page", "term") project.wiki.create_page('index_page', 'term')
project.wiki.index_blobs project.wiki.index_blobs
project1.wiki.create_page("index_page", " term") project1.wiki.create_page('index_page', ' term')
project1.wiki.index_blobs project1.wiki.index_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
result = Gitlab::Elastic::ProjectSearchResults.new(user, "term", project.id) result = Gitlab::Elastic::ProjectSearchResults.new(user, 'term', project.id)
expect(result.notes_count).to eq(1) expect(result.notes_count).to eq(1)
expect(result.wiki_blobs_count).to eq(1) expect(result.wiki_blobs_count).to eq(1)
expect(result.blobs_count).to eq(1) expect(result.blobs_count).to eq(1)
result1 = Gitlab::Elastic::ProjectSearchResults.new(user, "initial", project.id) result1 = Gitlab::Elastic::ProjectSearchResults.new(user, 'initial', project.id)
expect(result1.commits_count).to eq(1) expect(result1.commits_count).to eq(1)
end end
end end
describe "search for commits in non-default branch" do describe "search for commits in non-default branch" do
it "finds needed commit" do it 'finds needed commit' do
project = create :project project = create :project
result = Gitlab::Elastic::ProjectSearchResults.new(user, "initial", project.id, 'test') result = Gitlab::Elastic::ProjectSearchResults.new(user, 'initial', project.id, 'test')
expect(result.commits_count).to eq(1) expect(result.commits_count).to eq(1)
end end
it 'responds to total_pages method' do
project = create :project
result = Gitlab::Elastic::ProjectSearchResults.new(user, 'initial', project.id, 'test')
expect(result.objects('commits').total_pages).to eq(1)
end
end
describe 'search for blobs in non-default branch' do
it 'users FileFinder instead of ES search' do
project = create :project
expect_any_instance_of(Gitlab::FileFinder).to receive(:find).with('initial').and_return([])
result = Gitlab::Elastic::ProjectSearchResults.new(user, 'initial', project.id, 'test')
result.blobs_count
end
end end
describe 'confidential issues' do describe 'confidential issues' 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