diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index aec990680..c956acdae 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -331,7 +331,7 @@ remove whole data from index return [] if query.blank? url = build_url - return if url.blank? + return [] if url.blank? url += build_search_url(index) @@ -735,9 +735,11 @@ return true if backend is configured if Setting.get('es_multi_index') == false url = Setting.get('es_url') url = if type - url_pipline = Setting.get('es_pipeline') - if url_pipline.present? - url_pipline = "?pipeline=#{url_pipline}" + if pipeline == true + url_pipline = Setting.get('es_pipeline') + if url_pipline.present? + url_pipline = "?pipeline=#{url_pipline}" + end end if o_id "#{url}/#{index}/#{type}/#{o_id}#{url_pipline}" diff --git a/spec/lib/search_index_backend_spec.rb b/spec/lib/search_index_backend_spec.rb index bcb808503..0524597fc 100644 --- a/spec/lib/search_index_backend_spec.rb +++ b/spec/lib/search_index_backend_spec.rb @@ -1,6 +1,12 @@ require 'rails_helper' -RSpec.describe SearchIndexBackend do +RSpec.describe SearchIndexBackend, searchindex: true do + + before do + configure_elasticsearch + rebuild_searchindex + end + describe '.build_query' do subject(:query) { described_class.build_query('', query_extension: params) } @@ -20,7 +26,7 @@ RSpec.describe SearchIndexBackend do context 'on a single index' do let(:index) { 'User' } - it { is_expected.to be_nil } + it { is_expected.to be_an(Array).and be_empty } end context 'on multiple indices' do @@ -99,4 +105,28 @@ RSpec.describe SearchIndexBackend do end end end + + describe '.remove' do + context 'ticket' do + it 'from index after ticket delete' do + + skip('No ES configured') if !SearchIndexBackend.enabled? + + ticket = create :ticket + described_class.add('Ticket', ticket) + + # give es time to rebuild index + sleep 2 + result = described_class.search(ticket.number, 'Ticket', sort_by: ['updated_at'], order_by: ['desc']) + expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }]) + + described_class.remove('Ticket', ticket.id) + # give es time to rebuild index + sleep 2 + + result = described_class.search(ticket.number, 'Ticket', sort_by: ['updated_at'], order_by: ['desc']) + expect(result).to eq([]) + end + end + end end diff --git a/spec/support/searchindex_backend.rb b/spec/support/searchindex_backend.rb index 481030749..52890b84d 100644 --- a/spec/support/searchindex_backend.rb +++ b/spec/support/searchindex_backend.rb @@ -1,5 +1,21 @@ +require 'rake' + +# if you make changes, then please also change this file 'test/support/searchindex_helper.rb' +# this is required as long as our test suite is made of RSpec and MiniTest module SearchindexBackendHelper + def self.included(base) + # Execute in RSpec class context + base.class_exec do + + after(:each) do + next if ENV['ES_URL'].blank? + + Rake::Task['searchindex:drop'].execute + end + end + end + def configure_elasticsearch(required: false) if ENV['ES_URL'].blank? return if !required @@ -16,7 +32,7 @@ module SearchindexBackendHelper if ENV['ES_INDEX_RAND'].present? rand_id = ENV.fetch('CI_JOB_ID', "r#{rand(999)}") - test_method_name = subject.gsub(/[^\w]/, '_') + test_method_name = self.class.description.gsub(/[^\w]/, '_') ENV['ES_INDEX'] = "es_index_#{test_method_name}_#{rand_id}_#{rand(999_999_999)}" end if ENV['ES_INDEX'].blank? @@ -32,21 +48,11 @@ module SearchindexBackendHelper end def rebuild_searchindex + Rake::Task.clear + Zammad::Application.load_tasks Rake::Task['searchindex:rebuild'].execute end - def self.included(base) - - # Execute in RSpec class context - base.class_exec do - - after(:each) do - next if ENV['ES_URL'].blank? - - Rake::Task['searchindex:drop'].execute - end - end - end end RSpec.configure do |config| diff --git a/test/support/searchindex_helper.rb b/test/support/searchindex_helper.rb index 89d1a3e7e..e754da78c 100644 --- a/test/support/searchindex_helper.rb +++ b/test/support/searchindex_helper.rb @@ -1,5 +1,7 @@ require 'rake' +# if you make changes, then please also change this file 'spec/support/searchindex_backend.rb' +# this is required as long as our test suite is made of RSpec and MiniTest module SearchindexHelper def self.included(base)