Commit 966a5270 authored by Francisco Javier López's avatar Francisco Javier López Committed by Peter Leitzen

Limit snippet search count

Limits the search count query for snippets. Instead of showing
the complete number of snippets in the database, the UI will
display a text like 100+ indicating that there are more records
parent 693bd23d
......@@ -9,9 +9,7 @@ module Search
end
def execute
snippets = SnippetsFinder.new(current_user).execute
Gitlab::SnippetSearchResults.new(snippets, params[:search])
Gitlab::SnippetSearchResults.new(current_user, params[:search])
end
def scope
......
---
title: Limit snippets search count
merge_request: 17585
author:
type: performance
......@@ -3,38 +3,49 @@
module Gitlab
module Elastic
class SnippetSearchResults < ::Gitlab::SnippetSearchResults
def initialize(user, query)
@user = user
@query = query
end
def objects(scope, page = nil)
def formatted_count(scope)
case scope
when 'snippet_titles'
snippet_titles.page(page).per(per_page).records
snippet_titles_count.to_s
when 'snippet_blobs'
snippet_blobs.page(page).per(per_page).records
snippet_blobs_count.to_s
else
super
end
end
def snippet_titles_count
limited_snippet_titles_count
end
def snippet_blobs_count
limited_snippet_blobs_count
end
private
def snippet_titles
opt = {
user: @user
}
Snippet.elastic_search(query, options: opt)
Snippet.elastic_search(query, options: search_params)
end
def snippet_blobs
opt = {
user: @user
}
Snippet.elastic_search_code(query, options: search_params)
end
def limited_snippet_titles_count
@limited_snippet_titles_count ||= snippet_titles.total_count
end
def limited_snippet_blobs_count
@limited_snippet_blobs_count ||= snippet_blobs.total_count
end
def paginated_objects(relation, page)
super.records
end
Snippet.elastic_search_code(query, options: opt)
def search_params
{ user: current_user }
end
end
end
......
......@@ -4,19 +4,19 @@ module Gitlab
class SnippetSearchResults < SearchResults
include SnippetsHelper
attr_reader :limit_snippets
attr_reader :current_user
def initialize(limit_snippets, query)
@limit_snippets = limit_snippets
def initialize(current_user, query)
@current_user = current_user
@query = query
end
def objects(scope, page = nil)
case scope
when 'snippet_titles'
snippet_titles.page(page).per(per_page)
paginated_objects(snippet_titles, page)
when 'snippet_blobs'
snippet_blobs.page(page).per(per_page)
paginated_objects(snippet_blobs, page)
else
super(scope, nil, false)
end
......@@ -25,38 +25,47 @@ module Gitlab
def formatted_count(scope)
case scope
when 'snippet_titles'
snippet_titles_count.to_s
formatted_limited_count(limited_snippet_titles_count)
when 'snippet_blobs'
snippet_blobs_count.to_s
formatted_limited_count(limited_snippet_blobs_count)
else
super
end
end
def snippet_titles_count
@snippet_titles_count ||= snippet_titles.count
def limited_snippet_titles_count
@limited_snippet_titles_count ||= limited_count(snippet_titles)
end
def snippet_blobs_count
@snippet_blobs_count ||= snippet_blobs.count
def limited_snippet_blobs_count
@limited_snippet_blobs_count ||= limited_count(snippet_blobs)
end
private
# rubocop: disable CodeReuse/ActiveRecord
def snippet_titles
limit_snippets.search(query).order('updated_at DESC').includes(:author)
def snippets
SnippetsFinder.new(current_user)
.execute
.includes(:author)
.reorder(updated_at: :desc)
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def snippet_titles
snippets.search(query)
end
def snippet_blobs
limit_snippets.search_code(query).order('updated_at DESC').includes(:author)
snippets.search_code(query)
end
# rubocop: enable CodeReuse/ActiveRecord
def default_scope
'snippet_blobs'
end
def paginated_objects(relation, page)
relation.page(page).per(per_page)
end
end
end
......@@ -6,18 +6,17 @@ describe Gitlab::SnippetSearchResults do
include SearchHelpers
let!(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
let(:results) { described_class.new(Snippet.all, 'foo') }
let(:results) { described_class.new(snippet.author, 'foo') }
describe '#snippet_titles_count' do
it 'returns the amount of matched snippet titles' do
expect(results.snippet_titles_count).to eq(1)
expect(results.limited_snippet_titles_count).to eq(1)
end
end
describe '#snippet_blobs_count' do
it 'returns the amount of matched snippet blobs' do
expect(results.snippet_blobs_count).to eq(1)
expect(results.limited_snippet_blobs_count).to eq(1)
end
end
......@@ -25,8 +24,8 @@ describe Gitlab::SnippetSearchResults do
using RSpec::Parameterized::TableSyntax
where(:scope, :count_method, :expected) do
'snippet_titles' | :snippet_titles_count | '1234'
'snippet_blobs' | :snippet_blobs_count | '1234'
'snippet_titles' | :limited_snippet_titles_count | max_limited_count
'snippet_blobs' | :limited_snippet_blobs_count | max_limited_count
'projects' | :limited_projects_count | max_limited_count
'unknown' | nil | nil
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment