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}+"
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
# 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


119
120
121
# File 'lib/gitlab/search_results.rb', line 119

def aggregations(scope)
  []
end

#count_limitObject


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

def count_limit
  COUNT_LIMIT
end

#formatted_count(scope) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/search_results.rb', line 52

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


67
68
69
70
71
72
73
# File 'lib/gitlab/search_results.rb', line 67

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


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

def highlight_map(scope)
  {}
end

#limited_issues_countObject


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gitlab/search_results.rb', line 79

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


91
92
93
# File 'lib/gitlab/search_results.rb', line 91

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

#limited_milestones_countObject


95
96
97
# File 'lib/gitlab/search_results.rb', line 95

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

#limited_projects_countObject


75
76
77
# File 'lib/gitlab/search_results.rb', line 75

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

#limited_users_countObject


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

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/search_results.rb', line 37

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


107
108
109
110
111
# File 'lib/gitlab/search_results.rb', line 107

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

  UsersFinder.new(current_user, search: query).execute
end