Class: SearchService

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from Gitlab::Allowable

#can?

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: prevent_abusive_searches?)
end

Instance Attribute Details

#paramsObject (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

Returns:

  • (Boolean)

76
77
78
79
80
# File 'app/services/search_service.rb', line 76

def abuse_detected?
  strong_memoize(:abuse_detected) do
    params.abusive?
  end
end

#abuse_messagesObject


82
83
84
85
86
# File 'app/services/search_service.rb', line 82

def abuse_messages
  return [] unless params.abusive?

  params.abuse_detection.errors.full_messages
end

#global_search?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'app/services/search_service.rb', line 43

def global_search?
  project.blank? && group.blank?
end

#groupObject

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

#projectObject

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

#projectsObject

rubocop: enable CodeReuse/ActiveRecord


39
40
41
# File 'app/services/search_service.rb', line 39

def projects
  # overridden in EE
end

#search_aggregationsObject


72
73
74
# File 'app/services/search_service.rb', line 72

def search_aggregations
  search_results.aggregations(scope)
end

#search_highlightObject


68
69
70
# File 'app/services/search_service.rb', line 68

def search_highlight
  search_results.highlight_map(scope)
end

#search_objects(preload_method = nil) ⇒ Object


62
63
64
65
66
# File 'app/services/search_service.rb', line 62

def search_objects(preload_method = nil)
  @search_objects ||= redact_unauthorized_results(
    search_results.objects(scope, page: page, per_page: per_page, preload_method: preload_method)
  )
end

#search_resultsObject


56
57
58
59
60
# File 'app/services/search_service.rb', line 56

def search_results
  strong_memoize(:search_results) do
    abuse_detected? ? Gitlab::EmptySearchResults.new : search_service.execute
  end
end

#show_snippets?Boolean

Returns:

  • (Boolean)

47
48
49
50
51
# File 'app/services/search_service.rb', line 47

def show_snippets?
  return @show_snippets if defined?(@show_snippets)

  @show_snippets = params[:snippets] == 'true'
end

#valid_request?Boolean

Returns:

  • (Boolean)

88
89
90
91
92
# File 'app/services/search_service.rb', line 88

def valid_request?
  strong_memoize(:valid_request) do
    params.valid?
  end
end