2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class SearchController < ApplicationController
2018-03-27 19:54:05 +05:30
include ControllerWithCrossProjectAccessCheck
2014-09-02 18:07:02 +05:30
include SearchHelper
2020-11-24 15:15:51 +05:30
include RedisTracking
2022-07-23 23:45:48 +05:30
include ProductAnalyticsTracking
2022-05-07 20:08:51 +05:30
include SearchRateLimitable
2014-09-02 18:07:02 +05:30
2022-08-27 11:52:29 +05:30
RESCUE_FROM_TIMEOUT_ACTIONS = [ :count , :show , :autocomplete , :aggregations ] . freeze
2023-03-04 22:38:38 +05:30
CODE_SEARCH_LITERALS = %w[ blob: extension: path: filename: ] . freeze
2021-10-27 15:23:28 +05:30
2023-01-13 00:05:48 +05:30
track_custom_event :show ,
name : 'i_search_total' ,
label : 'redis_hll_counters.search.search_total_unique_counts_monthly' ,
action : 'executed' ,
destinations : [ :redis_hll , :snowplow ]
2020-11-24 15:15:51 +05:30
2022-08-27 11:52:29 +05:30
def self . search_rate_limited_endpoints
% i [ show count autocomplete ]
end
2019-09-30 21:07:59 +05:30
around_action :allow_gitaly_ref_name_caching
2021-11-11 11:23:49 +05:30
before_action :block_anonymous_global_searches , :check_scope_global_search_enabled , except : :opensearch
2018-03-27 19:54:05 +05:30
skip_before_action :authenticate_user!
requires_cross_project_access if : - > do
search_term_present = params [ :search ] . present? || params [ :term ] . present?
search_term_present && ! params [ :project_id ] . present?
end
2022-08-27 11:52:29 +05:30
before_action :check_search_rate_limit! , only : search_rate_limited_endpoints
2018-03-27 19:54:05 +05:30
2022-11-25 23:54:43 +05:30
before_action only : :show do
push_frontend_feature_flag ( :search_page_vertical_nav , current_user )
end
2023-03-04 22:38:38 +05:30
before_action only : :show do
update_scope_for_code_search
end
before_action :elasticsearch_in_use , only : :show
2021-09-30 23:02:18 +05:30
rescue_from ActiveRecord :: QueryCanceled , with : :render_timeout
2015-09-11 14:41:01 +05:30
layout 'search'
2021-01-03 14:25:43 +05:30
feature_category :global_search
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-03 14:25:43 +05:30
2014-09-02 18:07:02 +05:30
def show
2017-08-17 22:00:37 +05:30
@project = search_service . project
@group = search_service . group
2023-03-04 22:38:38 +05:30
@search_service = Gitlab :: View :: Presenter :: Factory . new ( search_service , current_user : current_user ) . fabricate!
2015-09-11 14:41:01 +05:30
2020-03-13 15:44:24 +05:30
return unless search_term_valid?
2021-01-29 00:20:46 +05:30
return if check_single_commit_result?
2016-06-02 11:05:42 +05:30
@search_term = params [ :search ]
2021-01-29 00:20:46 +05:30
@sort = params [ :sort ] || default_sort
2016-06-02 11:05:42 +05:30
2022-08-13 15:12:31 +05:30
@search_level = @search_service . level
@search_type = search_type
@global_search_duration_s = Benchmark . realtime do
@scope = @search_service . scope
@search_results = @search_service . search_results
@search_objects = @search_service . search_objects
@search_highlight = @search_service . search_highlight
end
2018-03-17 18:26:18 +05:30
2022-10-11 01:57:18 +05:30
Gitlab :: Metrics :: GlobalSearchSlis . record_apdex (
elapsed : @global_search_duration_s ,
search_type : @search_type ,
search_level : @search_level ,
search_scope : @scope
)
2020-06-23 00:09:42 +05:30
increment_search_counters
2022-10-11 01:57:18 +05:30
ensure
if @search_type
# If we raise an error somewhere in the @global_search_duration_s benchmark block, we will end up here
# with a 200 status code, but an empty @global_search_duration_s.
Gitlab :: Metrics :: GlobalSearchSlis . record_error_rate (
error : @global_search_duration_s . nil? || ( status < 200 || status > = 400 ) ,
search_type : @search_type ,
search_level : @search_level ,
search_scope : @scope
)
end
2014-09-02 18:07:02 +05:30
end
2019-10-12 21:52:04 +05:30
def count
params . require ( [ :search , :scope ] )
scope = search_service . scope
2021-04-29 21:17:54 +05:30
count = 0
ApplicationRecord . with_fast_read_statement_timeout do
count = search_service . search_results . formatted_count ( scope )
end
2019-10-12 21:52:04 +05:30
2021-04-17 20:07:23 +05:30
# Users switching tabs will keep fetching the same tab counts so it's a
# good idea to cache in their browser just for a short time. They can still
# clear cache if they are seeing an incorrect count but inaccurate count is
# not such a bad thing.
expires_in 1 . minute
2019-10-12 21:52:04 +05:30
render json : { count : count }
end
2020-07-28 23:09:34 +05:30
def autocomplete
term = params [ :term ]
2022-01-26 12:08:38 +05:30
@project = search_service . project
2020-07-28 23:09:34 +05:30
@ref = params [ :project_ref ] if params [ :project_ref ] . present?
2022-08-27 11:52:29 +05:30
@filter = params [ :filter ]
2020-07-28 23:09:34 +05:30
2023-01-13 00:05:48 +05:30
render json : Gitlab :: Json . dump ( search_autocomplete_opts ( term , filter : @filter ) )
2020-07-28 23:09:34 +05:30
end
2021-03-11 19:13:27 +05:30
def opensearch
end
2023-03-04 22:38:38 +05:30
def elasticsearch_in_use
search_service . respond_to? ( :use_elasticsearch? ) && search_service . use_elasticsearch?
end
strong_memoize_attr :elasticsearch_in_use
2020-06-23 00:09:42 +05:30
private
2014-09-02 18:07:02 +05:30
2023-03-04 22:38:38 +05:30
def update_scope_for_code_search
return if params [ :scope ] == 'blobs'
return unless params [ :search ] . present?
if CODE_SEARCH_LITERALS . any? { | literal | literal . in? params [ :search ] }
redirect_to search_path ( safe_params . except ( :controller , :action ) . merge ( scope : 'blobs' ) )
end
end
2021-01-29 00:20:46 +05:30
# overridden in EE
def default_sort
'created_desc'
end
2020-03-13 15:44:24 +05:30
def search_term_valid?
2022-08-27 11:52:29 +05:30
return false if params [ :search ] . blank?
2020-03-13 15:44:24 +05:30
unless search_service . valid_query_length?
2022-01-26 12:08:38 +05:30
flash [ :alert ] = t ( 'errors.messages.search_chars_too_long' , count : Gitlab :: Search :: Params :: SEARCH_CHAR_LIMIT )
2020-03-13 15:44:24 +05:30
return false
end
unless search_service . valid_terms_count?
2022-01-26 12:08:38 +05:30
flash [ :alert ] = t ( 'errors.messages.search_terms_too_long' , count : Gitlab :: Search :: Params :: SEARCH_TERM_LIMIT )
2020-03-13 15:44:24 +05:30
return false
end
true
end
2021-01-29 00:20:46 +05:30
def check_single_commit_result?
return false if params [ :force_search_results ]
return false unless @project . present?
2023-01-13 00:05:48 +05:30
return false unless Ability . allowed? ( current_user , :read_code , @project )
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
query = params [ :search ] . strip . downcase
return false unless Commit . valid_hash? ( query )
commit = @project . commit_by ( oid : query )
return false unless commit . present?
link = search_path ( safe_params . merge ( force_search_results : true ) )
flash [ :notice ] = html_escape ( _ ( " You have been redirected to the only result; see the %{a_start}search results%{a_end} instead. " ) ) % { a_start : " <a href= \" #{ link } \" ><u> " . html_safe , a_end : '</u></a>' . html_safe }
redirect_to project_commit_path ( @project , commit )
true
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
2020-06-23 00:09:42 +05:30
def increment_search_counters
Gitlab :: UsageDataCounters :: SearchCounter . count ( :all_searches )
2019-10-12 21:52:04 +05:30
return if params [ :nav_source ] != 'navbar'
2020-06-23 00:09:42 +05:30
Gitlab :: UsageDataCounters :: SearchCounter . count ( :navbar_searches )
2019-10-12 21:52:04 +05:30
end
2020-10-24 23:57:45 +05:30
def append_info_to_payload ( payload )
super
# Merging to :metadata will ensure these are logged as top level keys
2020-11-24 15:15:51 +05:30
payload [ :metadata ] || = { }
2020-10-24 23:57:45 +05:30
payload [ :metadata ] [ 'meta.search.group_id' ] = params [ :group_id ]
payload [ :metadata ] [ 'meta.search.project_id' ] = params [ :project_id ]
2021-04-17 20:07:23 +05:30
payload [ :metadata ] [ 'meta.search.scope' ] = params [ :scope ] || @scope
2021-01-29 00:20:46 +05:30
payload [ :metadata ] [ 'meta.search.filters.confidential' ] = params [ :confidential ]
payload [ :metadata ] [ 'meta.search.filters.state' ] = params [ :state ]
payload [ :metadata ] [ 'meta.search.force_search_results' ] = params [ :force_search_results ]
2022-03-02 08:16:31 +05:30
payload [ :metadata ] [ 'meta.search.project_ids' ] = params [ :project_ids ]
2022-08-27 11:52:29 +05:30
payload [ :metadata ] [ 'meta.search.filters.language' ] = params [ :language ]
2022-08-13 15:12:31 +05:30
payload [ :metadata ] [ 'meta.search.type' ] = @search_type if @search_type . present?
payload [ :metadata ] [ 'meta.search.level' ] = @search_level if @search_level . present?
payload [ :metadata ] [ :global_search_duration_s ] = @global_search_duration_s if @global_search_duration_s . present?
2022-01-26 12:08:38 +05:30
if search_service . abuse_detected?
payload [ :metadata ] [ 'abuse.confidence' ] = Gitlab :: Abuse . confidence ( :certain )
payload [ :metadata ] [ 'abuse.messages' ] = search_service . abuse_messages
end
2020-10-24 23:57:45 +05:30
end
2020-11-24 15:15:51 +05:30
def block_anonymous_global_searches
2022-01-26 12:08:38 +05:30
return unless search_service . global_search?
2020-11-24 15:15:51 +05:30
return if current_user
2021-12-11 22:18:48 +05:30
return unless :: Feature . enabled? ( :block_anonymous_global_searches , type : :ops )
2020-11-24 15:15:51 +05:30
store_location_for ( :user , request . fullpath )
redirect_to new_user_session_path , alert : _ ( 'You must be logged in to search across all of GitLab' )
end
2021-09-30 23:02:18 +05:30
2021-11-11 11:23:49 +05:30
def check_scope_global_search_enabled
2022-01-26 12:08:38 +05:30
return unless search_service . global_search?
2021-11-11 11:23:49 +05:30
search_allowed = case params [ :scope ]
when 'blobs'
2022-07-16 23:28:13 +05:30
Feature . enabled? ( :global_search_code_tab , current_user , type : :ops )
2021-11-11 11:23:49 +05:30
when 'commits'
2022-07-16 23:28:13 +05:30
Feature . enabled? ( :global_search_commits_tab , current_user , type : :ops )
2021-11-11 11:23:49 +05:30
when 'issues'
2022-07-16 23:28:13 +05:30
Feature . enabled? ( :global_search_issues_tab , current_user , type : :ops )
2021-11-11 11:23:49 +05:30
when 'merge_requests'
2022-07-16 23:28:13 +05:30
Feature . enabled? ( :global_search_merge_requests_tab , current_user , type : :ops )
2021-11-11 11:23:49 +05:30
when 'wiki_blobs'
2022-07-16 23:28:13 +05:30
Feature . enabled? ( :global_search_wiki_tab , current_user , type : :ops )
2022-06-21 17:19:12 +05:30
when 'users'
2022-07-16 23:28:13 +05:30
Feature . enabled? ( :global_search_users_tab , current_user , type : :ops )
2021-11-11 11:23:49 +05:30
else
true
end
return if search_allowed
redirect_to search_path , alert : _ ( 'Global Search is disabled for this scope' )
end
2021-09-30 23:02:18 +05:30
def render_timeout ( exception )
2021-10-27 15:23:28 +05:30
raise exception unless action_name . to_sym . in? ( RESCUE_FROM_TIMEOUT_ACTIONS )
2021-09-30 23:02:18 +05:30
log_exception ( exception )
@timeout = true
2021-10-27 15:23:28 +05:30
2022-01-26 12:08:38 +05:30
case action_name . to_sym
when :count
2021-10-27 15:23:28 +05:30
render json : { } , status : :request_timeout
2022-08-27 11:52:29 +05:30
when :autocomplete , :aggregations
2022-01-26 12:08:38 +05:30
render json : [ ] , status : :request_timeout
2021-10-27 15:23:28 +05:30
else
render status : :request_timeout
end
end
2022-08-13 15:12:31 +05:30
def tracking_namespace_source
search_service . project & . namespace || search_service . group
end
2023-01-13 00:05:48 +05:30
def tracking_project_source
search_service . project
end
2022-08-13 15:12:31 +05:30
def search_type
'basic'
end
2014-09-02 18:07:02 +05:30
end
2020-11-24 15:15:51 +05:30
2021-06-08 01:23:25 +05:30
SearchController . prepend_mod_with ( 'SearchController' )