def scoped_search(query, default_sort_by, default_sort_order, options = {})
      params[:sort_by] ||= default_sort_by
      params[:sort_order] ||= default_sort_order

      resource = options[:resource_class] || resource_class
      includes = options.fetch(:includes, [])
      group = options.fetch(:group, nil)
      params[:full_result] = true if options[:csv]
      blank_query = resource.none

      if params[:order]
        (params[:sort_by], params[:sort_order]) = params[:order].split(' ')
      else
        params[:order] = "#{params[:sort_by]} #{params[:sort_order]}"
      end

      total = scoped_search_total(query, group)

      query = query.select(:id) if query.respond_to?(:select)
      query = resource.search_for(*search_options).where("#{resource.table_name}.id" => query)

      query = self.final_custom_index_relation(query) if self.respond_to?(:final_custom_index_relation)

      query = query.select(group).group(group) if group
      sub_total = total.zero? ? 0 : scoped_search_total(query, group)

      if options[:custom_sort]
        query = options[:custom_sort].call(query)
      end
      query = query.order("#{query.table_name}.id DESC") unless group #secondary order to ensure sort is deterministic
      query = query.includes(includes) if includes.length > 0

      if ::Foreman::Cast.to_bool(params[:full_result])
        params[:per_page] = total
      else
        query = query.paginate(paginate_options)
      end
      page = params[:page] || 1
      per_page = params[:per_page] || Setting[:entries_per_page]
      query = (total.zero? || sub_total.zero?) ? blank_query : query

      options[:csv] ? query : scoped_search_results(query, sub_total, total, page, per_page)
    rescue ScopedSearch::QueryNotSupported, ActiveRecord::StatementInvalid => error
      message = error.message
      if error.class == ActiveRecord::StatementInvalid
        Rails.logger.error("Invalid search: #{error.message}")
        message = _('Your search query was invalid. Please revise it and try again. The full error has been sent to the application logs.')
      end

      scoped_search_results(blank_query, 0, 0, page, per_page, message)
    end
def scoped_search(query, default_sort_by, default_sort_order, options = {})
      params[:sort_by] ||= default_sort_by
      params[:sort_order] ||= default_sort_order

      resource = options[:resource_class] || resource_class
      includes = options.fetch(:includes, [])
      group = options.fetch(:group, nil)
      params[:full_result] = true if options[:csv]
      blank_query = resource.none

      if params[:order]
        (params[:sort_by], params[:sort_order]) = params[:order].split(' ')
      else
        params[:order] = "#{params[:sort_by]} #{params[:sort_order]}"
      end

      total = scoped_search_total(query, group)

      query = query.select(:id) if query.respond_to?(:select)
      query = resource.search_for(*search_options).where("#{resource.table_name}.id" => query)

      query = self.final_custom_index_relation(query) if self.respond_to?(:final_custom_index_relation)

      query = query.select(group).group(group) if group
      sub_total = total.zero? ? 0 : scoped_search_total(query, group)

      if options[:custom_sort]
        query = options[:custom_sort].call(query)
      end
      query = query.order("#{query.table_name}.id DESC") unless group #secondary order to ensure sort is deterministic
      query = query.includes(includes) if includes.length > 0

      if ::Foreman::Cast.to_bool(params[:full_result])
        params[:per_page] = total
      else
        query = query.paginate(paginate_options)
      end
      page = params[:page] || 1
      per_page = params[:per_page] || Setting[:entries_per_page]
      query = (total.zero? || sub_total.zero?) ? blank_query : query

      options[:csv] ? query : scoped_search_results(query, sub_total, total, page, per_page)
    rescue ScopedSearch::QueryNotSupported, ActiveRecord::StatementInvalid => error
      message = error.message
      if error.class == ActiveRecord::StatementInvalid
        Rails.logger.error("Invalid search: #{error.message}")
        message = _('Your search query was invalid. Please revise it and try again. The full error has been sent to the application logs.')
      end

      scoped_search_results(blank_query, 0, 0, page, per_page, message)
    end
rubocop:disable Metrics/CyclomaticComplexity
 rubocop:disable Metrics/MethodLength
 rubocop:disable Metrics/AbcSize
 rubocop:disable Metrics/PerceivedComplexity