Commit 72e1c25a authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'fj-32643-limit-snippets-search-count' into 'master'

Limit snippet search count

Closes #32643

See merge request gitlab-org/gitlab!17585
parents 693bd23d 966a5270
...@@ -9,9 +9,7 @@ module Search ...@@ -9,9 +9,7 @@ module Search
end end
def execute def execute
snippets = SnippetsFinder.new(current_user).execute Gitlab::SnippetSearchResults.new(current_user, params[:search])
Gitlab::SnippetSearchResults.new(snippets, params[:search])
end end
def scope def scope
......
---
title: Limit snippets search count
merge_request: 17585
author:
type: performance
...@@ -3,38 +3,49 @@ ...@@ -3,38 +3,49 @@
module Gitlab module Gitlab
module Elastic module Elastic
class SnippetSearchResults < ::Gitlab::SnippetSearchResults class SnippetSearchResults < ::Gitlab::SnippetSearchResults
def initialize(user, query) def formatted_count(scope)
@user = user
@query = query
end
def objects(scope, page = nil)
case scope case scope
when 'snippet_titles' when 'snippet_titles'
snippet_titles.page(page).per(per_page).records snippet_titles_count.to_s
when 'snippet_blobs' when 'snippet_blobs'
snippet_blobs.page(page).per(per_page).records snippet_blobs_count.to_s
else else
super super
end end
end end
def snippet_titles_count
limited_snippet_titles_count
end
def snippet_blobs_count
limited_snippet_blobs_count
end
private private
def snippet_titles def snippet_titles
opt = { Snippet.elastic_search(query, options: search_params)
user: @user
}
Snippet.elastic_search(query, options: opt)
end end
def snippet_blobs def snippet_blobs
opt = { Snippet.elastic_search_code(query, options: search_params)
user: @user 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 end
end end
......
...@@ -4,19 +4,19 @@ module Gitlab ...@@ -4,19 +4,19 @@ module Gitlab
class SnippetSearchResults < SearchResults class SnippetSearchResults < SearchResults
include SnippetsHelper include SnippetsHelper
attr_reader :limit_snippets attr_reader :current_user
def initialize(limit_snippets, query) def initialize(current_user, query)
@limit_snippets = limit_snippets @current_user = current_user
@query = query @query = query
end end
def objects(scope, page = nil) def objects(scope, page = nil)
case scope case scope
when 'snippet_titles' when 'snippet_titles'
snippet_titles.page(page).per(per_page) paginated_objects(snippet_titles, page)
when 'snippet_blobs' when 'snippet_blobs'
snippet_blobs.page(page).per(per_page) paginated_objects(snippet_blobs, page)
else else
super(scope, nil, false) super(scope, nil, false)
end end
...@@ -25,38 +25,47 @@ module Gitlab ...@@ -25,38 +25,47 @@ module Gitlab
def formatted_count(scope) def formatted_count(scope)
case scope case scope
when 'snippet_titles' when 'snippet_titles'
snippet_titles_count.to_s formatted_limited_count(limited_snippet_titles_count)
when 'snippet_blobs' when 'snippet_blobs'
snippet_blobs_count.to_s formatted_limited_count(limited_snippet_blobs_count)
else else
super super
end end
end end
def snippet_titles_count def limited_snippet_titles_count
@snippet_titles_count ||= snippet_titles.count @limited_snippet_titles_count ||= limited_count(snippet_titles)
end end
def snippet_blobs_count def limited_snippet_blobs_count
@snippet_blobs_count ||= snippet_blobs.count @limited_snippet_blobs_count ||= limited_count(snippet_blobs)
end end
private private
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def snippet_titles def snippets
limit_snippets.search(query).order('updated_at DESC').includes(:author) SnippetsFinder.new(current_user)
.execute
.includes(:author)
.reorder(updated_at: :desc)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord def snippet_titles
snippets.search(query)
end
def snippet_blobs def snippet_blobs
limit_snippets.search_code(query).order('updated_at DESC').includes(:author) snippets.search_code(query)
end end
# rubocop: enable CodeReuse/ActiveRecord
def default_scope def default_scope
'snippet_blobs' 'snippet_blobs'
end end
def paginated_objects(relation, page)
relation.page(page).per(per_page)
end
end end
end end
...@@ -6,18 +6,17 @@ describe Gitlab::SnippetSearchResults do ...@@ -6,18 +6,17 @@ describe Gitlab::SnippetSearchResults do
include SearchHelpers include SearchHelpers
let!(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') } let!(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
let(:results) { described_class.new(snippet.author, 'foo') }
let(:results) { described_class.new(Snippet.all, 'foo') }
describe '#snippet_titles_count' do describe '#snippet_titles_count' do
it 'returns the amount of matched snippet titles' 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
end end
describe '#snippet_blobs_count' do describe '#snippet_blobs_count' do
it 'returns the amount of matched snippet blobs' 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
end end
...@@ -25,8 +24,8 @@ describe Gitlab::SnippetSearchResults do ...@@ -25,8 +24,8 @@ describe Gitlab::SnippetSearchResults do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:scope, :count_method, :expected) do where(:scope, :count_method, :expected) do
'snippet_titles' | :snippet_titles_count | '1234' 'snippet_titles' | :limited_snippet_titles_count | max_limited_count
'snippet_blobs' | :snippet_blobs_count | '1234' 'snippet_blobs' | :limited_snippet_blobs_count | max_limited_count
'projects' | :limited_projects_count | max_limited_count 'projects' | :limited_projects_count | max_limited_count
'unknown' | nil | nil 'unknown' | nil | nil
end 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