class Blacklight::Solr::Repository

Public Instance Methods

build_solr_request(solr_params) click to toggle source

@return [Hash] @!visibility private

# File lib/blacklight/solr/repository.rb, line 89
def build_solr_request(solr_params)
  if uses_json_query_dsl?(solr_params)
    {
      data: { params: solr_params.to_hash.except(:json) }.merge(solr_params[:json]).to_json,
      method: :post,
      headers: { 'Content-Type' => 'application/json' }
    }
  else
    key = blacklight_config.http_method == :post ? :data : :params
    {
      key => solr_params.to_hash,
      method: blacklight_config.http_method
    }
  end
end
find(id, params = {}) click to toggle source

Find a single solr document result (by id) using the document configuration @param [String] id document’s unique key value @param [Hash] params additional solr query parameters

# File lib/blacklight/solr/repository.rb, line 9
def find id, params = {}
  doc_params = params.reverse_merge(blacklight_config.default_document_solr_params)
                     .reverse_merge(qt: blacklight_config.document_solr_request_handler)
                     .merge(blacklight_config.document_unique_id_param => id)

  solr_response = send_and_receive blacklight_config.document_solr_path || blacklight_config.solr_path, doc_params
  raise Blacklight::Exceptions::RecordNotFound if solr_response.documents.empty?

  solr_response
end
find_many(params) click to toggle source

Find multiple documents by their ids @param [Hash] _params query parameters

# File lib/blacklight/solr/repository.rb, line 22
def find_many(params)
  search(params: params, path: blacklight_config.fetch_many_documents_path)
end
ping() click to toggle source

@return [boolean] true if the repository is reachable

# File lib/blacklight/solr/repository.rb, line 52
def ping
  response = connection.send_and_receive 'admin/ping', {}
  Blacklight.logger&.info("Ping [#{connection.uri}] returned: '#{response['status']}'")
  response['status'] == "OK"
end
reflect_fields() click to toggle source

Gets a list of available fields @return [Hash]

# File lib/blacklight/solr/repository.rb, line 46
def reflect_fields
  send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end
send_and_receive(path, solr_params = {}) click to toggle source

Execute a solr query TODO: Make this private after we have a way to abstract admin/luke and ping @see [RSolr::Client#send_and_receive] @overload find(solr_path, params)

Execute a solr query at the given path with the parameters
@param [String] solr path (defaults to blacklight_config.solr_path)
@param [Hash] parameters for RSolr::Client#send_and_receive

@overload find(params)

@param [Hash] parameters for RSolr::Client#send_and_receive

@return [Blacklight::Solr::Response] the solr response object

# File lib/blacklight/solr/repository.rb, line 69
def send_and_receive(path, solr_params = {})
  benchmark("Solr fetch", level: :debug) do
    res = connection.send_and_receive(path, build_solr_request(solr_params))
    solr_response = blacklight_config.response_model.new(res, solr_params, document_model: blacklight_config.document_model, blacklight_config: blacklight_config)

    Blacklight.logger&.debug("Solr query: #{blacklight_config.http_method} #{path} #{solr_params.to_hash.inspect}")
    Blacklight.logger&.debug("Solr response: #{solr_response.inspect}") if defined?(::BLACKLIGHT_VERBOSE_LOGGING) && ::BLACKLIGHT_VERBOSE_LOGGING
    solr_response
  end
rescue *defined_rsolr_timeout_exceptions => e
  raise Blacklight::Exceptions::RepositoryTimeout, "Timeout connecting to Solr instance using #{connection.inspect}: #{e.inspect}"
rescue Errno::ECONNREFUSED => e
  # intended for and likely to be a RSolr::Error:ConnectionRefused, specifically.
  raise Blacklight::Exceptions::ECONNREFUSED, "Unable to connect to Solr instance using #{connection.inspect}: #{e.inspect}"
rescue RSolr::Error::Http => e
  raise Blacklight::Exceptions::InvalidRequest, e.message
end
suggestions(request_params) click to toggle source

@param [Hash] request_params @return [Blacklight::Suggest::Response]

# File lib/blacklight/solr/repository.rb, line 38
def suggestions(request_params)
  suggest_results = connection.send_and_receive(suggest_handler_path, params: request_params)
  Blacklight::Suggest::Response.new suggest_results, request_params, suggest_handler_path, suggester_name
end

Private Instance Methods

build_connection() click to toggle source
# File lib/blacklight/solr/repository.rb, line 117
def build_connection
  RSolr.connect(connection_config.merge(adapter: connection_config[:http_adapter]))
end
default_search_path(solr_params) click to toggle source

@return [String]

# File lib/blacklight/solr/repository.rb, line 136
def default_search_path(solr_params)
  return blacklight_config.json_solr_path if blacklight_config.json_solr_path && uses_json_query_dsl?(solr_params)

  blacklight_config.solr_path
end
defined_rsolr_timeout_exceptions() click to toggle source

RSolr 2.4.0+ has a RSolr::Error::Timeout that we’d like to treat specially instead of lumping into RSolr::Error::Http. Before that we can not rescue specially, so return an empty array.

@return [Array<Exception>] that can be used, with a splat, as argument

to a ruby rescue
# File lib/blacklight/solr/repository.rb, line 127
def defined_rsolr_timeout_exceptions
  if defined?(RSolr::Error::Timeout)
    [RSolr::Error::Timeout]
  else
    []
  end
end
suggest_handler_path() click to toggle source

@return [String]

# File lib/blacklight/solr/repository.rb, line 109
def suggest_handler_path
  blacklight_config.autocomplete_path
end
suggester_name() click to toggle source
# File lib/blacklight/solr/repository.rb, line 113
def suggester_name
  blacklight_config.autocomplete_suggester
end
uses_json_query_dsl?(solr_params) click to toggle source

@return [Boolean]

# File lib/blacklight/solr/repository.rb, line 143
def uses_json_query_dsl?(solr_params)
  solr_params[:json].present?
end