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
include SearchHelper
include RendersCommits
NON_ES_SEARCH_TERM_LIMIT = 64
NON_ES_SEARCH_CHAR_LIMIT = 4096
around_action :allow_gitaly_ref_name_caching
skip_before_action :authenticate_user!
......@@ -68,19 +65,13 @@ class SearchController < ApplicationController
private
def search_term_valid?
return true if Gitlab::CurrentSettings.elasticsearch_search? && search_service.try(:use_elasticsearch?)
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)
unless search_service.valid_query_length?
flash[:alert] = t('errors.messages.search_chars_too_long', count: SearchService::SEARCH_CHAR_LIMIT)
return false
end
search_terms_count = params[:search].split.count { |word| word.length >= 3 }
if search_terms_count > NON_ES_SEARCH_TERM_LIMIT
flash[:alert] = t('errors.messages.search_terms_too_long', count: NON_ES_SEARCH_TERM_LIMIT)
unless search_service.valid_terms_count?
flash[:alert] = t('errors.messages.search_terms_too_long', count: SearchService::SEARCH_TERM_LIMIT)
return false
end
......
......@@ -3,6 +3,9 @@
class SearchService
include Gitlab::Allowable
SEARCH_TERM_LIMIT = 64
SEARCH_CHAR_LIMIT = 4096
def initialize(current_user, params = {})
@current_user = current_user
@params = params.dup
......@@ -42,6 +45,14 @@ class SearchService
@show_snippets = params[:snippets] == 'true'
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
def search_results
......
......@@ -53,6 +53,18 @@ module EE
)
end
def valid_query_length?
return true if use_elasticsearch?
super
end
def valid_terms_count?
return true if use_elasticsearch?
super
end
private
def logger
......
......@@ -106,8 +106,8 @@ describe SearchController do
context 'check search term length' do
let(:search_queries) do
char_limit = controller.class::NON_ES_SEARCH_CHAR_LIMIT
term_limit = controller.class::NON_ES_SEARCH_TERM_LIMIT
char_limit = SearchService::SEARCH_CHAR_LIMIT
term_limit = SearchService::SEARCH_TERM_LIMIT
{
chars_under_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