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.3.0"
Instance Attribute Summary collapse
-
#logger ⇒ #info
readonly
The logger.
-
#url ⇒ String
readonly
String representation of the URL to solr.
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`)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/solr_cloud/connection.rb', line 70 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
#logger ⇒ #info (readonly)
Returns The logger.
35 36 37 |
# File 'lib/solr_cloud/connection.rb', line 35 def logger @logger 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 |
Class Method Details
.new_from_faraday(faraday_connection) ⇒ Object
Pass in your own faraday connection
89 90 91 92 93 94 |
# File 'lib/solr_cloud/connection.rb', line 89 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
156 157 158 |
# File 'lib/solr_cloud/connection.rb', line 156 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
122 123 124 125 126 127 |
# File 'lib/solr_cloud/connection.rb', line 122 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.
145 146 147 |
# File 'lib/solr_cloud/connection.rb', line 145 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/solr_cloud/connection.rb', line 98 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
55 |
# File 'lib/solr_cloud/connection.rb', line 55 def_delegator :@connection, :delete |
#get ⇒ Object
Forwarded on to the underlying Faraday connection
45 |
# File 'lib/solr_cloud/connection.rb', line 45 def_delegator :@connection, :get |
#inspect ⇒ Object Also known as: to_s
184 185 186 |
# File 'lib/solr_cloud/connection.rb', line 184 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.
180 181 182 |
# File 'lib/solr_cloud/connection.rb', line 180 def legal_solr_name?(str) !(/[^a-zA-Z_\-0-9]/.match?(str) or str.start_with?("_")) end |
#major_version ⇒ Integer
Returns solr major version.
161 162 163 |
# File 'lib/solr_cloud/connection.rb', line 161 def major_version _version_part_int(0) end |
#minor_version ⇒ Integer
Returns solr minor version.
166 167 168 |
# File 'lib/solr_cloud/connection.rb', line 166 def minor_version _version_part_int(1) end |
#mode ⇒ String
Returns the mode (“solrcloud” or “std”) solr is running in.
140 141 142 |
# File 'lib/solr_cloud/connection.rb', line 140 def mode system["mode"] end |
#patch_version ⇒ Integer
Returns solr patch version.
171 172 173 |
# File 'lib/solr_cloud/connection.rb', line 171 def patch_version _version_part_int(2) end |
#post ⇒ Object
Forwarded on to the underlying Faraday connection
50 |
# File 'lib/solr_cloud/connection.rb', line 50 def_delegator :@connection, :post |
#pretty_print(q) ⇒ Object
190 191 192 |
# File 'lib/solr_cloud/connection.rb', line 190 def pretty_print(q) q.text inspect end |
#put ⇒ Object
Forwarded on to the underlying Faraday connection
60 |
# File 'lib/solr_cloud/connection.rb', line 60 def_delegator :@connection, :put |
#system ⇒ Hash
Get basic system info from the server
132 133 134 135 136 137 |
# File 'lib/solr_cloud/connection.rb', line 132 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.
150 151 152 |
# File 'lib/solr_cloud/connection.rb', line 150 def version_string system["lucene"]["solr-spec-version"] end |