From 0eac6f0fd92a2c22a0eeab240c898e04be5039f1 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Wed, 27 Jan 2021 13:55:22 +0100 Subject: [PATCH] Fixes #3234 - Renaming a Group should trigger reindexing. --- .../concerns/has_search_index_backend.rb | 15 ++++- app/models/group.rb | 1 + app/models/ticket/priority.rb | 1 + app/models/ticket/state.rb | 1 + spec/requests/search_spec.rb | 60 +++++++++++++++++++ 5 files changed, 75 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/has_search_index_backend.rb b/app/models/concerns/has_search_index_backend.rb index 626279dd5..dd87155a8 100644 --- a/app/models/concerns/has_search_index_backend.rb +++ b/app/models/concerns/has_search_index_backend.rb @@ -63,14 +63,23 @@ returns =end def search_index_update_associations_full - return if self.class.to_s != 'Organization' + update_class = { + 'Organization' => :organization_id, + 'Group' => :group_id, + 'Ticket::State' => :state_id, + 'Ticket::Priority' => :priority_id, + } + update_column = update_class[self.class.to_s] + return if update_column.blank? - # reindex all organization tickets for the given organization id + # reindex all object related tickets for the given object id # we can not use the delta function for this because of the excluded # ticket article attachments. see explain in delta function - Ticket.select('id').where(organization_id: id).order(id: :desc).limit(10_000).pluck(:id).each do |ticket_id| + Ticket.select('id').where(update_column => id).order(id: :desc).limit(10_000).pluck(:id).each do |ticket_id| SearchIndexJob.perform_later('Ticket', ticket_id) end + + true end =begin diff --git a/app/models/group.rb b/app/models/group.rb index 17c3cae05..f9efe6d86 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -9,6 +9,7 @@ class Group < ApplicationModel include HasObjectManagerAttributesValidation include HasCollectionUpdate include HasTicketCreateScreenImpact + include HasSearchIndexBackend belongs_to :email_address, optional: true belongs_to :signature, optional: true diff --git a/app/models/ticket/priority.rb b/app/models/ticket/priority.rb index 06c85b35e..3c02dbe48 100644 --- a/app/models/ticket/priority.rb +++ b/app/models/ticket/priority.rb @@ -2,6 +2,7 @@ class Ticket::Priority < ApplicationModel include CanBeImported include HasCollectionUpdate + include HasSearchIndexBackend self.table_name = 'ticket_priorities' validates :name, presence: true diff --git a/app/models/ticket/state.rb b/app/models/ticket/state.rb index 48740e3b0..262d613a0 100644 --- a/app/models/ticket/state.rb +++ b/app/models/ticket/state.rb @@ -3,6 +3,7 @@ class Ticket::State < ApplicationModel include CanBeImported include ChecksLatestChangeObserved include HasCollectionUpdate + include HasSearchIndexBackend belongs_to :state_type, class_name: 'Ticket::StateType', inverse_of: :states, optional: true belongs_to :next_state, class_name: 'Ticket::State', optional: true diff --git a/spec/requests/search_spec.rb b/spec/requests/search_spec.rb index 6e029a982..397f51a4d 100644 --- a/spec/requests/search_spec.rb +++ b/spec/requests/search_spec.rb @@ -426,6 +426,66 @@ RSpec.describe 'Search', type: :request, searchindex: true do expect(json_response['assets']['Ticket'][ticket_nested.id.to_s]).to be_truthy end + it 'does find the ticket by group name even if the group name changes' do + authenticated_as(agent) + post '/api/v1/search/Ticket', params: { query: "number:#{ticket1.number} && group.name:ultrasupport" }, as: :json + expect(response).to have_http_status(:ok) + expect(json_response).to be_a_kind_of(Hash) + expect(json_response).to be_truthy + expect(json_response['assets']['Ticket']).to be_falsey + expect(group).not_to eq('ultrasupport') + + group.update(name: 'ultrasupport') + Scheduler.worker(true) + SearchIndexBackend.refresh + + post '/api/v1/search/Ticket', params: { query: "number:#{ticket1.number} && group.name:ultrasupport" }, as: :json + expect(response).to have_http_status(:ok) + expect(json_response).to be_a_kind_of(Hash) + expect(json_response).to be_truthy + expect(json_response['assets']['Ticket'][ticket1.id.to_s]).to be_truthy + end + + it 'does find the ticket by state name even if the state name changes' do + authenticated_as(agent) + post '/api/v1/search/Ticket', params: { query: "number:#{ticket1.number} && state.name:ultrastate" }, as: :json + expect(response).to have_http_status(:ok) + expect(json_response).to be_a_kind_of(Hash) + expect(json_response).to be_truthy + expect(json_response['assets']['Ticket']).to be_falsey + expect(ticket1.state.name).not_to eq('ultrastate') + + ticket1.state.update(name: 'ultrastate') + Scheduler.worker(true) + SearchIndexBackend.refresh + + post '/api/v1/search/Ticket', params: { query: "number:#{ticket1.number} && state.name:ultrastate" }, as: :json + expect(response).to have_http_status(:ok) + expect(json_response).to be_a_kind_of(Hash) + expect(json_response).to be_truthy + expect(json_response['assets']['Ticket'][ticket1.id.to_s]).to be_truthy + end + + it 'does find the ticket by priority name even if the priority name changes' do + authenticated_as(agent) + post '/api/v1/search/Ticket', params: { query: "number:#{ticket1.number} && priority.name:ultrapriority" }, as: :json + expect(response).to have_http_status(:ok) + expect(json_response).to be_a_kind_of(Hash) + expect(json_response).to be_truthy + expect(json_response['assets']['Ticket']).to be_falsey + expect(ticket1.priority.name).not_to eq('ultrapriority') + + ticket1.priority.update(name: 'ultrapriority') + Scheduler.worker(true) + SearchIndexBackend.refresh + + post '/api/v1/search/Ticket', params: { query: "number:#{ticket1.number} && priority.name:ultrapriority" }, as: :json + expect(response).to have_http_status(:ok) + expect(json_response).to be_a_kind_of(Hash) + expect(json_response).to be_truthy + expect(json_response['assets']['Ticket'][ticket1.id.to_s]).to be_truthy + end + it 'does find the ticket by attachment even after ticket reindex' do params = { query: 'text66',