Class: SolrCloud::Connection
- Inherits:
-
Object
- Object
- SolrCloud::Connection
- Extended by:
- Forwardable
- Includes:
- AliasAdmin, CollectionAdmin, ConfigsetAdmin
- Defined in:
- lib/solr_cloud/connection.rb,
lib/solr_cloud/connection/version.rb,
lib/solr_cloud/connection/alias_admin.rb,
lib/solr_cloud/connection/configset_admin.rb,
lib/solr_cloud/connection/collection_admin.rb
Overview
The connection object is the basis of all the other stuff. Everything will be created, directly or indirectly, through the connection.
For convenience, it forwards #get, #post, #put, and #delete HTTP verbs to the underlying raw faraday http client.
Defined Under Namespace
Modules: AliasAdmin, CollectionAdmin, ConfigsetAdmin
Constant Summary collapse
- VERSION =
"1.0.1"
Instance Attribute Summary collapse
-
#connection ⇒ Faraday::Connection
readonly
The underlying Faraday connection.
-
#logger ⇒ #info
readonly
The logger.
-
#password ⇒ String
readonly
Solr password.
-
#url ⇒ String
readonly
String representation of the URL to solr.
-
#user ⇒ String
(also: #username)
readonly
Solr user.
Class Method Summary collapse
-
.new_from_faraday(faraday_connection) ⇒ Object
Pass in your own faraday connection.
Instance Method Summary collapse
-
#_version_part_int(index) ⇒ Integer
Helper method to get version parts as ints.
-
#bail_if_incompatible! ⇒ Object
Check to see if we can actually talk to the solr in question raise [UnsupportedSolr] if the solr version isn't at least 8 raise [ConnectionFailed] if we can't connect for some reason.
-
#cloud? ⇒ Boolean
Whether or not solr is running in cloud mode.
-
#create_raw_connection(url:, adapter: :httpx, user: nil, password: nil, logger: nil) ⇒ Object
Create a Faraday connection object to base the API client off of.
-
#delete ⇒ Object
Forwarded on to the underlying Faraday connection.
-
#get ⇒ Object
Forwarded on to the underlying Faraday connection.
-
#initialize(url:, user: nil, password: nil, logger: nil, adapter: :httpx) ⇒ Connection
constructor
Create a new connection to talk to solr just be the root url (not including the
/solr). - #inspect ⇒ Object (also: #to_s)
-
#legal_solr_name?(str) ⇒ Boolean
Check to see if the given string follows solr's rules for thing Solr only allows ASCII letters and numbers, underscore, and dash, and it can't start with an underscore.
-
#major_version ⇒ Integer
Solr major version.
-
#minor_version ⇒ Integer
Solr minor version.
-
#mode ⇒ String
The mode ("solrcloud" or "std") solr is running in.
-
#patch_version ⇒ Integer
Solr patch version.
-
#post ⇒ Object
Forwarded on to the underlying Faraday connection.
- #pretty_print(q) ⇒ Object
-
#put ⇒ Object
Forwarded on to the underlying Faraday connection.
-
#solr9_or_later? ⇒ Boolean
Returns true if the connected Solr instance is version 9 or later (9, 10, 11, ...).
-
#system ⇒ Hash
Get basic system info from the server.
-
#version_string ⇒ String
The major.minor.patch string of the solr version.
Methods included from AliasAdmin
#alias_map, #alias_names, #aliases, #create_alias, #get_alias, #get_alias!, #has_alias?, #raw_alias_map
Methods included from CollectionAdmin
#collection_names, #collections, #create_collection, #get_collection, #get_collection!, #has_collection?, #only_collection_names, #only_collections
Methods included from ConfigsetAdmin
#configset_names, #configsets, #create_configset, #delete_configset, #get_configset, #has_configset?
Constructor Details
#initialize(url:, user: nil, password: nil, logger: nil, adapter: :httpx) ⇒ Connection
Create a new connection to talk to solr
just be the root url (not including the /solr)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/solr_cloud/connection.rb', line 77 def initialize(url:, user: nil, password: nil, logger: nil, adapter: :httpx) @url = url @user = user @password = password @logger = case logger when :off, :none Logger.new(File::NULL, level: Logger::FATAL) when nil Logger.new($stderr, level: Logger::WARN) else logger end @connection = create_raw_connection(url: url, adapter: adapter, user: user, password: password, logger: @logger) bail_if_incompatible! @logger.info("Connected to supported solr at #{url}") end |
Instance Attribute Details
#connection ⇒ Faraday::Connection (readonly)
Returns the underlying Faraday connection.
45 46 47 |
# File 'lib/solr_cloud/connection.rb', line 45 def connection @connection end |
#logger ⇒ #info (readonly)
Returns The logger.
42 43 44 |
# File 'lib/solr_cloud/connection.rb', line 42 def logger @logger end |
#password ⇒ String (readonly)
Returns Solr password.
39 40 41 |
# File 'lib/solr_cloud/connection.rb', line 39 def password @password end |
#url ⇒ String (readonly)
Returns String representation of the URL to solr.
32 33 34 |
# File 'lib/solr_cloud/connection.rb', line 32 def url @url end |
#user ⇒ String (readonly) Also known as: username
Returns solr user.
35 36 37 |
# File 'lib/solr_cloud/connection.rb', line 35 def user @user end |
Class Method Details
.new_from_faraday(faraday_connection) ⇒ Object
Pass in your own faraday connection
96 97 98 99 100 101 |
# File 'lib/solr_cloud/connection.rb', line 96 def self.new_from_faraday(faraday_connection) c = allocate c.instance_variable_set(:@connection, faraday_connection) c.instance_variable_set(:@url, faraday_connection.build_url.to_s) c end |
Instance Method Details
#_version_part_int(index) ⇒ Integer
Helper method to get version parts as ints
159 160 161 |
# File 'lib/solr_cloud/connection.rb', line 159 def _version_part_int(index) version_string.split(".")[index].to_i end |
#bail_if_incompatible! ⇒ Object
Check to see if we can actually talk to the solr in question raise [UnsupportedSolr] if the solr version isn't at least 8 raise [ConnectionFailed] if we can't connect for some reason
125 126 127 128 129 130 |
# File 'lib/solr_cloud/connection.rb', line 125 def bail_if_incompatible! raise UnsupportedSolr.new("SolrCloud::Connection needs at least solr 8") if major_version < 8 raise UnsupportedSolr.new("SolrCloud::Connection only works in solr cloud mode") unless cloud? rescue Faraday::ConnectionFailed raise ConnectionFailed.new("Can't connect to #{url}") end |
#cloud? ⇒ Boolean
Returns whether or not solr is running in cloud mode.
148 149 150 |
# File 'lib/solr_cloud/connection.rb', line 148 def cloud? mode == "solrcloud" end |
#create_raw_connection(url:, adapter: :httpx, user: nil, password: nil, logger: nil) ⇒ Object
Create a Faraday connection object to base the API client off of
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/solr_cloud/connection.rb', line 105 def create_raw_connection(url:, adapter: :httpx, user: nil, password: nil, logger: nil) Faraday.new(request: {params_encoder: Faraday::FlatParamsEncoder}, url: URI(url)) do |faraday| faraday.use Faraday::Response::RaiseError faraday.request :url_encoded if user faraday.request :authorization, :basic, user, password end faraday.request :json faraday.response :json if logger faraday.response :logger, logger end faraday.adapter adapter faraday.headers["Content-Type"] = "application/json" end end |
#delete ⇒ Object
Forwarded on to the underlying Faraday connection
62 |
# File 'lib/solr_cloud/connection.rb', line 62 def_delegator :@connection, :delete |
#get ⇒ Object
Forwarded on to the underlying Faraday connection
52 |
# File 'lib/solr_cloud/connection.rb', line 52 def_delegator :@connection, :get |
#inspect ⇒ Object Also known as: to_s
198 199 200 |
# File 'lib/solr_cloud/connection.rb', line 198 def inspect "<#{self.class} #{@url}>" end |
#legal_solr_name?(str) ⇒ Boolean
Check to see if the given string follows solr's rules for thing Solr only allows ASCII letters and numbers, underscore, and dash, and it can't start with an underscore.
194 195 196 |
# File 'lib/solr_cloud/connection.rb', line 194 def legal_solr_name?(str) !(/[^a-zA-Z_\-.0-9]/.match?(str) or str.start_with?("-")) end |
#major_version ⇒ Integer
Returns solr major version.
164 165 166 |
# File 'lib/solr_cloud/connection.rb', line 164 def major_version _version_part_int(0) end |
#minor_version ⇒ Integer
Returns solr minor version.
169 170 171 |
# File 'lib/solr_cloud/connection.rb', line 169 def minor_version _version_part_int(1) end |
#mode ⇒ String
Returns the mode ("solrcloud" or "std") solr is running in.
143 144 145 |
# File 'lib/solr_cloud/connection.rb', line 143 def mode system["mode"] end |
#patch_version ⇒ Integer
Returns solr patch version.
174 175 176 |
# File 'lib/solr_cloud/connection.rb', line 174 def patch_version _version_part_int(2) end |
#post ⇒ Object
Forwarded on to the underlying Faraday connection
57 |
# File 'lib/solr_cloud/connection.rb', line 57 def_delegator :@connection, :post |
#pretty_print(q) ⇒ Object
204 205 206 |
# File 'lib/solr_cloud/connection.rb', line 204 def pretty_print(q) q.text inspect end |
#put ⇒ Object
Forwarded on to the underlying Faraday connection
67 |
# File 'lib/solr_cloud/connection.rb', line 67 def_delegator :@connection, :put |
#solr9_or_later? ⇒ Boolean
Returns true if the connected Solr instance is version 9 or later (9, 10, 11, ...).
Several Solr APIs changed paths between Solr 8 and Solr 9. This predicate is used internally to dispatch to the correct API endpoint when behavior differs across major versions. External callers may also use it to gate version-specific logic.
185 186 187 |
# File 'lib/solr_cloud/connection.rb', line 185 def solr9_or_later? major_version >= 9 end |
#system ⇒ Hash
Get basic system info from the server
135 136 137 138 139 140 |
# File 'lib/solr_cloud/connection.rb', line 135 def system resp = get("/solr/admin/info/system") resp.body rescue Faraday::UnauthorizedError raise Unauthorized.new("Server reports failed authorization") end |
#version_string ⇒ String
Returns the major.minor.patch string of the solr version.
153 154 155 |
# File 'lib/solr_cloud/connection.rb', line 153 def version_string system["lucene"]["solr-spec-version"] end |