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

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: {}, source: nil) ⇒ SearchResults

Returns a new instance of SearchResults.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/search_results.rb', line 22

def initialize(
  current_user,
  query,
  limit_projects = nil,
  order_by: nil,
  sort: nil,
  default_project_filter: false,
  filters: {},
  source: nil
)
  @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
  @source = source
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



10
11
12
# File 'lib/gitlab/search_results.rb', line 10

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



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

def default_project_filter
  @default_project_filter
end

#filtersObject (readonly)

Returns the value of attribute filters.



10
11
12
# File 'lib/gitlab/search_results.rb', line 10

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



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

def limit_projects
  @limit_projects
end

#order_byObject (readonly)

Returns the value of attribute order_by.



10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def order_by
  @order_by
end

#queryObject (readonly)

Returns the value of attribute query.



10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def query
  @query
end

#sortObject (readonly)

Returns the value of attribute sort.



10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def sort
  @sort
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def source
  @source
end

Instance Method Details

#aggregationsObject

aggregations are only performed by Elasticsearch backed results



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

def aggregations(*)
  []
end

#count_limitObject



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

def count_limit
  COUNT_LIMIT
end

#countsObject

direct counts are only performed by Elasticsearch backed results



136
137
138
# File 'lib/gitlab/search_results.rb', line 136

def counts(*)
  {}
end

#errorObject



144
145
146
# File 'lib/gitlab/search_results.rb', line 144

def error(*)
  nil
end

#failed?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/gitlab/search_results.rb', line 140

def failed?(*)
  false
end

#formatted_count(scope) ⇒ Object



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

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



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

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

#highlight_mapObject

highlighting is only performed by Elasticsearch backed results



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

def highlight_map(*)
  {}
end

#limited_issues_countObject



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

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(confidential: false))
  @limited_issues_count = sum < count_limit ? limited_count(issues) : sum
end

#limited_merge_requests_countObject



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

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

#limited_milestones_countObject



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

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

#limited_projects_countObject



80
81
82
# File 'lib/gitlab/search_results.rb', line 80

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

#limited_users_countObject



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

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



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

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



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gitlab/search_results.rb', line 112

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

  params = { search: query, use_minimum_char_limit: false }

  if current_user && filters[:autocomplete]
    params[:group_member_source_ids] = current_user_authorized_group_ids
    params[:project_member_source_ids] = current_user_authorized_project_ids
  end

  UsersFinder.new(current_user, params).execute
end