Class: SearchService
- Inherits:
-
Object
- Object
- SearchService
- Includes:
- Gitlab::Allowable, Gitlab::Utils::StrongMemoize
- Defined in:
- app/services/search_service.rb
Constant Summary collapse
- DEFAULT_PER_PAGE =
Gitlab::SearchResults::DEFAULT_PER_PAGE
- MAX_PER_PAGE =
200
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #abuse_detected? ⇒ Boolean
- #abuse_messages ⇒ Object
- #global_search? ⇒ Boolean
- #global_search_enabled_for_scope? ⇒ Boolean
-
#group ⇒ Object
rubocop: disable CodeReuse/ActiveRecord.
-
#initialize(current_user, params = {}) ⇒ SearchService
constructor
A new instance of SearchService.
- #level ⇒ Object
-
#project ⇒ Object
rubocop: disable CodeReuse/ActiveRecord.
-
#projects ⇒ Object
rubocop: enable CodeReuse/ActiveRecord.
- #search_aggregations ⇒ Object
- #search_highlight ⇒ Object
- #search_objects(preload_method = nil) ⇒ Object
- #search_results ⇒ Object
- #search_type ⇒ Object
- #show_snippets? ⇒ Boolean
- #valid_request? ⇒ Boolean
Methods included from Gitlab::Allowable
Constructor Details
#initialize(current_user, params = {}) ⇒ SearchService
Returns a new instance of SearchService.
12 13 14 15 |
# File 'app/services/search_service.rb', line 12 def initialize(current_user, params = {}) @current_user = current_user @params = Gitlab::Search::Params.new(params, detect_abuse: true) end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'app/services/search_service.rb', line 10 def params @params end |
Instance Method Details
#abuse_detected? ⇒ Boolean
80 81 82 83 84 |
# File 'app/services/search_service.rb', line 80 def abuse_detected? strong_memoize(:abuse_detected) do params.abusive? end end |
#abuse_messages ⇒ Object
86 87 88 89 90 |
# File 'app/services/search_service.rb', line 86 def return [] unless params.abusive? params.abuse_detection.errors. end |
#global_search? ⇒ Boolean
43 44 45 |
# File 'app/services/search_service.rb', line 43 def global_search? project.blank? && group.blank? end |
#global_search_enabled_for_scope? ⇒ Boolean
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/services/search_service.rb', line 109 def global_search_enabled_for_scope? return false if show_snippets? && Feature.disabled?(:global_search_snippet_titles_tab, current_user, type: :ops) case params[:scope] when 'blobs' Feature.enabled?(:global_search_code_tab, current_user, type: :ops) when 'commits' Feature.enabled?(:global_search_commits_tab, current_user, type: :ops) when 'issues' Feature.enabled?(:global_search_issues_tab, current_user, type: :ops) when 'merge_requests' Feature.enabled?(:global_search_merge_requests_tab, current_user, type: :ops) when 'snippet_titles' Feature.enabled?(:global_search_snippet_titles_tab, current_user, type: :ops) when 'wiki_blobs' Feature.enabled?(:global_search_wiki_tab, current_user, type: :ops) when 'users' Feature.enabled?(:global_search_users_tab, current_user, type: :ops) else true end end |
#group ⇒ Object
rubocop: disable CodeReuse/ActiveRecord
29 30 31 32 33 34 35 36 |
# File 'app/services/search_service.rb', line 29 def group strong_memoize(:group) do if params[:group_id].present? && valid_request? the_group = Group.find_by(id: params[:group_id]) can?(current_user, :read_group, the_group) ? the_group : nil end end end |
#level ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'app/services/search_service.rb', line 98 def level @level ||= if project 'project' elsif group 'group' else 'global' end end |
#project ⇒ Object
rubocop: disable CodeReuse/ActiveRecord
18 19 20 21 22 23 24 25 |
# File 'app/services/search_service.rb', line 18 def project strong_memoize(:project) do if params[:project_id].present? && valid_request? the_project = Project.find_by(id: params[:project_id]) can?(current_user, :read_project, the_project) ? the_project : nil end end end |
#projects ⇒ Object
rubocop: enable CodeReuse/ActiveRecord
39 40 41 |
# File 'app/services/search_service.rb', line 39 def projects # overridden in EE end |
#search_aggregations ⇒ Object
76 77 78 |
# File 'app/services/search_service.rb', line 76 def search_aggregations search_results.aggregations(scope) end |
#search_highlight ⇒ Object
72 73 74 |
# File 'app/services/search_service.rb', line 72 def search_highlight search_results.highlight_map(scope) end |
#search_objects(preload_method = nil) ⇒ Object
66 67 68 69 70 |
# File 'app/services/search_service.rb', line 66 def search_objects(preload_method = nil) @search_objects ||= ( search_results.objects(scope, page: page, per_page: per_page, preload_method: preload_method) ) end |
#search_results ⇒ Object
60 61 62 63 64 |
# File 'app/services/search_service.rb', line 60 def search_results strong_memoize(:search_results) do abuse_detected? ? Gitlab::EmptySearchResults.new : search_service.execute end end |
#search_type ⇒ Object
47 48 49 |
# File 'app/services/search_service.rb', line 47 def search_type 'basic' end |
#show_snippets? ⇒ Boolean
51 52 53 54 55 |
# File 'app/services/search_service.rb', line 51 def show_snippets? strong_memoize(:show_snippets) do params[:snippets] == 'true' end end |
#valid_request? ⇒ Boolean
92 93 94 95 96 |
# File 'app/services/search_service.rb', line 92 def valid_request? strong_memoize(:valid_request) do params.valid? end end |