<commit_before>class SearchController < ApplicationController

  before_filter :login_required, :except => [ :index]
  after_filter :update_last_user_activity

  def index
    #if nothing has been selected then search for all types
    if !defined? params[:search_type] || params[:search_type].empty?
      params[:search_type] = ['people', 'surveys', 'methods', 'extracts', 'publications', 'variables'] 
    end
    @search_types = params[:search_type]
    @results_hash = Hash.new
    @search_query = params[:search_query]
    @search_query||=""

    #SOLR doesn't appear to support lower case "or"
    query = @search_query.upcase

    if (query.nil? or query.strip.empty?)
      flash[:error]="Sorry your query appeared blank. Please try again"
      redirect_to :back
    end
    #if (query.include?(' OR ') && query.include?(' AND '))
     # flash.now[:notice]='Sorry you can not mix "or" with "and" in the same query. Please try again'
      #return
    #end

    #can only search for people if logged in
    if params[:search_type].include?('people') && logged_in?
      @results_hash['people'] = find_people(query, params[:person_page]).results
    end  
    if params[:search_type].include?('surveys')
      results = find_surveys(query, params[:survey_page]).results
      @results_hash['survey'] = results
      surveys = results.sort!{|x,y| x.title <=> y.title}
      surveys_hash = {"total_entries" => surveys.size, "results"=>surveys.collect{ |s| {"id" => s.id, "title" => s.title, "description" => truncate_words(s.description, 50),  "type" => SurveyType.find(s.survey_type).name, "year" => s.year ? s.year : 'N/A', "source" => s.nesstar_id ? s.nesstar_uri : "methodbox"}}}
      @surveys_json = surveys_hash.to_json
    end 
    if params[:search_type].include?('methods')
      @results_hash['script'] = select_authorised find_methods(query, params[:method_page]).results
    end  
    if params[:search_type].include?('extracts')
     @results_hash['csvarchive'] = select_authorised find_csvarchive(query, params[:csvarchive_page]).results
    end   
    if params[:search_type].include?('publications')
      @results_hash['publication'] = select_authorised find_publications(query, params[:publication_page]).results
    end
    if params[:search_type].include?('variables')
      @results_hash['variable'] = find_variables(query, params[:variable_page]).results.sort!{|x,y| x.name <=> y.name}
    end 

    if @results_hash.empty?<commit_msg>order variables by relevance in general search<commit_after>class SearchController < ApplicationController

  before_filter :login_required, :except => [ :index]
  after_filter :update_last_user_activity

  def index
    #if nothing has been selected then search for all types
    if !defined? params[:search_type] || params[:search_type].empty?
      params[:search_type] = ['people', 'surveys', 'methods', 'extracts', 'publications', 'variables'] 
    end
    @search_types = params[:search_type]
    @results_hash = Hash.new
    @search_query = params[:search_query]
    @search_query||=""

    #SOLR doesn't appear to support lower case "or"
    query = @search_query.upcase

    if (query.nil? or query.strip.empty?)
      flash[:error]="Sorry your query appeared blank. Please try again"
      redirect_to :back
    end
    #if (query.include?(' OR ') && query.include?(' AND '))
     # flash.now[:notice]='Sorry you can not mix "or" with "and" in the same query. Please try again'
      #return
    #end

    #can only search for people if logged in
    if params[:search_type].include?('people') && logged_in?
      @results_hash['people'] = find_people(query, params[:person_page]).results
    end  
    if params[:search_type].include?('surveys')
      results = find_surveys(query, params[:survey_page]).results
      @results_hash['survey'] = results
      surveys = results.sort!{|x,y| x.title <=> y.title}
      surveys_hash = {"total_entries" => surveys.size, "results"=>surveys.collect{ |s| {"id" => s.id, "title" => s.title, "description" => truncate_words(s.description, 50),  "type" => SurveyType.find(s.survey_type).name, "year" => s.year ? s.year : 'N/A', "source" => s.nesstar_id ? s.nesstar_uri : "methodbox"}}}
      @surveys_json = surveys_hash.to_json
    end 
    if params[:search_type].include?('methods')
      @results_hash['script'] = select_authorised find_methods(query, params[:method_page]).results
    end  
    if params[:search_type].include?('extracts')
     @results_hash['csvarchive'] = select_authorised find_csvarchive(query, params[:csvarchive_page]).results
    end   
    if params[:search_type].include?('publications')
      @results_hash['publication'] = select_authorised find_publications(query, params[:publication_page]).results
    end
    if params[:search_type].include?('variables')
      #don't sort variable results but return by order of relevance
      @results_hash['variable'] = find_variables(query, params[:variable_page]).results
    end 

    if @results_hash.empty?
{'fraction_non_alphanumeric': 0.14325730292116848, 'fraction_numerical': 0.0008003201280512205, 'mean_word_length': 4.245540398740818, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 24, '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': '4023776', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1535, 'n_words': 351}