<commit_before>class SearchController < ApplicationController
  include SearchHelper

  before_action :set_variables

  # JSON / HTML API for hospital search
  # parameters:
  # term: search term
  # limit: maximum number of items
  def hospitals
    @hospitals = hospital_search @query

    respond_to do |format|
      format.json { render json: @hospitals.map { |hospital| {:id => hospital.id.to_s, :code => hospital.hospital_id, :text => hospital.name}}}
      format.html { render partial: 'search_results_hospitals' }
    end
  end

  # JSON / HTML API for code search
  # parameters:
  # term: search term
  # limit: maximum number of items
  # level: one of 'drg', 'adrg', 'mdc'
  def codes
    model = {'drg' => Drg, 'mdc' => Mdc, 'adrg' => Adrg}[params[:level]]
    model = Drg if model.nil?

    @codes = code_search model, @query

    respond_to do |format|
      format.json { render json: @codes.map { |code| {:id => code.id.to_s, :code => code.code, :text => code.text(@locale)}}}
      format.html { render partial: 'search_results_codes', locals: {codes: @codes} }
    end
  end

  # JSON / HTML API for codes and hospital search
  # parameters:
  # term_hospital: search term for hospitals
  # term_codes: search term for codes
  # limit: maximum number of items
  def all
    @query_codes = params[:term_codes].blank? ? '' : params[:term_codes]
    @query_hospital = params[:term_hospitals].blank? ? '' : params[:term_hospitals]

    json = {}
    json[:drgs] = @drgs = code_search(Drg, @query_codes)
    json[:adrgs] = @adrgs = code_search(Adrg, @query_codes)
    json[:mdcs] = @mdcs = code_search(Mdc, @query_codes)
    json[:hospitals] = @hospitals = hospital_search @query_hospital

    respond_to do |format|
      format.json { render json: json}
      format.html { render partial: 'systems/search_results' }
    end
  end

  private
    def set_variables
      @system = System.find(params[:system_id])
      @query = params[:term].blank? ? '' : params[:term]
      @limit = params[:limit].blank? ? 5 : params[:limit].to_i
      @locale =  check_locale(params[:locale])
    end

    def check_locale locale
      if(locale != 'fr' && locale != 'it')
        locale = 'de'
      end
      return locale
    end

    def code_search model, query
      return [] if query.blank?
      return model.where("version = '#{@system.version}' and (code ILIKE ? or text_#{@locale} ILIKE ?)", "%#{Regexp.escape(query)}%", "%#{Regexp.escape(query)}%")
                 .order(code: :asc).limit(@limit)
    end

    def hospital_search query
      return [] if query.blank?
      Hospital.where("year = '#{@system.base_year}' and (name ILIKE ? or address ILIKE ?)", "%#{Regexp.escape(query)}%", "%#{Regexp.escape(query)}%")
          .order(name: :asc).limit(@limit)
    end
end<commit_msg>Implement Hospital search with searchkick.<commit_after>class SearchController < ApplicationController
  include SearchHelper

  before_action :set_variables

  # JSON / HTML API for hospital search
  # parameters:
  # term: search term
  # limit: maximum number of items
  def hospitals
    @hospitals = hospital_search @query

    respond_to do |format|
      format.json { render json: @hospitals.map { |hospital| {:id => hospital.id.to_s, :code => hospital.hospital_id, :text => hospital.name}}}
      format.html { render partial: 'search_results_hospitals' }
    end
  end

  # JSON / HTML API for code search
  # parameters:
  # term: search term
  # limit: maximum number of items
  # level: one of 'drg', 'adrg', 'mdc'
  def codes
    model = {'drg' => Drg, 'mdc' => Mdc, 'adrg' => Adrg}[params[:level]]
    model = Drg if model.nil?

    @codes = code_search model, @query

    respond_to do |format|
      format.json { render json: @codes.map { |code| {:id => code.id.to_s, :code => code.code, :text => code.text(@locale)}}}
      format.html { render partial: 'search_results_codes', locals: {codes: @codes} }
    end
  end

  # JSON / HTML API for codes and hospital search
  # parameters:
  # term_hospital: search term for hospitals
  # term_codes: search term for codes
  # limit: maximum number of items
  def all
    @query_codes = params[:term_codes].blank? ? '' : params[:term_codes]
    @query_hospital = params[:term_hospitals].blank? ? '' : params[:term_hospitals]

    json = {}
    json[:drgs] = @drgs = code_search(Drg, @query_codes)
    json[:adrgs] = @adrgs = code_search(Adrg, @query_codes)
    json[:mdcs] = @mdcs = code_search(Mdc, @query_codes)
    json[:hospitals] = @hospitals = hospital_search @query_hospital

    respond_to do |format|
      format.json { render json: json}
      format.html { render partial: 'systems/search_results' }
    end
  end

  private
    def set_variables
      @system = System.find(params[:system_id])
      @query = params[:term].blank? ? '' : params[:term]
      @limit = params[:limit].blank? ? 5 : params[:limit].to_i
      @locale =  check_locale(params[:locale])
    end

    def check_locale locale
      if(locale != 'fr' && locale != 'it')
        locale = 'de'
      end
      return locale
    end

    def code_search model, query
      return [] if query.blank?
      return model.where("version = '#{@system.version}' and (code ILIKE ? or text_#{@locale} ILIKE ?)", "%#{Regexp.escape(query)}%", "%#{Regexp.escape(query)}%")
                 .order(code: :asc).limit(@limit)
    end

    def hospital_search query
      return [] if query.blank?
      Hospital.search query, where: {year: @system.base_year}, limit: @limit
    end
end
{'fraction_non_alphanumeric': 0.1380684906685994, 'fraction_numerical': 0.0003623844899438304, 'mean_word_length': 3.2526964560862868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4608612', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1758, 'n_words': 476}