Class: SolrCloud::Connection

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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)

Parameters:

  • url (String)

    URL to the "root" of the solr installation. For a default solr setup, this will

  • user (String) (defaults to: nil)

    username for basic auth, if you're using it

  • password (String) (defaults to: nil)

    password for basic auth, if you're using it

  • logger (#info, :off, nil) (defaults to: nil)

    An existing logger to pass in. The symbol ":off" means don't do logging. If left undefined, will create a standard ruby logger to $stdout

  • adapter (Symbol) (defaults to: :httpx)

    The underlying http library to use within Faraday



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

#connectionFaraday::Connection (readonly)

Returns the underlying Faraday connection.

Returns:

  • (Faraday::Connection)

    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.

Returns:

  • (#info)

    The logger



42
43
44
# File 'lib/solr_cloud/connection.rb', line 42

def logger
  @logger
end

#passwordString (readonly)

Returns Solr password.

Returns:

  • (String)

    Solr password



39
40
41
# File 'lib/solr_cloud/connection.rb', line 39

def password
  @password
end

#urlString (readonly)

Returns String representation of the URL to solr.

Returns:

  • (String)

    String representation of the URL to solr



32
33
34
# File 'lib/solr_cloud/connection.rb', line 32

def url
  @url
end

#userString (readonly) Also known as: username

Returns solr user.

Returns:

  • (String)

    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

Parameters:

  • faraday_connection (Faraday::Connection)

    A pre-build faraday connection object



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

Returns:

  • (Integer)

    Integerized version of the 0,1,2 portion of the version string



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.

Returns:

  • (Boolean)

    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

See Also:



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

#deleteObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.delete


62
# File 'lib/solr_cloud/connection.rb', line 62

def_delegator :@connection, :delete

#getObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.get


52
# File 'lib/solr_cloud/connection.rb', line 52

def_delegator :@connection, :get

#inspectObject Also known as: to_s



198
199
200
# File 'lib/solr_cloud/connection.rb', line 198

def inspect
  "<#{self.class} #{@url}>"
end

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.

Parameters:

  • str (String)

    string to check

Returns:

  • (Boolean)


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_versionInteger

Returns solr major version.

Returns:

  • (Integer)

    solr major version



164
165
166
# File 'lib/solr_cloud/connection.rb', line 164

def major_version
  _version_part_int(0)
end

#minor_versionInteger

Returns solr minor version.

Returns:

  • (Integer)

    solr minor version



169
170
171
# File 'lib/solr_cloud/connection.rb', line 169

def minor_version
  _version_part_int(1)
end

#modeString

Returns the mode ("solrcloud" or "std") solr is running in.

Returns:

  • (String)

    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_versionInteger

Returns solr patch version.

Returns:

  • (Integer)

    solr patch version



174
175
176
# File 'lib/solr_cloud/connection.rb', line 174

def patch_version
  _version_part_int(2)
end

#postObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.post


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

#putObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.put


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.

Returns:

  • (Boolean)

    true if the server reports major version >= 9



185
186
187
# File 'lib/solr_cloud/connection.rb', line 185

def solr9_or_later?
  major_version >= 9
end

#systemHash

Get basic system info from the server

Returns:

  • (Hash)

    The response from the info call

Raises:



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_stringString

Returns the major.minor.patch string of the solr version.

Returns:

  • (String)

    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