Class: SearchController

Inherits:
ApplicationController show all
Includes:
ExportHelper, PrefixHelper
Defined in:
frontend/app/controllers/search_controller.rb,
public/app/controllers/search_controller.rb

Constant Summary collapse

DEFAULT_SEARCH_FACET_TYPES =
['repository', 'primary_type', 'subjects', 'published_agents', 'langcode']
DEFAULT_SEARCH_OPTS =
{
#    'sort' => 'title_sort asc',
  'resolve[]' => ['repository:id', 'resource:id@compact_resource', 'ancestors:id@compact_resource', 'top_container_uri_u_sstr:id'],
  'facet.mincount' => 1
}
DEFAULT_TYPES =
%w{archival_object digital_object agent resource repository accession classification subject}

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

Methods included from PrefixHelper

app_prefix, #app_prefix, app_prefix_js, #app_prefix_js

Methods included from ExportHelper

#csv_response, #xml_response

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Methods included from JsonHelper

#merge_notes, #process_json_notes

Methods included from Searchable

#default_search_opts, #get_filter_years, #handle_results, #html_notes, #process_results, #process_search_results, #repo_context, #search_terms, #set_up_advanced_search, #set_up_and_run_search, #set_up_search, #strip_facet_fields

Methods included from HandleFaceting

#fetch_only_facets, #get_pretty_facet_value, #strip_facets

Methods included from ManipulateNode

#inheritance, #process_mixed_content, #strip_mixed_content

Instance Method Details

#advanced_searchObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'frontend/app/controllers/search_controller.rb', line 9

def advanced_search
  criteria = params_for_backend_search

  queries = advanced_search_queries.reject {|field|
    (field["value"].nil? || field["value"] == "") && !field["empty"]
  }

  if not queries.empty?
    if criteria['aq']
      existing_filter = ASUtils.json_parse(criteria['aq'])
      criteria['aq'] =  JSONModel::JSONModel(:advanced_query).from_hash({
                            query: JSONModel(:boolean_query)
                                     .from_hash({
                                                  :jsonmodel_type => 'boolean_query',
                                                  :op => 'AND',
                                                  :subqueries => [existing_filter['query'], AdvancedQueryBuilder.build_query_from_form(queries)['query']]
                                                })
                          }).to_json
    else
      criteria["aq"] = AdvancedQueryBuilder.build_query_from_form(queries).to_json
    end
    criteria['facet[]'] = SearchResultData.BASE_FACETS
  end



  respond_to do |format|
    format.json {
      @search_data = Search.all(session[:repo_id], criteria)
      render :json => @search_data
    }
    format.js {
      @search_data = Search.all(session[:repo_id], criteria)
      if params[:listing_only]
        render_aspace_partial :partial => "search/listing"
      else
        render_aspace_partial :partial => "search/results"
      end
    }
    format.html {
      @search_data = Search.all(session[:repo_id], criteria)
      render "search/do_search"
    }
    format.csv {
      uri = "/repositories/#{session[:repo_id]}/search"
      csv_response( uri, Search.build_filters(criteria), "#{I18n.t('search_results.title').downcase}." )
    }
  end
end

#do_searchObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'frontend/app/controllers/search_controller.rb', line 59

def do_search
  @search_data = Search.all(session[:repo_id], params_for_backend_search.merge({"facet[]" => SearchResultData.BASE_FACETS.concat(params[:facets]||[]).uniq}))

  respond_to do |format|
    format.json {
      render :json => @search_data
    }
    format.js {
      if params[:listing_only]
        render_aspace_partial :partial => "search/listing"
      else
        render_aspace_partial :partial => "search/results"
      end
    }
    format.html {
      # default render
    }
    format.csv {
      criteria = params_for_backend_search.merge({"facet[]" => SearchResultData.BASE_FACETS})
      uri = "/repositories/#{session[:repo_id]}/search"
      csv_response( uri, Search.build_filters(criteria), "#{I18n.t('search_results.title').downcase}." )
    }
  end
end

#searchObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'public/app/controllers/search_controller.rb', line 14

def search
  @repo_id = params.fetch(:rid, nil)
  repo_url = "/repositories/#{@repo_id}"
  @base_search = @repo_id ? "#{repo_url}/search?" : '/search?'
  fallback_location = @repo_id ? app_prefix(repo_url) : app_prefix('/search?reset=true');
  @new_search = fallback_location

  if params[:reset] == 'true' || !params.has_key?(:q)
    @reset = true
    params[:rid] = nil
    @search = Search.new(params)
    return render 'search/search_results'
  end

  search_opts = default_search_opts(DEFAULT_SEARCH_OPTS)
  search_opts['fq'] = ["repository:\"#{repo_url}\" OR used_within_published_repository::\"#{repo_url}\""] if @repo_id
  begin
    set_up_advanced_search(DEFAULT_TYPES, DEFAULT_SEARCH_FACET_TYPES, search_opts, params)
#NOTE the redirect back here on error!
  rescue Exception => error
    Rails.logger.debug(error.message)
    p error
    flash[:error] = I18n.t('search_results.error')
    redirect_back(fallback_location: root_path ) and return
  end
  page = Integer(params.fetch(:page, "1"))
  Rails.logger.debug("base search: #{@base_search}")
  Rails.logger.debug("query: #{@query}")

  @results = archivesspace.advanced_search(@base_search, page, @criteria)

  if @results['total_hits'].blank? || @results['total_hits'] == 0
    flash[:notice] = I18n.t('search_results.no_results')
    fallback_location = URI(fallback_location)
    fallback_location.query = URI(@base_search).query + "&reset=true"
    redirect_to(fallback_location.to_s)
  else
    process_search_results(@base_search)
    if @results['total_hits'] > 1
      @search[:dates_within] = true if params.fetch(:filter_from_year, '').blank? && params.fetch(:filter_to_year, '').blank?
      @search[:text_within] = true
    end
    @sort_opts = []
    all_sorts = Search.get_sort_opts
    all_sorts.keys.each do |type|
      @sort_opts.push(all_sorts[type])
    end
    # If there are any repositories in the results, get counts of their collections
    @counts = {}
    @results.records.each do |result|
      if result['primary_type'] == 'repository'
        @counts[result.uri] = get_collection_counts(result.uri)
      end
    end
    render 'search/search_results'
  end
end