Class: Gitlab::SearchResults

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/search_results.rb

Constant Summary collapse

COUNT_LIMIT =
100
COUNT_LIMIT_MESSAGE =
"#{COUNT_LIMIT - 1}+".freeze
DEFAULT_PAGE =
1
DEFAULT_PER_PAGE =
20
SCOPE_ONLY_SORT =
{
  popularity_asc: %w[issues],
  popularity_desc: %w[issues]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user, query, limit_projects = nil, order_by: nil, sort: nil, default_project_filter: false, filters: {}) ⇒ SearchResults

Returns a new instance of SearchResults.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/search_results.rb', line 27

def initialize(
  current_user,
  query,
  limit_projects = nil,
  order_by: nil,
  sort: nil,
  default_project_filter: false,
  filters: {})
  @current_user = current_user
  @query = query
  @limit_projects = limit_projects || Project.all
  @default_project_filter = default_project_filter
  @order_by = order_by
  @sort = sort
  @filters = filters
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



15
16
17
# File 'lib/gitlab/search_results.rb', line 15

def current_user
  @current_user
end

#default_project_filterObject (readonly)

Whether a custom filter is used to restrict scope of projects. If the default filter (which lists all projects user has access to) is used, we can skip it when filtering merge requests and optimize the query



25
26
27
# File 'lib/gitlab/search_results.rb', line 25

def default_project_filter
  @default_project_filter
end

#filtersObject (readonly)

Returns the value of attribute filters.



15
16
17
# File 'lib/gitlab/search_results.rb', line 15

def filters
  @filters
end

#limit_projectsObject (readonly)

Limit search results by passed projects It allows us to search only for projects user has access to



19
20
21
# File 'lib/gitlab/search_results.rb', line 19

def limit_projects
  @limit_projects
end

#order_byObject (readonly)

Returns the value of attribute order_by.



15
16
17
# File 'lib/gitlab/search_results.rb', line 15

def order_by
  @order_by
end

#queryObject (readonly)

Returns the value of attribute query.



15
16
17
# File 'lib/gitlab/search_results.rb', line 15

def query
  @query
end

#sortObject (readonly)

Returns the value of attribute sort.



15
16
17
# File 'lib/gitlab/search_results.rb', line 15

def sort
  @sort
end

Instance Method Details

#aggregations(_scope) ⇒ Object

aggregations are only performed by Elasticsearch backed results



126
127
128
# File 'lib/gitlab/search_results.rb', line 126

def aggregations(_scope)
  []
end

#count_limitObject



110
111
112
# File 'lib/gitlab/search_results.rb', line 110

def count_limit
  COUNT_LIMIT
end

#errorObject



134
135
136
# File 'lib/gitlab/search_results.rb', line 134

def error
  nil
end

#failed?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/gitlab/search_results.rb', line 130

def failed?
  false
end

#formatted_count(scope) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gitlab/search_results.rb', line 59

def formatted_count(scope)
  case scope
  when 'projects'
    formatted_limited_count(limited_projects_count)
  when 'issues'
    formatted_limited_count(limited_issues_count)
  when 'merge_requests'
    formatted_limited_count(limited_merge_requests_count)
  when 'milestones'
    formatted_limited_count(limited_milestones_count)
  when 'users'
    formatted_limited_count(limited_users_count)
  end
end

#formatted_limited_count(count) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/gitlab/search_results.rb', line 74

def formatted_limited_count(count)
  if count >= COUNT_LIMIT
    COUNT_LIMIT_MESSAGE
  else
    count.to_s
  end
end

#highlight_map(_scope) ⇒ Object

highlighting is only performed by Elasticsearch backed results



121
122
123
# File 'lib/gitlab/search_results.rb', line 121

def highlight_map(_scope)
  {}
end

#limited_issues_countObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gitlab/search_results.rb', line 86

def limited_issues_count
  return @limited_issues_count if @limited_issues_count

  # By default getting limited count (e.g. 1000+) is fast on issuable
  # collections except for issues, where filtering both not confidential
  # and confidential issues user has access to, is too complex.
  # It's faster to try to fetch all public issues first, then only
  # if necessary try to fetch all issues.
  sum = limited_count(issues(public_only: true))
  @limited_issues_count = sum < count_limit ? limited_count(issues) : sum
end

#limited_merge_requests_countObject



98
99
100
# File 'lib/gitlab/search_results.rb', line 98

def limited_merge_requests_count
  @limited_merge_requests_count ||= limited_count(merge_requests)
end

#limited_milestones_countObject



102
103
104
# File 'lib/gitlab/search_results.rb', line 102

def limited_milestones_count
  @limited_milestones_count ||= limited_count(milestones)
end

#limited_projects_countObject



82
83
84
# File 'lib/gitlab/search_results.rb', line 82

def limited_projects_count
  @limited_projects_count ||= limited_count(projects)
end

#limited_users_countObject



106
107
108
# File 'lib/gitlab/search_results.rb', line 106

def limited_users_count
  @limited_users_count ||= limited_count(users)
end

#objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitlab/search_results.rb', line 44

def objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil)
  should_preload = preload_method.present?
  collection = collection_for(scope)

  if collection.nil?
    should_preload = false
    collection = Kaminari.paginate_array([])
  end

  collection = collection.public_send(preload_method) if should_preload # rubocop:disable GitlabSecurity/PublicSend
  collection = collection.page(page).per(per_page)

  without_count ? collection.without_count : collection
end

#usersObject



114
115
116
117
118
# File 'lib/gitlab/search_results.rb', line 114

def users
  return User.none unless Ability.allowed?(current_user, :read_users_list)

  UsersFinder.new(current_user, { search: query, use_minimum_char_limit: false }).execute
end