Commit 535aeb80 authored by Valery Sizov's avatar Valery Sizov

[ES] Fix of record deletion

parent 38f7f31a
......@@ -45,7 +45,12 @@ module Elastic
after_commit on: :destroy do
if current_application_settings.elasticsearch_indexing? && self.searchable?
ElasticIndexerWorker.perform_async(:delete, self.class.to_s, self.id)
ElasticIndexerWorker.perform_async(
:delete,
self.class.to_s,
self.id,
project_id: self.es_parent
)
end
end
......@@ -55,7 +60,7 @@ module Elastic
end
def es_parent
project_id
return project_id if respond_to?(:project_id)
end
end
......
......@@ -5,6 +5,7 @@ class ElasticIndexerWorker
sidekiq_options queue: :elasticsearch
ISSUE_TRACKED_FIELDS = %w(assignee_id author_id confidential)
NOT_NESTED_ENTITIES = [Project, PersonalSnippet, ProjectSnippet, Snippet]
def perform(operation, class_name, record_id, options = {})
klass = class_name.constantize
......@@ -14,7 +15,7 @@ class ElasticIndexerWorker
record = klass.find(record_id)
record.__elasticsearch__.client = client
if [Project, PersonalSnippet, ProjectSnippet, Snippet].include?(klass)
if NOT_NESTED_ENTITIES.include?(klass)
record.__elasticsearch__.__send__ "#{operation}_document"
else
record.__elasticsearch__.__send__ "#{operation}_document", parent: record.es_parent
......@@ -22,7 +23,16 @@ class ElasticIndexerWorker
update_issue_notes(record, options["changed_fields"]) if klass == Issue
when /delete/
if NOT_NESTED_ENTITIES.include?(klass)
client.delete index: klass.index_name, type: klass.document_type, id: record_id
else
client.delete(
index: klass.index_name,
type: klass.document_type,
id: record_id,
parent: options["project_id"]
)
end
clear_project_indexes(record_id) if klass == Project
end
......@@ -30,6 +40,8 @@ class ElasticIndexerWorker
true # Less work to do!
end
private
def update_issue_notes(record, changed_fields)
if changed_fields && (changed_fields & ISSUE_TRACKED_FIELDS).any?
Note.import_with_parent query: -> { where(noteable: record) }
......@@ -37,7 +49,12 @@ class ElasticIndexerWorker
end
def clear_project_indexes(record_id)
# Remove repository index
remove_repository_index(record_id)
remove_wiki_index(record_id)
remove_nested_content(record_id)
end
def remove_repository_index(record_id)
client.delete_by_query({
index: Repository.__elasticsearch__.index_name,
body: {
......@@ -49,8 +66,20 @@ class ElasticIndexerWorker
}
}
})
end
def remove_nested_content(record_id)
client.delete_by_query({
index: Project.__elasticsearch__.index_name,
body: {
query: {
term: { "_parent" => record_id }
}
}
})
end
# Remove wiki index
def remove_wiki_index(record_id)
client.delete_by_query({
index: ProjectWiki.__elasticsearch__.index_name,
body: {
......
require 'spec_helper'
describe ElasticIndexerWorker, elastic: true do
include Gitlab::CurrentSettings
subject { described_class.new }
before do
Elasticsearch::Model.client = Elasticsearch::Client.new(
host: current_application_settings.elasticsearch_host,
port: current_application_settings.elasticsearch_port
)
Gitlab::Elastic::Helper.create_empty_index
end
after do
Gitlab::Elastic::Helper.delete_index
end
Sidekiq::Testing.disable! do
describe 'Indexing new records' do
it 'indexes a project' do
project = create :empty_project
expect do
subject.perform("index", "Project", project.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
it 'indexes an issue' do
issue = create :issue
expect do
subject.perform("index", "Issue", issue.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
it 'indexes a note' do
note = create :note
expect do
subject.perform("index", "Note", note.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
it 'indexes a milestone' do
milestone = create :milestone
expect do
subject.perform("index", "Milestone", milestone.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
it 'indexes a merge request' do
merge_request = create :merge_request
expect do
subject.perform("index", "MergeRequest", merge_request.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
end
describe 'Updating index' do
it 'updates a project' do
project = create :empty_project
subject.perform("index", "Project", project.id)
project.update(name: "new")
expect do
subject.perform("update", "Project", project.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates an issue' do
issue = create :issue
subject.perform("index", "Issue", issue.id)
issue.update(title: "new")
expect do
subject.perform("update", "Issue", issue.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates a note' do
note = create :note
subject.perform("index", "Note", note.id)
note.update(note: 'new')
expect do
subject.perform("update", "Note", note.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates a milestone' do
milestone = create :milestone
subject.perform("index", "Milestone", milestone.id)
milestone.update(title: 'new')
expect do
subject.perform("update", "Milestone", milestone.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates a merge request' do
merge_request = create :merge_request
subject.perform("index", "MergeRequest", merge_request.id)
merge_request.update(title: 'new')
expect do
subject.perform("index", "MergeRequest", merge_request.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
end
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