Module: Blacklight::SearchHelper

Extended by:
ActiveSupport::Concern
Includes:
RequestBuilders
Included in:
Base
Defined in:
app/controllers/concerns/blacklight/search_helper.rb

Overview

SearchHelper is a controller layer mixin. It is in the controller scope: request params, session etc.

NOTE: Be careful when creating variables here as they may be overriding something that already exists. The ActionController docs: api.rubyonrails.org/classes/ActionController/Base.html

Override these methods in your own controller for customizations:

class CatalogController < ActionController::Base
  include Blacklight::Catalog

  def repository_class
    MyAlternativeRepo
  end
end

Or by including in local extensions:

module LocalSearchHelperExtension
  [ local overrides ]
end

class CatalogController < ActionController::Base
  include Blacklight::Catalog
  include LocalSearchHelperExtension

  def repository_class
    MyAlternativeRepo
  end
end

Or by using ActiveSupport::Concern:

module LocalSearchHelperExtension
  extend ActiveSupport::Concern
  include Blacklight::SearchHelper

  [ local overrides ]
end

class CatalogController < ApplicationController
  include LocalSearchHelperExtension
  include Blacklight::Catalog
end

Constant Summary

Constants included from RequestBuilders

RequestBuilders::DEFAULT_FACET_LIMIT

Instance Method Summary collapse

Methods included from RequestBuilders

#facet_limit_for, #previous_and_next_document_params, #search_builder, #solr_opensearch_params

Instance Method Details

#fetch(id = nil, extra_controller_params = {}) ⇒ Blacklight::Solr::Response, Blacklight::SolrDocument

retrieve a document, given the doc id

Parameters:

  • id (Array{#to_s}, #to_s) (defaults to: nil)

Returns:



73
74
75
76
77
78
79
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 73

def fetch(id = nil, extra_controller_params = {})
  if id.is_a? Array
    fetch_many(id, search_state.to_h, extra_controller_params)
  else
    fetch_one(id, extra_controller_params)
  end
end

#get_facet_field_response(facet_field, user_params = params || {}, extra_controller_params = {}) ⇒ Blacklight::Solr::Response

Get the solr response when retrieving only a single facet field

Returns:



84
85
86
87
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 84

def get_facet_field_response(facet_field, user_params = params || {}, extra_controller_params = {})
  query = search_builder.with(user_params).facet(facet_field)
  repository.search(query.merge(extra_controller_params))
end

#get_opensearch_response(field = nil, request_params = params || {}, extra_controller_params = {}) ⇒ Object

a solr query method does a standard search but returns a simplified object. an array is returned, the first item is the query string, the second item is an other array. This second array contains all of the field values for each of the documents… where the field is the “field” argument passed in.



109
110
111
112
113
114
115
116
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 109

def get_opensearch_response(field = nil, request_params = params || {}, extra_controller_params = {})
  field ||= blacklight_config.view_config('opensearch').title_field

  query = search_builder.with(request_params).merge(solr_opensearch_params(field)).merge(extra_controller_params)
  response = repository.search(query)

  [response.params[:q], response.documents.flat_map {|doc| doc[field] }.uniq]
end

#get_previous_and_next_documents_for_search(index, request_params, extra_controller_params = {}) ⇒ Blacklight::Solr::Response, Array<Blacklight::SolrDocument>

Get the previous and next document from a search result

Returns:



91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 91

def get_previous_and_next_documents_for_search(index, request_params, extra_controller_params={})
  p = previous_and_next_document_params(index)
  query = search_builder.with(request_params).start(p.delete(:start)).rows(p.delete(:rows)).merge(extra_controller_params).merge(p)
  response = repository.search(query)
  document_list = response.documents

  # only get the previous doc if there is one
  prev_doc = document_list.first if index > 0
  next_doc = document_list.last if (index + 1) < response.total
  [response, [prev_doc, next_doc]]
end

#grouped_key_for_resultsObject

The key to use to retrieve the grouped field to display



120
121
122
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 120

def grouped_key_for_results
  blacklight_config.index.group
end

#repositoryObject



126
127
128
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 126

def repository
  repository_class.new(blacklight_config)
end

#search_results(user_params) {|search_builder| ... } ⇒ Blacklight::Solr::Response

a solr query method

Parameters:

  • user_params (Hash)

    ({}) the user provided parameters (e.g. query, facets, sort, etc)

Yields:

  • (search_builder)

    optional block yields configured SearchBuilder, caller can modify or create new SearchBuilder to be used. Block should return SearchBuilder to be used.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/concerns/blacklight/search_helper.rb', line 53

def search_results(user_params)
  builder = search_builder.with(user_params)
  builder.page = user_params[:page] if user_params[:page]
  builder.rows = (user_params[:per_page] || user_params[:rows]) if user_params[:per_page] || user_params[:rows]

  builder = yield(builder) if block_given?
  response = repository.search(builder)

  if response.grouped? && grouped_key_for_results
    [response.group(grouped_key_for_results), []]
  elsif response.grouped? && response.grouped.length == 1
    [response.grouped.first, []]
  else
    [response, response.documents]
  end
end