Improve speed for global search [SCI-11177]

This commit is contained in:
Andrej 2024-11-07 11:26:24 +01:00 committed by Andrej
parent ae832cb3d3
commit 3494ccdf72
4 changed files with 18 additions and 12 deletions

View file

@ -92,7 +92,7 @@ class SearchController < ApplicationController
@model = Protocol
search_by_name({ in_repository: true })
render json: @records,
render json: @records.includes(:team, :added_by),
each_serializer: GlobalSearch::ProtocolSerializer,
meta: {
total: @records.total_count,

View file

@ -115,7 +115,7 @@ module SearchableModel
options[:raw_input].where(id: search_subquery(phrase[:query], options[:raw_input]))
end
query_clauses = if index.zero?
where(id: subquery_result)
subquery_result
elsif phrase[:current_operator] == 'or'
query_clauses.or(subquery_result)
else

View file

@ -157,11 +157,13 @@ class Protocol < ApplicationRecord
templates = latest_available_versions(teams)
.with_granted_permissions(user, ProtocolPermissions::READ)
templates = templates.active unless include_archived
templates.select(:id)
templates
end || []
protocol_my_modules = if options[:options]&.dig(:in_repository).blank?
protocols = viewable_by_user_my_module_protocols(user, teams)
.where(protocol_type: [Protocol.protocol_types[:unlinked],
Protocol.protocol_types[:linked]])
unless include_archived
protocols = protocols.joins(my_module: { experiment: :project })
.active
@ -170,12 +172,16 @@ class Protocol < ApplicationRecord
projects: { archived: false })
end
protocols.select(:id)
protocols
end || []
protocols = Protocol.where('(protocols.protocol_type IN (?) AND protocols.id IN (?)) OR (protocols.id IN (?))',
[Protocol.protocol_types[:unlinked], Protocol.protocol_types[:linked]],
protocol_my_modules, protocol_templates)
protocols = if (options[:options]&.dig(:in_repository).present? || options[:options].blank?) &&
options[:options]&.dig(:in_repository).blank?
where('protocols.id IN ((?) UNION ALL (?))',
protocol_templates.select(:id), protocol_my_modules.select(:id))
else
options[:options]&.dig(:in_repository).blank? ? protocol_my_modules : Protocol.where(id: protocol_templates.pluck(:id))
end
protocols.where_attributes_like_boolean(SEARCHABLE_ATTRIBUTES, query, { with_subquery: true, raw_input: protocols })
end
@ -209,7 +215,7 @@ class Protocol < ApplicationRecord
.where(protocol_type: Protocol.protocol_types[:in_repository_draft], parent_id: nil)
.select(:id)
where('protocols.id IN ((?) UNION (?) UNION (?))', original_without_versions, published_versions, new_drafts)
where('protocols.id IN ((?) UNION ALL (?) UNION ALL (?))', original_without_versions, published_versions, new_drafts)
end
def self.viewable_by_user(user, teams, options = {})

View file

@ -48,8 +48,7 @@ class Result < ApplicationRecord
options = {})
teams = options[:teams] || current_team || user.teams.select(:id)
new_query = left_joins(:result_comments, :result_texts, result_tables: :table)
.joins(:my_module)
new_query = joins(:my_module)
.where(my_modules: MyModule.with_granted_permissions(user, MyModulePermissions::READ)
.where(user_assignments: { team: teams }))
@ -63,11 +62,12 @@ class Result < ApplicationRecord
new_query.where_attributes_like_boolean(
SEARCHABLE_ATTRIBUTES, query, { with_subquery: true, raw_input: new_query }
).distinct
)
end
def self.search_subquery(query, raw_input)
raw_input.where_attributes_like_boolean(SEARCHABLE_ATTRIBUTES, query)
raw_input.left_joins(:result_comments, :result_texts, result_tables: :table)
.where_attributes_like_boolean(SEARCHABLE_ATTRIBUTES, query)
end
def duplicate(my_module, user, result_name: nil)