Class: Blacklight::Solr::Repository

Inherits:
AbstractRepository show all
Defined in:
lib/blacklight/solr/repository.rb

Instance Attribute Summary

Attributes inherited from AbstractRepository

#blacklight_config, #connection

Instance Method Summary collapse

Methods inherited from AbstractRepository

#initialize

Constructor Details

This class inherits a constructor from Blacklight::AbstractRepository

Instance Method Details

#find(id, params = {}) ⇒ Object

Find a single solr document result (by id) using the document configuration

Parameters:

  • id (String)

    document’s unique key value

  • params (Hash) (defaults to: {})

    additional solr query parameters

Raises:



9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Object

Find multiple documents by their ids

Parameters:

  • _params (Hash)

    query parameters



22
23
24
# File 'lib/blacklight/solr/repository.rb', line 22

def find_many(params)
  search(params: params, path: blacklight_config.fetch_many_documents_path)
end

#pingboolean

Returns true if the repository is reachable.

Returns:

  • (boolean)

    true if the repository is reachable



52
53
54
55
56
# 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_fieldsHash

Gets a list of available fields

Returns:

  • (Hash)


46
47
48
# File 'lib/blacklight/solr/repository.rb', line 46

def reflect_fields
  send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end

#search(pos_params = {}, path: nil, params: nil, **kwargs) ⇒ Object

Execute a search query against solr

Parameters:

  • params (Hash) (defaults to: nil)

    solr query parameters

  • path (String) (defaults to: nil)

    solr request handler path



30
31
32
33
34
# File 'lib/blacklight/solr/repository.rb', line 30

def search pos_params = {}, path: nil, params: nil, **kwargs
  request_params = (params || pos_params).reverse_merge(kwargs).reverse_merge({ qt: blacklight_config.qt })

  send_and_receive(path || default_search_path(request_params), request_params)
end

#find(solr_path, params) ⇒ Blacklight::Solr::Response #find(params) ⇒ Blacklight::Solr::Response

Execute a solr query TODO: Make this private after we have a way to abstract admin/luke and ping

Overloads:

  • #find(solr_path, params) ⇒ Blacklight::Solr::Response

    Execute a solr query at the given path with the parameters

    Parameters:

    • solr (String)

      path (defaults to blacklight_config.solr_path)

    • parameters (Hash)

      for RSolr::Client#send_and_receive

  • #find(params) ⇒ Blacklight::Solr::Response

    Parameters:

    • parameters (Hash)

      for RSolr::Client#send_and_receive

Returns:

See Also:

  • Blacklight::Solr::Repository.[RSolr[RSolr::Client[RSolr::Client#send_and_receive]


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Blacklight::Suggest::Response

Parameters:

  • request_params (Hash)

Returns:



38
39
40
41
# 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