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 =
"0.6.0"
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.
-
#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`)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/solr_cloud/connection.rb', line 78 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
97 98 99 100 101 102 |
# File 'lib/solr_cloud/connection.rb', line 97 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
160 161 162 |
# File 'lib/solr_cloud/connection.rb', line 160 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
126 127 128 129 130 131 |
# File 'lib/solr_cloud/connection.rb', line 126 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.
149 150 151 |
# File 'lib/solr_cloud/connection.rb', line 149 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/solr_cloud/connection.rb', line 106 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
63 |
# File 'lib/solr_cloud/connection.rb', line 63 def_delegator :@connection, :delete |
#get ⇒ Object
Forwarded on to the underlying Faraday connection
53 |
# File 'lib/solr_cloud/connection.rb', line 53 def_delegator :@connection, :get |
#inspect ⇒ Object Also known as: to_s
188 189 190 |
# File 'lib/solr_cloud/connection.rb', line 188 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.
184 185 186 |
# File 'lib/solr_cloud/connection.rb', line 184 def legal_solr_name?(str) !(/[^a-zA-Z_\-.0-9]/.match?(str) or str.start_with?("-")) end |
#major_version ⇒ Integer
Returns solr major version.
165 166 167 |
# File 'lib/solr_cloud/connection.rb', line 165 def major_version _version_part_int(0) end |
#minor_version ⇒ Integer
Returns solr minor version.
170 171 172 |
# File 'lib/solr_cloud/connection.rb', line 170 def minor_version _version_part_int(1) end |
#mode ⇒ String
Returns the mode (“solrcloud” or “std”) solr is running in.
144 145 146 |
# File 'lib/solr_cloud/connection.rb', line 144 def mode system["mode"] end |
#patch_version ⇒ Integer
Returns solr patch version.
175 176 177 |
# File 'lib/solr_cloud/connection.rb', line 175 def patch_version _version_part_int(2) end |
#post ⇒ Object
Forwarded on to the underlying Faraday connection
58 |
# File 'lib/solr_cloud/connection.rb', line 58 def_delegator :@connection, :post |
#pretty_print(q) ⇒ Object
194 195 196 |
# File 'lib/solr_cloud/connection.rb', line 194 def pretty_print(q) q.text inspect end |
#put ⇒ Object
Forwarded on to the underlying Faraday connection
68 |
# File 'lib/solr_cloud/connection.rb', line 68 def_delegator :@connection, :put |
#system ⇒ Hash
Get basic system info from the server
136 137 138 139 140 141 |
# File 'lib/solr_cloud/connection.rb', line 136 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.
154 155 156 |
# File 'lib/solr_cloud/connection.rb', line 154 def version_string system["lucene"]["solr-spec-version"] end |