Browse Source

Fixed issue #2611 - SearchIndexBackend.remove() (ES) is not removing entries

Denny Bresch 5 years ago
parent
commit
c60e1c0780

+ 6 - 4
lib/search_index_backend.rb

@@ -331,7 +331,7 @@ remove whole data from index
     return [] if query.blank?
     return [] if query.blank?
 
 
     url = build_url
     url = build_url
-    return if url.blank?
+    return [] if url.blank?
 
 
     url += build_search_url(index)
     url += build_search_url(index)
 
 
@@ -735,9 +735,11 @@ return true if backend is configured
     if Setting.get('es_multi_index') == false
     if Setting.get('es_multi_index') == false
       url = Setting.get('es_url')
       url = Setting.get('es_url')
       url = if type
       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
               end
               if o_id
               if o_id
                 "#{url}/#{index}/#{type}/#{o_id}#{url_pipline}"
                 "#{url}/#{index}/#{type}/#{o_id}#{url_pipline}"

+ 32 - 2
spec/lib/search_index_backend_spec.rb

@@ -1,6 +1,12 @@
 require 'rails_helper'
 require 'rails_helper'
 
 
-RSpec.describe SearchIndexBackend do
+RSpec.describe SearchIndexBackend, searchindex: true do
+
+  before do
+    configure_elasticsearch
+    rebuild_searchindex
+  end
+
   describe '.build_query' do
   describe '.build_query' do
     subject(:query) { described_class.build_query('', query_extension: params) }
     subject(:query) { described_class.build_query('', query_extension: params) }
 
 
@@ -20,7 +26,7 @@ RSpec.describe SearchIndexBackend do
       context 'on a single index' do
       context 'on a single index' do
         let(:index) { 'User' }
         let(:index) { 'User' }
 
 
-        it { is_expected.to be_nil }
+        it { is_expected.to be_an(Array).and be_empty }
       end
       end
 
 
       context 'on multiple indices' do
       context 'on multiple indices' do
@@ -99,4 +105,28 @@ RSpec.describe SearchIndexBackend do
       end
       end
     end
     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
 end

+ 19 - 13
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
 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)
   def configure_elasticsearch(required: false)
     if ENV['ES_URL'].blank?
     if ENV['ES_URL'].blank?
       return if !required
       return if !required
@@ -16,7 +32,7 @@ module SearchindexBackendHelper
 
 
     if ENV['ES_INDEX_RAND'].present?
     if ENV['ES_INDEX_RAND'].present?
       rand_id          = ENV.fetch('CI_JOB_ID', "r#{rand(999)}")
       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)}"
       ENV['ES_INDEX']  = "es_index_#{test_method_name}_#{rand_id}_#{rand(999_999_999)}"
     end
     end
     if ENV['ES_INDEX'].blank?
     if ENV['ES_INDEX'].blank?
@@ -32,21 +48,11 @@ module SearchindexBackendHelper
   end
   end
 
 
   def rebuild_searchindex
   def rebuild_searchindex
+    Rake::Task.clear
+    Zammad::Application.load_tasks
     Rake::Task['searchindex:rebuild'].execute
     Rake::Task['searchindex:rebuild'].execute
   end
   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
 end
 
 
 RSpec.configure do |config|
 RSpec.configure do |config|

+ 2 - 0
test/support/searchindex_helper.rb

@@ -1,5 +1,7 @@
 require 'rake'
 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
 module SearchindexHelper
 
 
   def self.included(base)
   def self.included(base)