Commit 08bee854 authored by Markus Koller's avatar Markus Koller

Don't delete unrelated index when indexing wiki

When using Gitlab::Elastic::Indexer with a wiki repository, the call to
delete_index_for_commits_and_blobs caused the existing commits and blobs
for the project repository to be deleted from the index.
parent 2f409a9a
......@@ -318,7 +318,14 @@ module Elasticsearch
res
end
def delete_index_for_commits_and_blobs
def delete_index_for_commits_and_blobs(wiki: false)
types =
if wiki
%w[wiki_blob]
else
%w[commit blob]
end
client_for_indexing.delete_by_query(
index: self.class.index_name,
routing: es_parent,
......@@ -328,7 +335,7 @@ module Elasticsearch
filter: [
{
terms: {
type: %w{commit blob}
type: types
}
},
{
......
......@@ -92,7 +92,7 @@ module Gitlab
def run_indexer!(to_sha)
if index_status && !repository_contains_last_indexed_commit?
repository.delete_index_for_commits_and_blobs
repository.delete_index_for_commits_and_blobs(wiki: wiki?)
end
command =
......
......@@ -31,7 +31,7 @@ describe Gitlab::Elastic::Indexer do
end
context 'wikis' do
let!(:project) { create(:project, :wiki_repo) }
let(:project) { create(:project, :wiki_repo) }
let(:indexer) { described_class.new(project, wiki: true) }
before do
......@@ -66,6 +66,57 @@ describe Gitlab::Elastic::Indexer do
indexer.run
end
context 'when IndexStatus#last_wiki_commit is no longer in repository', :elastic do
let(:user) { project.owner }
let(:ee_application_setting) { nil }
before do
stub_ee_application_setting(elasticsearch_indexing: true)
ElasticIndexerWorker.new.perform('index', 'Project', project.id, project.es_id)
end
def change_wiki_and_index(project, &blk)
yield blk if blk
current_commit = project.wiki.repository.commit('master').sha
described_class.new(project, wiki: true).run(current_commit)
Gitlab::Elastic::Helper.refresh_index
end
def indexed_wiki_paths_for(term)
blobs = ProjectWiki.search(
term,
type: :wiki_blob
)[:wiki_blobs][:results].response
blobs.map do |blob|
blob['_source']['blob']['path']
end
end
it 'reindexes from scratch' do
sha_for_reset = nil
change_wiki_and_index(project) do
sha_for_reset = project.wiki.repository.create_file(user, '12', '', message: '12', branch_name: 'master')
project.wiki.repository.create_file(user, '23', '', message: '23', branch_name: 'master')
end
expect(indexed_wiki_paths_for('12')).to include('12')
expect(indexed_wiki_paths_for('23')).to include('23')
project.index_status.update!(last_wiki_commit: '____________')
change_wiki_and_index(project) do
project.wiki.repository.write_ref('master', sha_for_reset)
end
expect(indexed_wiki_paths_for('12')).to include('12')
expect(indexed_wiki_paths_for('23')).not_to include('23')
end
end
end
context 'repository has unborn head' do
......@@ -250,10 +301,10 @@ describe Gitlab::Elastic::Indexer do
context 'when IndexStatus#last_commit is no longer in repository' do
before do
ElasticIndexerWorker.new.perform("index", "Project", project.id, project.es_id)
ElasticIndexerWorker.new.perform('index', 'Project', project.id, project.es_id)
end
it 'reindexes from scratch if IndexStatus#last_commit is no longer in repository' do
it 'reindexes from scratch' do
sha_for_reset = nil
change_repository_and_index(project) do
......@@ -264,7 +315,7 @@ describe Gitlab::Elastic::Indexer do
expect(indexed_file_paths_for('12')).to include('12')
expect(indexed_file_paths_for('23')).to include('23')
project.index_status.update(last_commit: '____________')
project.index_status.update!(last_commit: '____________')
change_repository_and_index(project) do
project.repository.write_ref('master', sha_for_reset)
......
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