mirror of
				https://github.com/scinote-eln/scinote-web.git
				synced 2025-10-25 05:27:33 +08:00 
			
		
		
		
	Improve speed for global search [SCI-11177]
This commit is contained in:
		
							parent
							
								
									ae832cb3d3
								
							
						
					
					
						commit
						3494ccdf72
					
				
					 4 changed files with 18 additions and 12 deletions
				
			
		|  | @ -92,7 +92,7 @@ class SearchController < ApplicationController | ||||||
|           @model = Protocol |           @model = Protocol | ||||||
|           search_by_name({ in_repository: true }) |           search_by_name({ in_repository: true }) | ||||||
| 
 | 
 | ||||||
|           render json: @records, |           render json: @records.includes(:team, :added_by), | ||||||
|                  each_serializer: GlobalSearch::ProtocolSerializer, |                  each_serializer: GlobalSearch::ProtocolSerializer, | ||||||
|                  meta: { |                  meta: { | ||||||
|                    total: @records.total_count, |                    total: @records.total_count, | ||||||
|  |  | ||||||
|  | @ -115,7 +115,7 @@ module SearchableModel | ||||||
|                               options[:raw_input].where(id: search_subquery(phrase[:query], options[:raw_input])) |                               options[:raw_input].where(id: search_subquery(phrase[:query], options[:raw_input])) | ||||||
|                             end |                             end | ||||||
|           query_clauses = if index.zero? |           query_clauses = if index.zero? | ||||||
|                             where(id: subquery_result) |                             subquery_result | ||||||
|                           elsif phrase[:current_operator] == 'or' |                           elsif phrase[:current_operator] == 'or' | ||||||
|                             query_clauses.or(subquery_result) |                             query_clauses.or(subquery_result) | ||||||
|                           else |                           else | ||||||
|  |  | ||||||
|  | @ -157,11 +157,13 @@ class Protocol < ApplicationRecord | ||||||
|                            templates = latest_available_versions(teams) |                            templates = latest_available_versions(teams) | ||||||
|                                        .with_granted_permissions(user, ProtocolPermissions::READ) |                                        .with_granted_permissions(user, ProtocolPermissions::READ) | ||||||
|                            templates = templates.active unless include_archived |                            templates = templates.active unless include_archived | ||||||
|                            templates.select(:id) |                            templates | ||||||
|                          end || [] |                          end || [] | ||||||
| 
 | 
 | ||||||
|     protocol_my_modules = if options[:options]&.dig(:in_repository).blank? |     protocol_my_modules = if options[:options]&.dig(:in_repository).blank? | ||||||
|                             protocols = viewable_by_user_my_module_protocols(user, teams) |                             protocols = viewable_by_user_my_module_protocols(user, teams) | ||||||
|  |                                         .where(protocol_type: [Protocol.protocol_types[:unlinked], | ||||||
|  |                                                                Protocol.protocol_types[:linked]]) | ||||||
|                             unless include_archived |                             unless include_archived | ||||||
|                               protocols = protocols.joins(my_module: { experiment: :project }) |                               protocols = protocols.joins(my_module: { experiment: :project }) | ||||||
|                                                    .active |                                                    .active | ||||||
|  | @ -170,12 +172,16 @@ class Protocol < ApplicationRecord | ||||||
|                                                           projects: { archived: false }) |                                                           projects: { archived: false }) | ||||||
|                             end |                             end | ||||||
| 
 | 
 | ||||||
|                             protocols.select(:id) |                             protocols | ||||||
|                           end || [] |                           end || [] | ||||||
| 
 | 
 | ||||||
|     protocols = Protocol.where('(protocols.protocol_type IN (?) AND protocols.id IN (?)) OR (protocols.id IN (?))', |     protocols = if (options[:options]&.dig(:in_repository).present? || options[:options].blank?) && | ||||||
|                                [Protocol.protocol_types[:unlinked], Protocol.protocol_types[:linked]], |                    options[:options]&.dig(:in_repository).blank? | ||||||
|                                protocol_my_modules, protocol_templates) |                   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 }) |     protocols.where_attributes_like_boolean(SEARCHABLE_ATTRIBUTES, query, { with_subquery: true, raw_input: protocols }) | ||||||
|   end |   end | ||||||
|  | @ -209,7 +215,7 @@ class Protocol < ApplicationRecord | ||||||
|                  .where(protocol_type: Protocol.protocol_types[:in_repository_draft], parent_id: nil) |                  .where(protocol_type: Protocol.protocol_types[:in_repository_draft], parent_id: nil) | ||||||
|                  .select(:id) |                  .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 |   end | ||||||
| 
 | 
 | ||||||
|   def self.viewable_by_user(user, teams, options = {}) |   def self.viewable_by_user(user, teams, options = {}) | ||||||
|  |  | ||||||
|  | @ -48,8 +48,7 @@ class Result < ApplicationRecord | ||||||
|                   options = {}) |                   options = {}) | ||||||
|     teams = options[:teams] || current_team || user.teams.select(:id) |     teams = options[:teams] || current_team || user.teams.select(:id) | ||||||
| 
 | 
 | ||||||
|     new_query = left_joins(:result_comments, :result_texts, result_tables: :table) |     new_query = joins(:my_module) | ||||||
|                 .joins(:my_module) |  | ||||||
|                 .where(my_modules: MyModule.with_granted_permissions(user, MyModulePermissions::READ) |                 .where(my_modules: MyModule.with_granted_permissions(user, MyModulePermissions::READ) | ||||||
|                                            .where(user_assignments: { team: teams })) |                                            .where(user_assignments: { team: teams })) | ||||||
| 
 | 
 | ||||||
|  | @ -63,11 +62,12 @@ class Result < ApplicationRecord | ||||||
| 
 | 
 | ||||||
|     new_query.where_attributes_like_boolean( |     new_query.where_attributes_like_boolean( | ||||||
|       SEARCHABLE_ATTRIBUTES, query, { with_subquery: true, raw_input: new_query } |       SEARCHABLE_ATTRIBUTES, query, { with_subquery: true, raw_input: new_query } | ||||||
|     ).distinct |     ) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def self.search_subquery(query, raw_input) |   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 |   end | ||||||
| 
 | 
 | ||||||
|   def duplicate(my_module, user, result_name: nil) |   def duplicate(my_module, user, result_name: nil) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue