Browse Source

Fixes #4819 - Search is slow due to being executed twice.

Martin Gruner 1 year ago
parent
commit
b8a616a03f
2 changed files with 18 additions and 3 deletions
  1. 3 3
      app/services/service/search.rb
  2. 15 0
      spec/services/service/search_spec.rb

+ 3 - 3
app/services/service/search.rb

@@ -20,11 +20,11 @@ class Service::Search < Service::BaseWithCurrentUser
       # Finally, sort by object priority.
       models(objects: objects).map do |model|
         result_by_model[model]
-      end.flatten
+      end.flatten.compact
     else
       models(objects: objects).map do |model|
         model_search(model: model, term: term, options: options)
-      end.flatten
+      end.flatten.compact
     end
   end
 
@@ -53,7 +53,7 @@ class Service::Search < Service::BaseWithCurrentUser
     objects.select do |model|
       prefs = model.search_preferences(current_user)
       next false if !prefs
-      next false if direct_search_index.present? && !prefs[:direct_search_index] != direct_search_index
+      next false if !direct_search_index.nil? && prefs[:direct_search_index] != direct_search_index
 
       true
     end.sort_by do |model|

+ 15 - 0
spec/services/service/search_spec.rb

@@ -0,0 +1,15 @@
+# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe Service::Search do
+  describe '#models' do
+    let(:search)                      { described_class.new(current_user: create(:agent)) }
+    let(:models_with_direct_index)    { search.models(objects: Models.searchable, direct_search_index: true) }
+    let(:models_without_direct_index) { search.models(objects: Models.searchable, direct_search_index: false) }
+
+    it 'returns different models for different direct_search_index flags' do
+      expect(models_with_direct_index).not_to be_intersect(models_without_direct_index)
+    end
+  end
+end