Skip to content

Commit

Permalink
Fixes #4819 - Search is slow due to being executed twice.
Browse files Browse the repository at this point in the history
(cherry picked from commit b8a616a)
  • Loading branch information
mgruner committed Sep 18, 2023
1 parent d6fd381 commit 3c9d1d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/services/service/search.rb
Expand Up @@ -20,11 +20,11 @@ def perform_search(term:, objects:, options:)
# 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

Expand Down Expand Up @@ -53,7 +53,7 @@ def models(objects:, direct_search_index: nil)
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|
Expand Down
15 changes: 15 additions & 0 deletions 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

0 comments on commit 3c9d1d8

Please sign in to comment.