Commit ade96e25 authored by Dmitry Gruzd's avatar Dmitry Gruzd

Push if condition to the search_service level

Implement 2 methods for SearchService and override them for EE,
that way we hide ES details and separate logic
parent a1f68852
...@@ -5,9 +5,6 @@ class SearchController < ApplicationController ...@@ -5,9 +5,6 @@ class SearchController < ApplicationController
include SearchHelper include SearchHelper
include RendersCommits include RendersCommits
NON_ES_SEARCH_TERM_LIMIT = 64
NON_ES_SEARCH_CHAR_LIMIT = 4096
around_action :allow_gitaly_ref_name_caching around_action :allow_gitaly_ref_name_caching
skip_before_action :authenticate_user! skip_before_action :authenticate_user!
...@@ -68,19 +65,13 @@ class SearchController < ApplicationController ...@@ -68,19 +65,13 @@ class SearchController < ApplicationController
private private
def search_term_valid? def search_term_valid?
return true if Gitlab::CurrentSettings.elasticsearch_search? && search_service.try(:use_elasticsearch?) unless search_service.valid_query_length?
flash[:alert] = t('errors.messages.search_chars_too_long', count: SearchService::SEARCH_CHAR_LIMIT)
chars_count = params[:search].length
if chars_count > NON_ES_SEARCH_CHAR_LIMIT
flash[:alert] = t('errors.messages.search_chars_too_long', count: NON_ES_SEARCH_CHAR_LIMIT)
return false return false
end end
search_terms_count = params[:search].split.count { |word| word.length >= 3 } unless search_service.valid_terms_count?
if search_terms_count > NON_ES_SEARCH_TERM_LIMIT flash[:alert] = t('errors.messages.search_terms_too_long', count: SearchService::SEARCH_TERM_LIMIT)
flash[:alert] = t('errors.messages.search_terms_too_long', count: NON_ES_SEARCH_TERM_LIMIT)
return false return false
end end
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
class SearchService class SearchService
include Gitlab::Allowable include Gitlab::Allowable
SEARCH_TERM_LIMIT = 64
SEARCH_CHAR_LIMIT = 4096
def initialize(current_user, params = {}) def initialize(current_user, params = {})
@current_user = current_user @current_user = current_user
@params = params.dup @params = params.dup
...@@ -42,6 +45,14 @@ class SearchService ...@@ -42,6 +45,14 @@ class SearchService
@show_snippets = params[:snippets] == 'true' @show_snippets = params[:snippets] == 'true'
end end
def valid_query_length?
params[:search].length <= SEARCH_CHAR_LIMIT
end
def valid_terms_count?
params[:search].split.count { |word| word.length >= 3 } <= SEARCH_TERM_LIMIT
end
delegate :scope, to: :search_service delegate :scope, to: :search_service
def search_results def search_results
......
...@@ -53,6 +53,18 @@ module EE ...@@ -53,6 +53,18 @@ module EE
) )
end end
def valid_query_length?
return true if use_elasticsearch?
super
end
def valid_terms_count?
return true if use_elasticsearch?
super
end
private private
def logger def logger
......
...@@ -106,8 +106,8 @@ describe SearchController do ...@@ -106,8 +106,8 @@ describe SearchController do
context 'check search term length' do context 'check search term length' do
let(:search_queries) do let(:search_queries) do
char_limit = controller.class::NON_ES_SEARCH_CHAR_LIMIT char_limit = SearchService::SEARCH_CHAR_LIMIT
term_limit = controller.class::NON_ES_SEARCH_TERM_LIMIT term_limit = SearchService::SEARCH_TERM_LIMIT
{ {
chars_under_limit: ('a' * (char_limit - 1)), chars_under_limit: ('a' * (char_limit - 1)),
chars_over_limit: ('a' * (char_limit + 1)), chars_over_limit: ('a' * (char_limit + 1)),
......
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